Your poor man’s SQL Server log shipping-PowerShell version

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 of the beneficiary.

I wrote a chapter for the book based on this blog post some three years ago and one of the recommendations I did was to convert the VBScript scripts to Windows PowerShell. So, here it is.

I’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’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 – no need to add an entry for every database that will be configured. How cool was that?

So, here’s the Windows PowerShell version of the custom restore LOG backup command

$logBackupFolder="your LOG backup location here or a parameter variable"

##Variable for time duration - the amount of time you need to generate and restore LOG backups
= [DateTime]::Now.AddHours(-1)

##Retrieve folders and files in the specified directory
foreach ($databaseName in (Get-ChildItem $logBackupFolder | Where { $_.PsIsContainer }) )
foreach ($logName in (Get-ChildItem $logBackupFolder$databasename | where {($_.CreationTime -ge $1HourOld) -and ($_.Extension -eq ".trn")} SELECT name))

$logBackupPath = [System.String]::Concat("'$logBackupFolder",$databasename,"",$logName.Name,"'")
$restoreSQL = "RESTORE LOG $databaseName FROM DISK=$logBackupPath WITH NORECOVERY" | Out-File D:$databaseName.sql

Invoke-Expression "osql.exe -SYourSQLServerInstanceHere -E -iD:$databaseName.sql"


You might be wondering why I am using osql.exe instead of sqlcmd.exe. Well, it’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.

And all of that for a mere 17-liner script. I could write it even less than that but I’m a big fan of script readability. Now, that’s a good enough reason to dig deeper on what Windows PowerShell has to offer

Please note: I reserve the right to delete comments that are offensive or off-topic.

Leave a Reply

Your email address will not be published.

10 thoughts on “Your poor man’s SQL Server log shipping-PowerShell version

  1. >Hi Hugo,I've read your documentation a lot of times and it is really, really impressive. As somebody who is a bit of a paranoid when it comes to security, I just don't like enabling xp_cmdshell on my TSQL scripts 🙂

  2. not sure if its just me but you seem to be missing a couple of pipes ( | ) in your script….

    Get-ChildItem $logBackupFolder$databasename where {($_.CreationTime
    should be
    Get-ChildItem $logBackupFolder$databasename | where {($_.CreationTime

    great script nonetheless!!!

  3. Hi Chris,

    This blog post was imported from Blogger and from the post, the PowerShell scripts are being modified after being posted. I guess it has something to do with HTML conversions. Thanks for pointing that out.

    I’ve updated the blog post to reflect the missing pipelines. Hopefully, I won’t have the same issues in future blog posts

      • Thanks for the tip, Chris. I guess I have to learn more about WordPress now that I’ve been using it for quite some time. I’ve incorporated your tip in this blog post. Now, to check the other blog posts that have PowerShell scripts in them 🙂

  4. Edwin….Its a script but I am getting an error for out-file, any idea?
    Unexpected token ‘Out-File’ in expression or statement.
    At C:scriptsres3.ps1:13 char:93
    + $restoreSQL = “RESTORE LOG $databaseName FROM DISK=$logBackupPath WITH NORECO
    VERY” Out-File <<<< D:$databaseName.sql