Thursday, 2 August 2012

Powershell: Check is Analysis Server / SQL Server is running!


If you are planning automation then it becomes necessary to check if the server is up and running before passing query to it.

Below is the Power-shell script which quickly checks if the SQL Server Analysis Server / SQL Server is running or not.

Checking Analysis Server (SSAS)
# Load AMO assembly
[reflection.assembly]::LoadWithPartialName("Microsoft.AnalysisServices") | out-null
$assembly = New-Object "Microsoft.AnalysisServices.Server" 
#Trap is since the connect method will throw error if no server is found.
Trap{$null;continue}
#Try connecting to server 
$assembly.Connect(".\SQL2008R2")
if($assembly.Version -eq $null){
Write-Host -f Red "Not connected"
}else{
Write-Host -f Yellow "Connected!"
}

Checking SQL Server (SQL)
# Load assembly
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | out-null
$s = New-Object "Microsoft.SqlServer.Management.Smo.Server" "SQL2008R2"
if($s.Version -eq $Null){
Write-Host -f Red "SQL not connected!"
}else{
Write-Host -f Yellow "SQL Server connected!"
}

Happy Powershelling!!
Ravikumar Vishwakarma.



No comments:

Post a Comment