private775 icon

PS: SP - restore all backups from a dir

private775 | PRO | 01/26/16 03:36:49 PM UTC | 0 ⭐ | 598 👁️ | Never ⏰ | []
PowerShell |

1.02 KB

|

None

|

0 👍

/

0 👎

$waUrl = "http://contoso/"
$backupDir = "E:\temp\20160126"
$dbNamePrefix = "WSS_Content"
 
 
$files = Get-Item "$($backupDir)\*.bak"
 
foreach($file in $files){
 
    $path = $file.FullName
    $name = $file.Name
 
    $nodt = $name.Substring(12)
    $pos = $nodt.LastIndexOf('.')
    $noExt = $nodt.Substring(0, $pos)
    $srUrl = $noExt.Replace("_","/")
 
 
    $siteUrl = $waUrl.TrimEnd("/") + $srUrl
 
    $s = get-spsite -Identity $siteUrl -ErrorAction SilentlyContinue
 
    if($s -ne $null){
        remove-spsite -Identity $siteUrl -Verbose -Confirm:$false
    }
 
 
 
    $dbName = $dbNamePrefix + $noExt
 
 
 
    $dbExists = Get-SPContentDatabase -Identity $dbName -ErrorAction SilentlyContinue
    if($dbExists -ne $null){
        Remove-SPContentDatabase -Identity $dbName -Force -Verbose -Confirm:$false
    }
 
    New-SPContentDatabase -Name $dbName -WebApplication $waUrl -Verbose -Confirm:$false
 
    Restore-SPSite -Identity $siteUrl -ContentDatabase $dbName -Path $path -Verbose -Confirm:$false
}

Comments