Quantcast
Channel: SQLServerCentral » Programming » Powershell » Latest topics
Viewing all articles
Browse latest Browse all 467

Using Restore-SQLDatabase

$
0
0
when I run the script below in ISE it runs fine. when I try to schedule as a job to run it blows up. I get the following error "get-childitem : Cannot call method. The provider does not support the use of filters."I have tried scheduling as a CMD type job in SQL and as a powershell type job. Any ideas??import-module sqlps -DisableNameChecking$server = "<servername>"$DBNAME = "<dbname>"$path = "<UNC Path to backup location>\$Server\$dbname\" $lastwritetime = gci $path -Filter *.BAK | Measure-Object -Property lastwritetime -Maximum $databasefiles = gci $path -Filter *.bak | Where-Object{$_.LastWriteTime -GE $($lastwritetime.Maximum)} | Sort-Object -Property {[int]$_.Name.Split('of')[0]} ## set loop variable$i = 0<# counts number of files. if only one .bak file then will execute the following statement create a string array and initalize #>[string[]] $bup = @()$bup = if($databasefiles.count -eq 1){$path + $databasefiles[$i]}else{while($i -lt $databasefiles.length){ if ($i -eq $databasefiles.Length -1) {$path + $databasefiles[$i]} else {$path + $databasefiles[$i]} $i++ }}Restore-SqlDatabase -Database <DBName> -ServerInstance <ServerName> -BackupFile $bup -ReplaceDatabase

Viewing all articles
Browse latest Browse all 467

Trending Articles