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

  • Mike Shepard says:

    I would use a hashtable where the keys are the names of the servers and the values are lists of services for each server.

    Lots easier to look at and the code is much cleaner (IMHO). :-)

  • RichardSiddaway says:

    Hash tables can be used. I thought about hash tables but in the end decided not to use them.

    I though it was time that Get-Variable had a turn in the sun.

    Could you post your code because a comparison would be useful.

    I’ve also seen an option using a PSObject which was quite neat

  • Mike Shepard says:

    I didn’t see this last night or I would have. Your follow-up post covers it pretty much like I would have.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>