Solved

Find running service on multiple machines running powershell

Posted on 2013-10-27
12
1,726 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
  • 6
  • 6
12 Comments
 
LVL 68

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 68

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
 
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 68

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 68

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 68

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 68

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
A brief introduction to what I consider to be the best editor for PowerShell.
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 the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now