Use Powershell To Get-service Status On Remote Computer

Powershell offers a few cmdlets targeted to managing a remote server and setting the service state as well as the startup. We will run through a few examples of getting the service information and than setting their state as well as the startup option. These scripts requires Administrator access to the remote server.

Get-Service and State

The Get-Service cmdlet is pretty straight forward, you enter the computer name and the name of the service you would like to see displayed. By using this basic command we get a list of all of the services regardless of their status or startup.

 Get-service -ComputerName VMServer01 

As you can see this is simple and useful for a single machine if you want to browse the running services.
What if we want to see only the SQL services or just one service by name?

Get-service -ComputerName VMServer01 -Name *SQL*
Get-service -ComputerName VMServer01 -Name SQLBrowser
Get-service -ComputerName VMServer01 -Name 'MSSQL$VMSQL01'

In this case the asterisk is useful to find multiple services where you are not sure of the exact name. Then when you have it you can complete the name and retrieve only that services details. One last option you should be aware of is certain services have dependencies such as the SQL Agent which needs SQL Service to be running for example.
We can find the service dependencies easily:

Get-service -ComputerName VMServer01 -Name 'SQLAgent$VMSQL01' -RequiredServices
Get-service -ComputerName VMServer01 -Name 'MSSQL$VMSQL01' -RequiredServices
Get-service -ComputerName VMServer01 -Name 'MSSQL$VMSQL01' -DependentServices

You will notice here the SQL Agent has a requirement for the SQL Service but MSSQL does not. The DependantServices option goes the other way and shows what services depend on the specified. Also note that I had to use the single quotes because of the dollar sign ($) identifying the SQL instance. If you are working with a default instance than the name will just be MSSQLSERVER.

More Details from Get-Service

There is more that we can collect from the objects returned by this cmdlet. I like to assign the results to a variable and work with it using the dot notation but we can also just add parenthesis. Try the following:

( Get-service -ComputerName VMServer01 -Name  'MSSQL$VMSQL01' ).MachineName
( Get-service -ComputerName VMServer01 -Name  'MSSQL$VMSQL01' ).DependentServices
( Get-service -ComputerName VMServer01 -Name  'MSSQL$VMSQL01' ).Status
( Get-service -ComputerName VMServer01 -Name  'MSSQL$VMSQL01' ).StartType
( Get-service -ComputerName VMServer01 -Name  'MSSQL$VMSQL01' ).DisplayName

Multiple Services Status and StartupType

In order to deal with multiple services and/or instances we can assign the results to a variable and work with that instead of all this repeating code. Lets assign the results of our “anything” name SQL request into a variable called “$sqlservices”

$sqlservices = Get-service -ComputerName VMServer01 -Name  '*SQL*'

Now we can get the same info by using the dot notation, after executing the above, try this:

$sqlservices.Name
$sqlservices.DependentServices
$sqlservices.Status
$sqlservices.StartType
$sqlservices.DisplayName

The same detail is now stored in the object but it doesn’t seem very useful yet. Using ‘Name” just returns the Name and no details. How can we use this?

The Set-Service Cmdlet

Set-Service is also a simple command that does pretty much what it says. It can set the parameters for a service like the startuptype or just stop and restart services. In conjunction with the Get-Service cmdlet we grab services from a remote server and use this cmdlet to manage its parameters or current state. The only thing we need is the Name of the service for this cmdlet to work.

Using Get-Service try using the following examples to stop, start and set the startup type.

Set-service -ComputerName VMServer01 -Name 'MSSQL$VMSQL01' -Status Stopped
Set-service -ComputerName VMServer01 -Name SQLBrowser -StartupType Disabled
Set-service -ComputerName VMServer01 -Name 'MSSQL$VMSQL01' -Status Running  -StartupType Manual

Stop and Start using Set-Service

Now that we understand the basics for Set-Service the next step in our process is to control multiple services. We will use a foreach loop to select each service by name and set the appropriate values. For this SQL service example we can imagine that we are performing some maintenance or patching and all of the SQL services need to be stopped and disabled during a few reboots on a certain date.

We can use a foreach loop to stop and set all of the services startup to manual or disabled before the maintenance window. Imagine scheduling this to run automatically instead of working at 1am.
To start lets make sure we are getting all of the services by name in our foreach:

## Get anything with SQL in its name
$sqlservices = Get-service -ComputerName VMServer01 -Name *SQL* 

Foreach ($service in $sqlservices) {
    $service.Name
}

The above code will display the same services with SQL in their names. This is creating a new variable called $service and each object currently in $sqlservices is being assigned one at a time so we can work with it inside the curly brackets {}.

Now lets add the Set-Service cmdlet to stop and disable each SQL service for us.

$sqlservices = Get-service -ComputerName VMServer01 -Name *SQL* 

Foreach ($service in $sqlservices) {
    Set-service -ComputerName VMServer01 -Name $service.Name -StartupType Disabled
    Set-Service -ComputerName VMServer01 -Name $service.Name -Status Stopped
}
## Visually check that the services are stopped and disabled
 Get-service -ComputerName VMServer01 -Name *SQL*

We added another Get-Service at the bottom so we can see that the status and startup has been set properly. If your familiar with sending email via powershell you could send yourself this as a report.

After patching is completed we can simply reverse the command and start all of the SQL services up again.

$sqlservices = Get-service -ComputerName VMServer01 -Name *SQL* 

Foreach ($service in $sqlservices) {
    Set-service -ComputerName VMServer01 -Name $service.Name -StartupType Automatic
    Set-Service -ComputerName VMServer01 -Name $service.Name -Status Running
}
## Visually check that the services are stopped and disabled
 Get-service -ComputerName VMServer01 -Name *SQL*

Managing Remote Computers

There is much more to explore as you understand working with the foreach and variables we can create a much more resilient script. We could use a list of servers and perform these actions on each one using the same foreach logic. Think about adding features such as:

  • Try / Catch for error checking: in case the service fails to stop or start and you want the error code
  • Email communication: send an email when completed with the last status of the services
  • Foreach “Server”: then stop and disable services
  • Use variables: for the service Name, Status or StartType and set them at the top of the script to be flexible

Feel free to be creative and good luck!