<?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>Edwin M SarmientoYour poor man&#8217;s SQL Server log shipping-PowerShell version &#8211; Edwin M Sarmiento</title>
	<atom:link href="https://www.edwinmsarmiento.com/your-poor-mans-sql-server-log-shipping-powershell-version/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.edwinmsarmiento.com</link>
	<description>Intentional Excellence</description>
	<lastBuildDate>Mon, 13 Apr 2026 21:00:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
<site xmlns="com-wordpress:feed-additions:1">84283043</site>		<item>
		<title>Your poor man&#8217;s SQL Server log shipping-PowerShell version</title>
		<link>https://www.edwinmsarmiento.com/your-poor-mans-sql-server-log-shipping-powershell-version/</link>
		<comments>https://www.edwinmsarmiento.com/your-poor-mans-sql-server-log-shipping-powershell-version/#comments</comments>
		<pubDate>Sat, 01 May 2010 14:18:00 +0000</pubDate>
		<dc:creator>Edwin M Sarmiento</dc:creator>
				<category><![CDATA[log shipping]]></category>
		<category><![CDATA[modify SQL Server logins]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SQL Server]]></category>
		<guid isPermaLink="false">http://bassplayerdoc.wordpress.com/2010/05/01/your-poor-mans-sql-server-log-shipping-powershell-version</guid>

				<description><![CDATA[In 2008, I was privileged to be a part of a project to write a chapter for a book that was for a good cause. SQL Server MVP Deep Dives became an avenue for a lot of the SQL Server MVPs to share their expertise for the benefit of not just the technical community but [&#8230;]]]></description>
					<content:encoded><![CDATA[<p><span style="font-family: arial;">In 2008, I was privileged to be a part of a project to write a chapter for a book that was for a good cause. </span><a href="http://www.manning.com/nielsen/"><span style="font-family: arial;">SQL Server MVP Deep Dives</span></a><span style="font-family: arial;"> became an avenue for a lot of the SQL Server MVPs to share their expertise for the benefit of not just the technical community but of the beneficiary.</span></p>
<p><span style="font-family: Arial;">I wrote a chapter for the book based on this <a href="https://www.edwinmsarmiento.com/your-poor-mans-sql-server-log-shipping/" target="_blank">blog post</a> some three years ago and one of the recommendations I did was to convert the VBScript scripts to Windows PowerShell. So, here it is.</span></p>
<p><span style="font-family: Arial;">I&#8217;ve converted the VBScript that does the checking of the folders containing my latest LOG backups and restoring them in NORECOVERY mode to Windows PowerShell. What is fascinating is the fact that if the process I&#8217;ve outlined in the blog post is followed thru, adding another database to be configured for log shipping was as easy as creating regular LOG backups via TSQL scripts or database maintenance plans &#8211; no need to add an entry for every database that will be configured. How cool was that?</span></p>
<p><span style="font-family: Arial;">So, here&#8217;s the Windows PowerShell version of the custom restore LOG backup command</span></p>
<p><code style="font-size: 12px;"><span style="color: black;">$logBackupFolder</span><span style="color: blue;">=</span><span style="color: darkred;">"your LOG backup location here or a parameter variable"</p>
<p></span><span style="color: black;">##Variable </span><span style="color: blue;">for </span><span style="color: black;">time duration </span><span style="color: gray;">- </span><span style="color: black;">the amount of time you need to generate and restore LOG backups<br />
$1HourOld </span><span style="color: blue;">= </span><span style="color: black;">[DateTime]</span><span style="color: gray;">::</span><span style="color: black;">Now.AddHours</span><span style="color: gray;">(-</span><span style="color: black;">1</span><span style="color: gray;">)</p>
<p></span><span style="color: black;">##Retrieve folders and files </span><span style="color: blue;">in </span><span style="color: black;">the specified directory<br />
</span><span style="color: blue;">foreach </span><span style="color: gray;">(</span><span style="color: black;">$databaseName </span><span style="color: blue;">in </span><span style="color: gray;">(</span><span style="color: black;">Get</span><span style="color: gray;">-</span><span style="color: black;">ChildItem $logBackupFolder </span><span style="color: gray;">| </span><span style="color: black;">Where { $_.PsIsContainer }</span><span style="color: gray;">) )<br />
</span><span style="color: black;">{<br />
</span><span style="color: blue;">foreach </span><span style="color: gray;">(</span><span style="color: black;">$logName </span><span style="color: blue;">in </span><span style="color: gray;">(</span><span style="color: black;">Get</span><span style="color: gray;">-</span><span style="color: black;">ChildItem $logBackupFolder$databasename </span><span style="color: gray;">| </span><span style="color: black;">where {</span><span style="color: gray;">(</span><span style="color: black;">$_.CreationTime </span><span style="color: gray;">-</span><span style="color: black;">ge $1HourOld</span><span style="color: gray;">) -</span><span style="color: black;">and </span><span style="color: gray;">(</span><span style="color: black;">$_.Extension </span><span style="color: gray;">-</span><span style="color: black;">eq </span><span style="color: darkred;">".trn"</span><span style="color: gray;">)</span><span style="color: black;">} SELECT name</span><span style="color: gray;">))<br />
</span><span style="color: black;">{</p>
<p>$logBackupPath </span><span style="color: blue;">= </span><span style="color: black;">[System.String]</span><span style="color: gray;">::</span><span style="color: black;">Concat</span><span style="color: gray;">(</span><span style="color: darkred;">"'$logBackupFolder"</span><span style="color: gray;">,</span><span style="color: black;">$databasename</span><span style="color: gray;">,</span><span style="color: darkred;">""</span><span style="color: gray;">,</span><span style="color: black;">$logName.Name</span><span style="color: gray;">,</span><span style="color: darkred;">"'"</span><span style="color: gray;">)<br />
</span><span style="color: black;">$restoreSQL </span><span style="color: blue;">= </span><span style="color: darkred;">"RESTORE LOG $databaseName FROM DISK=$logBackupPath WITH NORECOVERY" </span><span style="color: gray;">| </span><span style="color: black;">Out</span><span style="color: gray;">-</span><span style="color: black;">File D:$databaseName.sql</p>
<p>Invoke</span><span style="color: gray;">-</span><span style="color: black;">Expression </span><span style="color: darkred;">"osql.exe -SYourSQLServerInstanceHere -E -iD:$databaseName.sql"</p>
<p></span><span style="color: black;">}<br />
}<br />
</span></code></p>
<p><span style="font-family: Arial;">You might be wondering why I am using osql.exe instead of sqlcmd.exe. Well, it&#8217;s still there even if you have SQL Server 2008. Which means this script will work even for SQL Server 2000 instances as long as you install Windows PowerShell on the machine running SQL Server.</span></p>
<p><span style="font-family: Arial;">And all of that for a mere 17-liner script. I could write it even less than that but I&#8217;m a big fan of script readability. Now, that&#8217;s a good enough reason to dig deeper on what Windows PowerShell has to offer</span></p>
<div class="blogger-post-footer"><img alt="" width="1" height="1" /></div>
]]></content:encoded>
			

		<wfw:commentRss>https://www.edwinmsarmiento.com/your-poor-mans-sql-server-log-shipping-powershell-version/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
				<post-id xmlns="com-wordpress:feed-additions:1">213</post-id>	</item>
	</channel>
</rss>