Solved

Find running service on multiple machines running powershell

Posted on 2013-10-27
12
1,640 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Free book by J.Peter Bruzzese, Microsoft MVP

Are you using Office 365? Trying to set up email signatures but you’re struggling with transport rules and connectors? Let renowned Microsoft MVP J.Peter Bruzzese show you how in this exclusive e-book on Office 365 email signatures. Better yet, it’s free!

 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you very much wish i could award you more points.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
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…

743 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

18 Experts available now in Live!

Get 1:1 Help Now