0
$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?

Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
v_b
  • 175
  • 1
  • 4
  • 10

1 Answers1

2

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
}
Community
  • 1
  • 1
poiu2000
  • 960
  • 2
  • 14
  • 30
  • actually i was asking about different services on different servers eg. server1 -- service1 server2 -- service2 how can it be done???? – v_b Nov 02 '13 at 10:14