?
Solved

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

Posted on 2011-09-13
4
Medium Priority
?
287 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 71

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 71

Accepted Solution

by:
Qlemo earned 2000 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

Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

Question has a verified solution.

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

This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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 from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

649 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