Advertisement
private775

PS: SP - restore all backups from a dir

Jan 26th, 2016
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $waUrl = "http://contoso/"
  2. $backupDir = "E:\temp\20160126"
  3. $dbNamePrefix = "WSS_Content"
  4.  
  5.  
  6. $files = Get-Item "$($backupDir)\*.bak"
  7.  
  8. foreach($file in $files){
  9.  
  10.     $path = $file.FullName
  11.     $name = $file.Name
  12.  
  13.     $nodt = $name.Substring(12)
  14.     $pos = $nodt.LastIndexOf('.')
  15.     $noExt = $nodt.Substring(0, $pos)
  16.     $srUrl = $noExt.Replace("_","/")
  17.  
  18.  
  19.     $siteUrl = $waUrl.TrimEnd("/") + $srUrl
  20.  
  21.     $s = get-spsite -Identity $siteUrl -ErrorAction SilentlyContinue
  22.  
  23.     if($s -ne $null){
  24.         remove-spsite -Identity $siteUrl -Verbose -Confirm:$false
  25.     }
  26.  
  27.  
  28.  
  29.     $dbName = $dbNamePrefix + $noExt
  30.  
  31.  
  32.  
  33.     $dbExists = Get-SPContentDatabase -Identity $dbName -ErrorAction SilentlyContinue
  34.     if($dbExists -ne $null){
  35.         Remove-SPContentDatabase -Identity $dbName -Force -Verbose -Confirm:$false
  36.     }
  37.  
  38.     New-SPContentDatabase -Name $dbName -WebApplication $waUrl -Verbose -Confirm:$false
  39.  
  40.     Restore-SPSite -Identity $siteUrl -ContentDatabase $dbName -Path $path -Verbose -Confirm:$false
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement