Solved

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

Posted on 2011-09-13
4
243 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
  • 2
  • 2
4 Comments
 
LVL 68

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 7

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 68

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 7

Author Closing Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
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 walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
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…

706 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