Archive for the ‘Powershell’ Category

Management recently decided to use database mirroring as our DR solution. Because mirroring is done at the database level and not at the server level, I had a lot, a very lot of databases to be mirrored. To make it easier I decided to cobble together a simplistic script to do this.

I typically use a ‘management’ server for most of my needs and have therefore written the script to use UNC type pathing for the primary as well as mirror server. You will also notice I defaulted several of the parameters. This is handy when working on a set of servers for multiple databases.

I hope you find the script useful. You can also find a copy of the script at www.scriptinganswers.com.

del.icio.us Tags: ,,
<#
	.SYNOPSIS
		Set up an SQL Server mirrored database
	.DESCRIPTION
		Backs up a database and tlog, copies it to the destination,
		Restores the database on the mirror server, sets up the partner,
		and starts the mirror.
	.PARAMETER  database
		The name of the database to be mirrored
	.PARAMETER  SourceServer
		The name of the primary server
	.PARAMETER  SourcePath
		Local Path for the backup
	.PARAMETER  DestServer
		The name of mirror server
	.PARAMETER  DestPath
		Local path for restore file
	.EXAMPLE
		PS C:\> Invoke-Mirror -database 'string value' `
			-SourceServer 'string\string' -SourcePath 'string' `
			-DestServer 'string\string' -DestPath 'string'
	.NOTES
		The SQL connections rely on Windows authentication and assumes Endpoints
		already exist. Error checking is minimal (i.e. no check is made to
		verify the recovery model is FULL).
	.AUTHOR
		John P. Wood
		07/28/2010
#>
Param(
	[Parameter(Mandatory=$true)]
	[string]$database,
	[string]$SourceServer='Server1\Instance',
	[string]$SourcePath='K:\SQL Backups',
	[string]$DestServer='Server2\Instance',
	[string]$DestPath='K:\SQL Backups'
	)
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended")
Function New-SMOconnection {
    Param (
		[string]$server
	)
	$conn = New-Object Microsoft.SqlServer.Management.Common.ServerConnection($server)
	$conn.applicationName = "PowerShell SMO"
	$conn.StatementTimeout = 0
	$conn.Connect()
	if ($conn.IsOpen -eq $false) {
		Throw "Could not connect to server $($server) for database backup of $($dbname)."
	}
	$smo = New-Object Microsoft.SqlServer.Management.Smo.Server($conn)
	$smo
}
Function Invoke-SqlBackup {
	$dbbk = new-object ('Microsoft.SqlServer.Management.Smo.Backup')
	$dbbk.Action = [Microsoft.SqlServer.Management.Smo.BackupActionType]::Database
	$dbbk.BackupSetDescription = "Full backup of " + $database
	$dbbk.BackupSetName = $database + " Backup"
	$dbbk.Database = $database
	$dbbk.MediaDescription = "Disk"
	$device = "$SourcePath\$bkpfile"
	$dbbk.Devices.AddDevice($device, 'File')
	$smo = New-SMOconnection -server $SourceServer
	Try {
		$dbbk.SqlBackup($smo)
		$dbbk.Action = [Microsoft.SqlServer.Management.Smo.BackupActionType]::Log
		$dbbk.SqlBackup($smo)
		$smo.ConnectionContext.Disconnect()
	}
	Catch {
		$ex = $_.Exception
		Write-Output $ex.message
		$ex = $ex.InnerException
		while ($ex.InnerException)
		{
			Write-Output $ex.InnerException.message
			$ex = $ex.InnerException
		};
		continue
	}
	Finally {
		if ($smo.ConnectionContext.IsOpen -eq $true) {
			$smo.ConnectionContext.Disconnect()
		}
	}
}
Function Invoke-SqlRestore {
	Param([string]$filename)
	$restore = new-object Microsoft.SqlServer.Management.Smo.Restore
	$restore.Action = 'Database'
	$restore.Database = $database
	$device = "$DestPath\$filename"
	$restore.Devices.AddDevice($device, 'File')
    $restore.ReplaceDatabase = $true
    $restore.NoRecovery = $true
	$smo = New-SMOconnection -server $DestServer
	Try {
		$restore.SqlRestore($smo)
		$restore.Action = [Microsoft.SqlServer.Management.Smo.RestoreActionType]::Log
		$restore.FileNumber = 2
		$restore.SqlRestore($smo)
		$smo.ConnectionContext.Disconnect()
	}
	Catch {
		$ex = $_.Exception
		Write-Output $ex.message
		$ex = $ex.InnerException
		while ($ex.InnerException)
		{
			Write-Output $ex.InnerException.message
			$ex = $ex.InnerException
		};
		continue
	}
	Finally {
		if ($smo.ConnectionContext.IsOpen -eq $true) {
			$smo.ConnectionContext.Disconnect()
		}
	}
}
Function Set-Mirror {
	Param([string]$server,[string]$database,[string]$partner)
	$conn = "Server=$server; Integrated Security=SSPI; Database=Master"
	$cn = New-Object "System.Data.SqlClient.SqlConnection" $conn
	$cn.Open()
	$cmd = New-Object "System.Data.SqlClient.SqlCommand"
	$cmd.CommandType = [System.Data.CommandType]::Text

	$cmd.CommandText = "ALTER DATABASE $database SET PARTNER = 'TCP://" + $partner + ":5022'"
	$cmd.Connection = $cn
	$cmd.ExecuteNonQuery()
	$cn.Close()
	Trap {
		$ex = $_.Exception
		Write-Output $ex.message
		$ex = $ex.InnerException
		while ($ex.InnerException)
		{
			Write-Output $ex.InnerException.message
			$ex = $ex.InnerException
		};
		continue
	}
}
$srcUNC = Join-Path "\\$($SourceServer.Split('\\')[0])" $($SourcePath.Replace(':','$'))
if (-not(Test-Path $srcUNC)) { New-Item $srcUNC -ItemType directory | Out-Null}
$destUNC = Join-Path "\\$($DestServer.Split('\\')[0])" $($DestPath.Replace(':','$'))
if (-not(Test-Path $destUNC)) { New-Item $destUNC -ItemType directory | Out-Null}
$bkpfile = $($SourceServer.Replace("\", "$")) + "_" + $database + "_FULL_" + $(get-date -format yyyyMMdd-HHmmss) + ".bak"
Invoke-SqlBackup
Copy-Item $(Join-Path $srcUNC $bkpfile) -Destination $destUNC -Verbose
Invoke-SqlRestore -filename $bkpfile
# Establish Mirroring from the mirrored database
Set-Mirror -server $DestServer -database $database -partner $($SourceServer.Split('\\')[0])
# Start the mirror
Set-Mirror -server $SourceServer -database $database -partner $($DestServer.Split('\\')[0])

Powershell is a fantastic tool to use for management of multiple computers. I have been slowly converting many of our administrative functions from a hodge-podge mixture of CMD, BAT, VPS, Python, and Perl scripts. One daily administrative talks is copying a variety of backups from a variety of Windows servers to our data ’warehouse’ where they are then copied to tape.

I recently found the “Jobs” cmdlets in Powershell V2.0. With “Jobs” you can asynchronously process multiple tasks (e.g. copying backups from many remote machines to data storage on the current local machine). In my case, running the copies synchronously results in the process spanning over to the next morning. Backups are stored daily. If a backup is taken on Wednesday, I can’t have it being stored in our warehouse under Thursday.

Continue reading ‘Asynchronous Processing using Powershell Jobs’ »

Don’t you just hate it when you open one of your server logs in Management Studio and then you have to wait forever to review it because it never stops reading? I’ve done it enough times and decided to put an end to it or at least greatly reduce the incidence.

Using Powershell and a simple script to execute Sp_Cycle_Errorlog, I now have a weekly, Windows scheduled task which executes the sproc to cycle the logs for all my SQL Servers.

foreach ($svr in get-content D:\Scripts\Servers.TXT ){
    $svr
    Invoke-Expression 'SQLCMD -E -S $svr -Q "Exec Sp_Cycle_Errorlog"' | Out-Null
	}

As a databases admin with a few dozen servers and a few hundred SQL Server database, I need to effectively build automated jobs to perform various administrative tasks such as backing up databases. I’m still new to PowerShell but I am beginning to see how immensely helpful it is to me and my work.

I found a good PowerShell script for backing up databases at this blog location. However, I wanted to change it slightly to accommodate backing up ALL, System, User, or a list of databases. Processing ALL, System, or User is straightforward enough but I had to it was the first time I came up against have to compare a value against a list of values. In SQL you simply use an IN operator with a list of values but I was not sure of how to accomplish the same process in PowerShell.

Here was my problem: the user could supply a list of databases to backup and the backup script has a line with captures all the database objects for that SQL Server instance. I need to backup only those which are in the users list.

I found that the PowerShell WHERE clause can use a -contains operator which effectively filters the selection for an array. To test the operation I set up a small script to build two arrays and then I piped one array through a WHERE filter of which the results are piped to a ForEach. The ForEach sees only the results of the WHERE which mimics the SQL IN operator.

Run this code in PowerShell to see what I mean.

$a = @("db1","db2","db3","db4")
$d = @("db1","db2","db3","db4","db5","db6","db7","db8")
$d | where { $a -contains $_ } | foreach { Write-Host $_ }

$a represents the list of database supplied by the user.
$d represents the array of databases returned when accessing the Microsoft.SqlServer.Management.Smo.Server object.
I can now back up the user supplied databases within the foreach script block.

This is a wonderful article which not only details the use of Compare-Object but also provides some very excellent examples on its use.

Tips & Tricks Using Compare-Object – Dreaming in PowerShell – PowerShell.com

I am very new to PowerShell but I am beginning to like using it considerably more each day. I recently had a need for a quick and easy way to script a process which would copy only new files from one location to another. after scouring the Internet for a good example, I stumbled upon the Compare-Object cmdlet and this excellent article.

The actual script I came up with is rather simple and serves my purpose exactly. As I  mentioned, I am new to PowerShell and there may very well be a better method. However, this one works for me.

The script code:

param(
    [string]$s = '\\serverx\c$\ProgramData\Polaris\3.5\SQLVS1\AuthorityUpdates',
    [string]$t = '\\servery\data03\shared\libraries\zmarc'
)

$target = Get-ChildItem $t
$source = get-childitem $s
Compare-Object $source $target -Property Name -PassThru |
    Where-Object { $_.SideIndicator -eq '<=' } |
    foreach-object -process{
        copy-item $_.FullName -destination $t
        }