Pages

30 May 2013

Powershell script to check for stopped "Automatically started" services for multiple servers

The powershell script below checks for services that are automatically started when the system starts.
If a service is found stopped, the scripts starts the service and sends a notification email to an admin or a group.

Multiple servers are supported, and services that stop automatically because they have no need to run al the time (software protection for instance) are filtered out.

Modify the following to adjust for your environment:
  • The service name you want to exclude
  • The server array server names
  • The email receipients
  • The smtp server

The script:

#####################################################################
# Author:  Edwin van Brenk                                                                                             #
# date:  23-05-2013                                                                                                         #
# Purpose: Check if auto start services for a number of                                                 #
#                 servers are running, if not send a notification                                              #
#                 email and start the service                                                                           #
#####################################################################

$Computers = "server1","server2","server3","server4"
foreach ($Computer in $Computers)
 {
  #$exclusion = "clr_optimization"
  Write-Host "checking server: $computer"
  #Create an array of all services running
  $GetService = Get-WmiObject win32_service -ComputerName $Computer | where {($_.startmode -eq "Auto") -and ($_.state -ne "Running")}
  foreach ($service in $GetService)
   {
    if ($service.name -match "clr_optimization*")
     {
      Write-Host "`tService exclusion: " $service.name
     }
    elseif ($service.name -match "MMCSS")
     {
      Write-Host "`tService exclusion: " $service.name
     }
    elseif ($service.name -match "sppsvc")
     {
      Write-Host "`tService exclusion: " $Computer $service.name
     }
     elseif ($service.name -match "BMR Boot Service")
     {
      Write-Host "`tService exclusion: " $Computer $service.name
     }
     elseif ($service.name -match "NetBackup SAN Client Fibre Transport Service")
     {
      Write-Host "`tService exclusion: " $Computer $service.name
     }
     elseif ($service.name -match "SysmonLog")
     {
      Write-Host "`tService exclusion: " $Computer $service.name
     }
     elseif ($service.name -match "Mapsvc")
     {
      Write-Host "`tService exclusion: " $Computer $service.name
     }
     elseif ($service.name -match "NetBackup Vault Manager")
     {
      Write-Host "`tService exclusion: " $Computer $service.name
     }
     elseif ($service.name -match "NetBackup SAN Client Fibre Transport Service")
     {
      Write-Host "`tService exclusion: " $Computer $service.name
     }
     elseif ($service.name -match "NetBackup Key Management Service")
     {
      Write-Host "`tService exclusion: " $Computer $service.name
     }
     elseif ($service.name -match "RtcSrv")
     {
      Write-Host "`tService exclusion: " $Computer $service.name
     }
     elseif ($service.name -match "ShellHWDetection")
     {
      Write-Host "`tService exclusion: " $Computer $service.name
     }
    else
     {
      $servicename = $service.name
      Send-Mailmessage -to "
admin1@domain.com","admin2@domain.com" -Subject "$servicename is stopped on $Computer" -from ServicesCheck@domain.com -Body "The $servicename service was found stopped on $computer. Check if the script started the service" -SmtpServer smtp.domain.lan
      Write-Host "`tStarting service " $service.name ".." -NoNewline
      $service.StartService()
      Write-Host "Done!" -BackgroundColor Green
     }
   }
 }

No comments:

Post a Comment