Monday, March 2, 2015

How to Check Whether Service Application is Running using PowerShell

For one of my requirements, I wanted to Start/Stop the Search Service Application through PowerShell.
Before executing Start/Stop operations, I wanted to check if Search Service Application is running or not. Based on the state i would start or stop the service accordingly.

I have used the below PowerShell script to check whether a Search Service Application is running or not. Then Start/Stop the service accordingly.
$ServiceName = "state service"
$serviceApp = Get-SPStateServiceApplication -Identity $ServiceName
Write-Host -ForegroundColor DarkGray "Checking if "  $ServiceName  " instance is Online"
if($serviceApp.Status -ne "Online")
{
   Write-Host -ForegroundColor Yellow "Starting " $ServiceName " instance"
   Start-SPStateServiceApplication -Identity $stateServiceName
   While ($serviceApp.Status -ne "Online")
   {
       Start-Sleep -s 5
   }
   Write-Host -ForegroundColor Yellow $ServiceName " instance is started"
}
else
{
   Write-Host -ForegroundColor Green $ServiceName " instance is already running"
   Stop-SPStateServiceApplication -Identity $ServiceName
}
The same script can be used to check if any of the service applications is running by providing the service application name against the variable $ServiceName. The scripts executes against the provided service application and checks if it is running or not.

No comments:

Post a Comment