Skip to content

Instantly share code, notes, and snippets.

@Stuart-Moore
Created January 31, 2017 07:35
Show Gist options
  • Save Stuart-Moore/1215b9bba857a49430aab9243bb1557b to your computer and use it in GitHub Desktop.
Save Stuart-Moore/1215b9bba857a49430aab9243bb1557b to your computer and use it in GitHub Desktop.
Restore striped SQL Server backup from share using powershell
#Written off the top of my head, check before relying on it.
import-module "SQLPS" -DisableNameChecking
$sqlsvr = New-Object -TypeName Microsoft.SQLServer.Management.Smo.Server("Server1")
$restore = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Restore
$devicetype = [Microsoft.SqlServer.Management.Smo.DeviceType]::File
$files=(Get-ChildItem \\YourServer\Yourshare\FULL\ -Filter *.bak | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-1)})
foreach ($file in $files){
$backupdevice = New-Object -TypeName Microsoft.SQLServer.Management.Smo.BackupDeviceItem($file.fullname,$devicetype)
$backups.Devices.Add($backupdevice)
}
$restore.Database = "YourDB"
$restore.ReplaceDatabase = $True
$restore.SQLRestore($sqlsvr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment