PowerShell script to monitor service status.

PowerShell script to monitor a specified list of services, attempt to start them if they’re not running, and send you an email if the service won’t start.

Run from a Windows Scheduled Task as frequently as you want to check if a service is running.
Requires a text file with the servers you want to monitor listed, 1 server per line.

<#-------------Config-------------#>

#Set the directory to work from, containing the list of servers to check
$WorkingDirectory = "C:\Monitoring Utilities\SQLServerServices"
$ServerListFile = "Servers.txt"
$ServerListFullPath = $WorkingDirectory + "\" + $ServerListFile

#SMTP server details
$SMTPServer = "SMTP SERVER ADDRESS"
$EmailFromAddress = ""
$EmailToAddress = ""

#Comma separated list of services to check
$ServiceList = "MSSQLSERVER,SQLSERVERAGENT"

<#-------------Config-------------#>

#Read the server list into an array
$Servers = Get-Content $ServerListFullPath

#Split the services list up into an array
$Services = $ServiceList -split ','

#Loop through the services
ForEach($ServiceName In $Services)
    {
        #Loop through the servers
        ForEach($Server In $Servers)
            {
                $Service = Get-Service $ServiceName -ComputerName $Server
                #Uncomment to debug and see server name, service name and service status
                #"$Server : $ServiceName : $($Service.Status)"
                If($Service.Status -ne "Running")
                    {
                        #Start the service
                        
                        #Add current server to trusted hosts so we can remotely start the service
                        $Command = '@{TrustedHosts="'+$Server+'"}'
                        winrm s winrm/config/client $Command

                        #Set command to start the current service
                        $ServiceStartCommand = "net start $ServiceName"

                        #Try and start the service
                        Invoke-Command -ComputerName $Server -ScriptBlock {
                            Invoke-Expression -Command:$args[0]
                        } -ArgumentList $ServiceStartCommand
                        
                        #Wait 10s before checking if the service started
                        Start-Sleep -Seconds 10

                        #Check if service started
                        $Service = Get-Service $ServiceName -ComputerName $Server
                        If($Service.Status -ne "Running")
                            {
                                #Send email if service still isn't running
                                $EmailSubject = "$ServiceName Service on $Server isn't running. Tried to start it and was unsuccessful."
                                Send-MailMessage -To $EmailToAddress -From $EmailFromAddress -Subject $EmailSubject -SMTPServer $SMTPServer
                            }
                    }
            }
    }

Leave a Reply

Your email address will not be published.