Find running service on multiple machines running powershell

Hi guys, I'm looking for a running service on some of our machines which is causing a bandwidth issue. if possible id like to have a Powershell script that searches for a part of a service name such as express* and run it against multiple machines. or if anybody can suggest an alternative method

thanks
LVL 15
cwstad2Asked:
Who is Participating?
 
QlemoConnect With a Mentor DeveloperCommented:
There was a closing parens left over in the first line of the second code snippet. I've edited that, so please try again. But you should get the same error as with the first code snippet - it is the same approach for the remote PC.

The error might result from the PC not being reachable at all. Did you get a result by the other PC's despite that message?
0
 
QlemoDeveloperCommented:
Easy with PowerShell, if you are in a domain - you should think, at least. But because of an error with the PS 2 Get-Services, you can't apply it against multiple PCs; Only the first will be queried. So, this does not work:
Get-Service -Name express* -Computer PC1, PC2, PC3 |
  ? { $_.Status -eq "Running" } |
  format-table -auto MachineName, ServiceName, DisplayName, Status

Open in new window

and you have to fall back to manual loop:
foreach ($pc in PC1, PC2, PC3) {
  Get-Service -Name express* -Computer $pc |
     ? { $_.Status -eq "Running" }
} |   format-table -auto MachineName, ServiceName, DisplayName, Status

Open in new window

The first code snippet should work on PS 3.
0
 
cwstad2Author Commented:
Thanks, is it possible to do run the script against a few letters of the PC. example PC* if not im guessing they will have to be imported from a text file
0
Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

 
QlemoDeveloperCommented:
No, you can't use wildcards for above, you have to enumerate the machines. Whether the list is stored in a text file, or retrieved using AD cmdlets (W2008R2 or higher domain controller), doesn't matter for the code above - just plug the command to get the list into the foreach, e.g. with
foreach ($pc in get-content c:\temp\Servers.txt) {
  Get-Service -Name express* -Computer $pc |
     ? { $_.Status -eq "Running" }
} |   format-table -auto MachineName, ServiceName, DisplayName, Status

Open in new window

0
 
cwstad2Author Commented:
that works great thanks, only thing is i get the following so i dont know what PC its referring to. i have changed the service to dhcp for a test . many thanks

Running  Dhcp               DHCP Client                          
Running  Dhcp               DHCP Client                          
Running  Dhcp               DHCP Client
0
 
QlemoDeveloperCommented:
That output is no result of my script. For proper output you need to select MachineName explicitly, else the format-* cmdlets will just ignore that property (as many others). Hence the final line, which should give you a result like:
PC1    DHCP    DHCP Client     Running
PC2    DHCP    DHCP Client     Running
PC3    DHCP    DHCP Client     Running

Open in new window

0
 
cwstad2Author Commented:
Sorry im not following. when i ran the original script it got (see below) so i removed the pipe.

} |   format-table -auto MachineName, ServiceName, DisplayName, Status
An empty pipe element is not allowed.
At line:4 char:3
0
 
QlemoDeveloperCommented:
Stupid me. I'm so used to foreach-object that I forget the foreach statement does not provide anything to the pipeline (unless it is used as an expression - complex topic, so I will skip that). So use either of the following snippets:
$(foreach ($pc in get-content c:\temp\Servers.txt) {
  Get-Service -Name express* -Computer $pc |
     ? { $_.Status -eq "Running" }
}) |   format-table -auto MachineName, ServiceName, DisplayName, Status

Open in new window

get-content c:\temp\Servers.txt | % {
    Get-Service -Name express* -Computer $_
  } |
  ? { $_.Status -eq "Running" }  |
  format-table -auto MachineName, ServiceName, DisplayName, Status

Open in new window

0
 
cwstad2Author Commented:
almost there i think i run the top one and it says
Get-Service : Cannot open Service Control Manager on computer 'PC1'. This operation might require other privileges

although im running ise with domain admin. the second script i get the following. thanks for your patience.

At line:1 char:52
+ get-content c:\Users\james\Desktop\Computer.txt) | % {
+                                                    ~
Unexpected token ')' in expression or statement.
At line:1 char:54
+ get-content c:\Users\jamesb\Desktop\Computer.txt) | % {
+                                                      ~
An empty pipe element is not allowed.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken
0
 
cwstad2Author Commented:
Thanks Qlemo, you are the poweshell don. Is there a way to supress any errors in the output?
0
 
QlemoDeveloperCommented:
You can always use the (common) parameter -ErrorAction SilentlyContinue (you can abbreviate the parameter name as -EA) for any cmdlet. Here you would apply it to Get-Service, of course, to suppress any error generated by that cmdlet.
0
 
cwstad2Author Commented:
Thank you very much wish i could award you more points.
0
All Courses

From novice to tech pro — start learning today.