I am trying to maken a PS script that retores multiple Analisys DB's to SSAS from windows explorer.
Here are de DB's F:\Backup\SSAS\SQL002\ xx.abf and yy.abf and many more. And the sript that i use is this:
$SQLInstanceName = "SQL002"
\#$DatabaseName = Get-ChildItem $BackupFilePath -Filter \*.abf -Recurse
$DatabaseName = "DBC_tst__yesterday"
$BackupFilePath = "\\localhost\\F$\\Backup\\SSAS\\SQL002"
## Add the AMO namespace
$loadInfo = \[System.Reflection.Assembly\]::LoadWithPartialName("Microsoft.AnalysisServices") | Out-Null
\[Microsoft.AnalysisServices.Server\]$server = New-Object Microsoft.AnalysisServices.Server
$server.connect($SQLInstanceName)
if ($server.name -eq $null) {
Write-Output ("Server '{0}' not found" -f $ServerName)
break
}
# Lets restore using the default locations
\#If ($BackupFilePath -match '.abf') { $File = $BackupFilePath }
$File = (Get-ChildItem $BackupFilePath -Filter \*.abf -Recurse| Sort LastWriteTime -Descending | Select -First 1).Fullname
Write-Host "File Variable contains " $File
Write-Host "Restoring $File on $SQLInstanceName"
$Server.restore($File, $DatabaseName, $true, $null)
if ($?) {"Successfully Restore " $DatabaseName " to " $SQLInstanceName }
else {"Failed to Restore " $DatabaseName " to " $SQLInstanceName }
$server.Disconnect()
It only restores 1 db, how do i get it done that it restore als db's in de directory? I tried already with an other variable. But that didn't work.
Thank you in advanced.
The result of my script is that it restores only 1 db.
CodePudding user response:
Try following :
$BackupFilePath = "\\localhost\\F$\\Backup\\SSAS\\SQL002"
$SQLInstanceName = "SQL002"
$DatabaseName = "DBC_tst__yesterday"
$Files = Get-ChildItem $BackupFilePath -Filter \*.abf -Recurse -File
foreach($File in $Files)
{
Write-Host "File Variable contains " $File.Fullname
Write-Host "Restoring $File on $SQLInstanceName"
$Server.restore($File.Fullname, $DatabaseName, $true, $null)
if ($?) {"Successfully Restore " $DatabaseName " to " $SQLInstanceName }
else {"Failed to Restore " $DatabaseName " to " $SQLInstanceName }
}
$server.Disconnect()
CodePudding user response:
I modified the script. but doesn't still work. I even do not get a error anymore.
$BackupFilePath = "F:\Backup\SSAS\SQL002\"
$SQLInstanceName = "SQL002"
$DatabaseName = "DBC_tst__yesterday"
$Files = Get-ChildItem $BackupFilePath -Filter -File -Recurse
foreach($File in $Files)
{
Write-Host "File Variable contains " $File.Name
Write-Host "Restoring $File on $SQLInstanceName"
$Server = New-Object Microsoft.AnalysisServices.Server
$Server.Connect($SQLInstanceName)
$Server.restore($File.Fullname, $DatabaseName, $true, $null)
if ($?) {"Successfully Restore " $DatabaseName " to " $SQLInstanceName }
else {"Failed to Restore " $DatabaseName " to " $SQLInstanceName }
$server.Disconnect()
}
Please help