Categories

Getting remote services

Getting the service on a remote server is easy

Get-Service -ComputerName Exch10

 

Getting a set of services on a remote machine isn’t difficult

Get-Service -ComputerName Exch10 -Name "MSExchangeAB", "W32Time", "W3SVC"

Ok so what about the scenario with multiple servers

Get-Service -ComputerName Exch10, server02

 

Multiple servers with a set of servers just means adding the –Name parameter and the list of services

Get-Service -ComputerName Exch10, server02 -Name "W32Time", "W3SVC"

 

but you have to have the same list of services for each server.

Now lets get really picky and go for multiple servers where each one has a different set of services.

 

I thought about csv files but then how do you repent the list?  Nested arrays – yuck.

I ended up with this approach. Its a bit messy but works and is easily expandable & changeable

 

$servers = "server02", "exch10"
$server02_services = "BITS", "NtFrs", "MSMQ",  "Kdc"
$exch10_services = "MSExchangeAB", "W32Time", "W3SVC"

foreach ($server in $servers){
Get-Service -ComputerName $server -Name (Get-Variable -Name ($server + "_services")).value |
select @{N="Server"; E={$server}}, Status, Name, DisplayName
}

 

Create a list of servers. Create a list of services per server. Notice the naming convention for the variables.

Iterate over the servers using foreach.  The server name comes from the foreach iteration variable.  The list of services is created by using get-variable. Substitute the server name to create the variable name and use the Value property to give you the services of interest. Use select to add the computer name to the output.

I don’t use the *Variable cmdlets very often and this was a neat use of get-variable

3 Responses to Getting remote services

Leave a Reply