<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Personal Weblog of John Wood &#187; tips</title>
	<atom:link href="http://www.webofwood.com/tag/tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webofwood.com</link>
	<description>Wordpress weblog of John Wood, a Database Administrator</description>
	<lastBuildDate>Fri, 16 Sep 2011 17:18:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Restore All SQL Transaction Logs using Powershell</title>
		<link>http://www.webofwood.com/2011/09/15/restore-all-sql-transaction-logs-using-powershell/</link>
		<comments>http://www.webofwood.com/2011/09/15/restore-all-sql-transaction-logs-using-powershell/#comments</comments>
		<pubDate>Thu, 15 Sep 2011 20:49:16 +0000</pubDate>
		<dc:creator>Woody</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.webofwood.com/?p=167</guid>
		<description><![CDATA[I came across another opportunity for using Powershell when setting up mirroring for a large database at our DR site. The database was over 73Gig. The problem arose when it took a very long time to copy the backup and transaction logs and run the database restore. By the time the copy had finished another [...]]]></description>
			<content:encoded><![CDATA[<p>I came across another opportunity for using Powershell when setting up mirroring for a large database at our DR site. The database was over 73Gig. The problem arose when it took a very long time to copy the backup and transaction logs and run the database restore. By the time the copy had finished another several transaction logs had been produced. While applying the *.bak  restore, yet another several transaction logs had been produced.<span id="more-167"></span></p>
<p>I don’t know about you, but using the SSMS GUI tool to restore a bunch of transaction logs is very tedious. I’ve got another number of large databases which will require mirroring also. With this in mind, I decided it would be beneficial to have a script to automate the process. What was of interest to me was being able to iterate through the process several times while copying new transaction logs. The script will process all the transaction logs in a directory at one time. If you happen to generate more transaction logs to the directory and run the script again, you will get error messages for the logs already restored but the new logs will still be restored.</p>
<p>The script also takes into account using UNC paths. So, you can run the script from any machine against an SQL Server on a remote machine.</p>
<pre class="brush: ps"> Restore-TransLogs.ps1 -server "MYSERVER\MYSQL" -database "MY_Database"
	.NOTES
		AUTHOR:    John P. Wood
		CREATED:   September, 2011
		VERSION:   1.0.0
#&gt;
Param(
	[Parameter(Mandatory=$true)]
	[string]$Server,
	[Parameter(Mandatory=$true)]
	[string]$database,
	[Parameter(Mandatory=$true)]
	[string]$FilePath
	)
Set-StrictMode -Version Latest
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended")

## SQL Server Backup\Restore uses local file paths.
Function Get-LocalPath($File) {
	$dir = $File.DirectoryName
	$rp = $dir.Remove(0,($dir.IndexOf('$')+2))
	$root = (((Get-Item $dir).Root).Name).Replace('$',':')
	$local = $root + '\' + $rp + '\' + $File
	$local
}
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)"
	}
	$smo = New-Object Microsoft.SqlServer.Management.Smo.Server($conn)
	$smo
}
Function Invoke-SqlRestore {
	Param(
		[string]$filename
	)
	# Get a new connection to the server
    $backupDevice = New-Object("Microsoft.SqlServer.Management.Smo.BackupDeviceItem") ($filename, "File")

	# Load up the Restore object settings
	$Restore = new-object Microsoft.SqlServer.Management.Smo.Restore
	$Restore.Action = [Microsoft.SqlServer.Management.Smo.RestoreActionType]::Log
	$Restore.Database = $database
    $Restore.NoRecovery = $true
	$Restore.Devices.Add($backupDevice)

	$Restore.SqlRestore($smo)
}
Clear-Host
$smo = New-SMOconnection -server $Server
If(!(Test-Path -LiteralPath $FilePath)){Throw "FilePath not found: $FilePath"}
Get-ChildItem $FilePath -Filter "*.trn" | Sort-Object -Property LastWriteTime |
	ForEach-Object {
		If(($_.Fullname).StartsWith('\\')) {$file = Get-LocalPath $_ }
		Else {$file = $_.FullName}
		Try {Invoke-SqlRestore -filename $file}
		Catch {
			$ex = $Error[0].Exception
			Write-Output $ex.message
			while ($ex.InnerException)
			{
				$ex = $ex.InnerException
				Write-Output $ex.message
			}
		}
}
If ($smo.ConnectionContext.IsOpen -eq $true) {$smo.ConnectionContext.Disconnect()}</pre>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webofwood.com/2011/09/15/restore-all-sql-transaction-logs-using-powershell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tips &amp; Tricks Using Compare-Object &#8211; Copy New Files</title>
		<link>http://www.webofwood.com/2009/07/29/tips-tricks-using-compare-object-copy-new-files/</link>
		<comments>http://www.webofwood.com/2009/07/29/tips-tricks-using-compare-object-copy-new-files/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 15:57:32 +0000</pubDate>
		<dc:creator>Woody</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[compare]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.webofwood.com/2009/07/29/tips-tricks-using-compare-object-copy-new-files/</guid>
		<description><![CDATA[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 &#38; Tricks Using Compare-Object &#8211; Dreaming in PowerShell &#8211; PowerShell.com I am very new to PowerShell but I am beginning to like using it considerably more each day. I recently had [...]]]></description>
			<content:encoded><![CDATA[<p>This is a wonderful article which not only details the use of Compare-Object but also provides some very excellent examples on its use.</p>
<p><a href="http://powershell.com/cs/blogs/tobias/archive/2009/01/09/tipps-amp-tricks-using-compare-object.aspx">Tips &amp; Tricks Using Compare-Object &#8211; Dreaming in PowerShell &#8211; PowerShell.com</a></p>
<p>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 <a title="Compare-Object" href="http://powershell.com/cs/blogs/tobias/archive/2009/01/09/tipps-amp-tricks-using-compare-object.aspx" target="_blank">excellent article</a>.</p>
<p>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.</p>
<h3>The script code:</h3>
<pre class="brush: ps">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 '&lt;=' } |
    foreach-object -process{
        copy-item $_.FullName -destination $t
        }</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.webofwood.com/2009/07/29/tips-tricks-using-compare-object-copy-new-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

