-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackup-SSASDatabaseScript.ps1
More file actions
46 lines (35 loc) · 1.65 KB
/
Backup-SSASDatabaseScript.ps1
File metadata and controls
46 lines (35 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Function Backup-SSASDatabaseScript
{
[CmdletBinding()]
Param (
[parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
[object]$sqlServer,
[object]$OutputFolder
)
[string]$ExecutionDate = (Get-Date).ToString("yyyyMMdd-HHmmss")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.AnalysisServices") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("System.Xml") | Out-Null
#Connect to the server.
$SSASServer = New-Object Microsoft.AnalysisServices.Server
$SSASServer.Connect($sqlServer)
foreach ($db in $SSASServer.Databases)
{
#[string]$OutputPath = "$OutputFolder\$db\"
#Write-Host "Scripting:" $db.Name " "
$FileWriter = New-Object System.Xml.XmlTextWriter("$($OutputFolder)SSAS_$($db.Name)_$($ExecutionDate).xmla", [System.Text.Encoding]::UTF8)
$FileWriter.Formatting = [System.Xml.Formatting]::Indented
[Microsoft.AnalysisServices.Scripter]::WriteCreate($FileWriter,$SSASServer,$db,$true,$true)
$FileWriter.Close()
#Keep most recent .xmla file. Delete older one(s).
$NumberToSave = 1
$files = Get-ChildItem "$OutputFolder\*$db*.xmla" | Sort-Object LastWriteTime -Descending
#$files
if ($NumberToSave -lt $files.Count)
{
$files[$NumberToSave..($files.Count-1)] | Remove-Item
} #End if.
} #End foreach.
#Disconnect from SSAS.
$SSASServer.Disconnect()
}#End Backup-SSASDatabaseScript function.
#Backup-SSASDatabaseScript -sqlServer "Server1" -OutputFolder "C:\Users\user\SSAS_backups"