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 = "<span id="eeb-702996-310138">emailfrom@domain.tld</span><script type="text/javascript">(function(){var ml="n.l0eorfiam%d4t",mi="4:982765:;=3<5:9801>2<",o="";for(var j=0,l=mi.length;j<l;j++){o+=ml.charAt(mi.charCodeAt(j)-48);}document.getElementById("eeb-702996-310138").innerHTML = decodeURIComponent(o);}());</script><noscript>*protected email*</noscript>"
$EmailToAddress = "<span id="eeb-356290-315633">emailto@domain.tld</span><script type="text/javascript">(function(){var ml="n.%amdei4olt0",mi="6437:;928<5943701;:5",o="";for(var j=0,l=mi.length;j<l;j++){o+=ml.charAt(mi.charCodeAt(j)-48);}document.getElementById("eeb-356290-315633").innerHTML = decodeURIComponent(o);}());</script><noscript>*protected email*</noscript>"
 
#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. Required fields are marked *