Solved

Powershell script to query which server has a particular service and then restart that service ?

Posted on 2011-09-13
4
272 Views
Last Modified: 2012-08-14
Hi People, i'd like to know if anyone can help me in refining my powershell script below to query which server has a particular service and then restart that service ?

1. Get from List of Servers that is online (perform ping test one time)
2. if the server in the list above got Service with particular name restart it
3. if not then display the server name.

gc C:\Servers.txt | Where-Object { Test-Connection $_ -Count 1 -Quiet } |
Foreach-Object {
   Get-WmiObject -Class Win32_Service -ComputerName $_ -Filter "Name='MonitoringAgent'" | Select-Object SystemName,Name,State,Status,StartMode
  Trap {
        write-warning "Error Trapped for " $_
        Continue
    }
} | Sort State | ft -AutoSize

Open in new window


Any help would be greatly appreciated.

Thanks.
0
Comment
[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
  • 2
  • 2
4 Comments
 
LVL 70

Expert Comment

by:Qlemo
ID: 36534328
Can we use Remoting (requires PowerShell and WinRM [part of PowerShell] enabled on all remote machines)? Else we need to fall back to WMI to restart services. You can check for services without Remoting (get-service -ComputerName), but that's it, sadly.
0
 
LVL 8

Author Comment

by:Senior IT System Engineer
ID: 36534349
Good question, unfortunately it is not possible at the moment, that is why I'm using WMI for my script to restart the service like the following:

$names = gc C:\Servers.txt | Where-Object { Test-Connection $_ -Count 1 -Quiet }
foreach($name in $names) {
  $svc = Get-WmiObject Win32_Service -ComputerName $_ -Filter "Name='ServerAgent'"
	$svc | % { 
	  $_.StopService()
	  $_.StartService()
	}
}  

Open in new window

0
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 36534421

You almost got what you want already. The only part missing is the output of machines not having that particular service at all. You should get that part if you check $svc for $null.

Testing the connection first, before doing the service stuff, has one disadvantage - machines not responding delay processing. I would use piping as in your first code snippet, and combine that with the restart part of your second code snippet:
gc C:\Servers.txt | ? { Test-Connection $_ -Count 1 -Quiet } | % {
  $svc = Get-WmiObject Win32_Service -ComputerName $_ -Filter "Name='ServerAgent'"
  $svc | % { $_.StopService(); $_.StartService() }
  if ($svc -eq $null) { write-host "$_: ServerAgent service not found" }
}

Open in new window

0
 
LVL 8

Author Closing Comment

by:Senior IT System Engineer
ID: 36913764
Thanks !
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

734 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