Solved

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

Posted on 2011-09-13
4
267 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 69

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 69

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Last week, our Skyport webinar on “How to secure your Active Directory” (https://www.experts-exchange.com/videos/5810/Webinar-Is-Your-Active-Directory-as-Secure-as-You-Think.html?cid=Gene_Skyport) provided 218 attendees with a step-by-step guide for…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
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…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

680 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