Solved

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

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ADFS Redirection 4 31
Powershell script 13 60
Unable to add an AD Group to a an Computer Account as local system 2 24
Troubles Logging On Creating New Profile 3 44
OfficeMate Freezes on login or does not load after login credentials are input.
A procedure for exporting installed hotfix details of remote computers using powershell
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licenā€¦
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ā€¦

910 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

21 Experts available now in Live!

Get 1:1 Help Now