Reputation: 11
I need to check service status of remote computer
If it satisfies all the conditions then service has to be started
If service is configured with service account then it has to be started with the respective service account only.
If service is related to App but not OS, then send email to server owner and application team to start service.
Upvotes: 0
Views: 1589
Reputation: 79
Okay, let's start to break this down. First of all, you want to check the services of another computer. For this task, there are multiple ways to do it. One could be the Invoke-Command. You could also first connect to the computer and then execute the commands. Herefore you can use the Enter-PSSession.
Let's go to the other parts. You want to check if a service is running. You can use the Get-Service command for this. The services you get back have different properties. One you should look for is the property "status". As you may see if you just use the command Get-Service you get every Service on your (or remote) machine. You can filter the results by using the | (Data pipe) and the Where-Object command. You want to filter for all services which aren't running anymore. This could look like this:
Get-Service | Where-Object {$_.status -eq "stopped"}
Now we have all Services which are not running. We need to extend our filter more to get the services that are from the user "LocalSystem".
Get-Service | Where-Object {$_.status -eq "stopped" -and $_.UserName -eq "LocalSystem"}
Last but not least we just want services which are from the OS. The property "ServiceType" determines whether the service is "Win32OwnProcess" or "Win32ShareProcess". So we filter for "Win32OwnProcess".
Get-Service | Where-Object {$_.status -eq "stopped" -and $_.UserName -eq "LocalSystem" -and $_.ServiceType -eq "Win32OwnProcess"}
Sadly I didn't really understand what you need the dependent services for, but you can have a look at them and extend this oneliner to your own needs.
Get-Service | Where-Object {$_.status -eq "stopped" -and $_.UserName -eq "LocalSystem" -and $_.ServiceType -eq "Win32OwnProcess"} | ft -Property Name, DependentServices
I think that's a good start for you to finish the rest of it by yourself. You may need IF Statements, and the Send-MailMessage if you want to send an e-mail to the server owner. For restarting a service you can use the Start-Service command.
You can use the data pipe for this again. First filter for all the services you are looking for as I did earlier this post and then use the data pipe and the Start-Service command. This could like this:
Get-Service | Where-Object {$_.status -eq "stopped" -and $_.StartType -eq "automatic"} | Start-Service
This would restart all services which aren't running and have as start type "automatic".
Upvotes: 2