v_b
v_b

Reputation: 225

How to check different services on different servers in powershell scripting?

$c = Get-Content D:\Users\vbaranwa\Desktop\vb.txt
for ($i = 0; $i-le $c.count; $i++) {
  $a = ($c)[$i]
  Get-Service -ComputerName $a -Name Power | select Name, MachineName, Status 
}

This is working if the services are same but if there are different services to check then how it can be done?

Upvotes: 0

Views: 2893

Answers (1)

poiu2000
poiu2000

Reputation: 980

For the Name parameter, you can specify multiple patterns and wildcards can be used in the patterns. You can use your get-service command as flollowing:

$c= Get-Content D:\Users\vbaranwa\Desktop\vb.txt
for($i=0; $i-le $c.count; $i++)
{
    $a=($c)[$i]
    get-service -ComputerName $a -Name *pattern1*, *pattern2*, *pattern3* |select Name, MachineName, Status
}

You can refer to the following link on how to use the parameter for this command: Get-Service Cmdlet

I did some search and found this SO link should be talking the same problem you encountered: How to check if a particular service is running in a remote computer using PowerShell

You can refer to the SO link to configure which server have which service in the code, or you can have a change in your vb.txt file (I assume now this file only contains the server names) to configure which server have which services like:

server1, service1
server2, service2
server3, service3
server3, service4

And then the code will be changed to:

$c= Import-CSV -header server, service D:\Users\vbaranwa\Desktop\vb.txt
for($i=0; $i-le $c.count; $i++)
{
    $server  = $c[$i].server
    $service = $c[$i].service
    get-service -ComputerName $server -Name $service |select Name, MachineName, Status
}

Upvotes: 2

Related Questions