Solved

Find running service on multiple machines running powershell

Posted on 2013-10-27
12
1,870 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 69

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 69

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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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 69

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 69

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 69

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 69

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
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 configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…

830 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