Skip to content

Instantly share code, notes, and snippets.

@dgosbell
Created July 20, 2016 03:23
Show Gist options
  • Save dgosbell/9b135d9e357acbe25faca504f93d2b2b to your computer and use it in GitHub Desktop.
Save dgosbell/9b135d9e357acbe25faca504f93d2b2b to your computer and use it in GitHub Desktop.
## This is an example script showing how to detect the compatibility level
## and execute the DBCC command against multiple databases
## note: to execute DBCC from powershell you need to wrap it in <Execute> and <Command> wrappers
## or it won't work
## Author: Darren Gosbell
## Date : 19 July 2016
$serverName = 'localhost\tab16'
[Reflection.Assembly]::LoadWithPartialName("Microsoft.AnalysisServices") > $null
$server = New-Object Microsoft.AnalysisServices.Server
$server.connect($serverName)
foreach ($db in $server.Databases) {
if ($db.CompatibilityLevel -ge 1200) {
$cmd = "<Execute xmlns=""urn:schemas-microsoft-com:xml-analysis"">
<Command>
<DBCC xmlns=""http://schemas.microsoft.com/analysisservices/2014/engine"">
<DatabaseID>$($db.ID)</DatabaseID>
</DBCC>
</Command>
<Properties />
</Execute>
"
} else {
$cmd = "<Execute xmlns=""urn:schemas-microsoft-com:xml-analysis"">
<Command>
<DBCC xmlns=""http://schemas.microsoft.com/analysisservices/2003/engine"">
<Object>
<DatabaseID>$($db.ID)</DatabaseID>
<CubeID>$($db.Cubes[0].ID)</CubeID>
</Object>
</DBCC>
</Command>
<Properties />
</Execute>"
}
$db.Cubes
TRY{
[xml]$res = Invoke-ASCmd -Query $cmd -Server "$serverName" -Database $db.Name -ErrorVariable e -OutVariable out -WarningVariable w
if ($x.ExecuteResponse.return.root.xmlns -eq "urn:schemas-microsoft-com:xml-analysis:empty") {
write-host "DBCC No Errors found for $($db.Name)" -ForegroundColor Green
}
else
{
Write-warning "DBCC invoked for $($db.Name) results not empty"
}
}
CATCH{
write-error "$db $serverName $($_.Exception.Message)"
}
}
$server.Disconnect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment