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?

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

actually i was asking about different services on different servers eg. server1 -- service1 server2 -- service2 how can it be done????

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.