Solved

Find running service on multiple machines running powershell

Posted on 2013-10-27
12
2,252 Views
Last Modified: 2013-10-27
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
0
Comment
Question by:cwstad2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 70

Expert Comment

by:Qlemo
ID: 39603802
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
 
LVL 15

Author Comment

by:cwstad2
ID: 39603830
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
 
LVL 70

Expert Comment

by:Qlemo
ID: 39603859
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
IoT Devices - Fast, Cheap or Secure…Pick Two

The IoT market is growing at a rapid pace and manufacturers are under pressure to quickly provide new products. Can you be sure that your devices do what they're supposed to do, while still being secure?

 
LVL 15

Author Comment

by:cwstad2
ID: 39603897
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
 
LVL 70

Expert Comment

by:Qlemo
ID: 39604003
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
 
LVL 15

Author Comment

by:cwstad2
ID: 39604023
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
 
LVL 70

Expert Comment

by:Qlemo
ID: 39604033
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
 
LVL 15

Author Comment

by:cwstad2
ID: 39604064
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
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39604098
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
 
LVL 15

Author Comment

by:cwstad2
ID: 39604184
Thanks Qlemo, you are the poweshell don. Is there a way to supress any errors in the output?
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39604194
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
 
LVL 15

Author Comment

by:cwstad2
ID: 39604223
Thank you very much wish i could award you more points.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…
Suggested Courses

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question