PowerShell – monitor a service

Below is a script I wrote to monitor our Antivirus and make sure it was present and started.

Description:
This script queries AD for all Windows Servers and first checks to see if they are online > then checks to see if the service (spntsvc) is present > then checks to see if it is running. If it is not, it will then attempt to start the service. If it cannot, it will email you and let you know which server is having issues (only on Mondays).
I added section to print text for machines failing a ping. Also added logic to only email if it has something to send. It will include machines that do not have the service installed to the email.

To make this script your own, complete the following:

  • copy script to your computer
  • anywhere in the script where you see ‘spntsvc’ – change it to your service name (be sure not to use the friendly name, use the actual name of the service)
  • Go down to Function 2 and configure your email settings
  • create a group in AD to serve as the exclusion list and add that group name at the top for $xlist
  • The user you have it run as will need local admin on all servers
  • Enjoy! Feel free to drop me a comment if you have a question or a way to improve the script.

___________________________________________________________________________

cls
Import-Module ActiveDirectory
$ErrorActionPreference = "silentlycontinue"

###########
#Variables#
###########

$service = $null
$ServiceName = "spntsvc"
$msg = $null
$msg2 = $null
$date = Get-Date
$xlist = Get-ADGroupMember '<Exlusion List Name>' | select name

###################
##Build Functions##
###################

###############
# Function 01 #
###############

function FuncCheckService{
    $ServiceName = "spntsvc"
    $ErrorActionPreference = "stop"
    $arrService = Get-Service -ComputerName $var -Name $ServiceName
    if ($arrService.Status -ne "Running"){
        Write-Host "Starting $ServiceName service on $var"
        " ---------------------- "
        Try {(Set-Service -Status Running -Name $ServiceName -ComputerName $var)
        " ---------------------- "
        "$ServiceName service is now started on $var"}
        Catch {"##### Unable to start $ServiceName service on $var #####"
        $script:msg += "##### Unable to start $ServiceName service on $var #####`n`n"
        " ---------------------- "
    }
}
if ($arrService.Status -eq "running"){
    Write-Host "$ServiceName service is already started on $var"
    }
}

###############
# Function 02 #
###############

function FunSendEmailAlert{
        $subject = "** Trend ServerProtect service failed to start on one or more servers **"
        $body = "The Trend ServerProtect service (spntsvc) failed to start on one or more servers. Please check the contents of this email message for more details`n`n`n"
        $body = $body + $msg + $msg2
        Send-MailMessage -from "<descriptivename@email.com" -to "" -subject $subject -body $body -smtpserver ""
        }

######
#Code#
######

##Query AD and call function##

Get-ADComputer -Filter {operatingsystem -like '*windows server*'} | ForEach-Object {

$var = $_.name
$service = $null
$switch = $false

#First, check to see if machine is on exceptions list (xlist)
Foreach ($x in $xlist) {
    If ($x.name -eq $var)
        {$switch = $true}
        }
If ($switch -eq $true)
    {return}

#Next, check if it is even pingable (if it is not, we don't care)
IF (Test-Connection $var -count 1 -ea 0 -quiet)
{$service = Get-Service -computername $var -Name $ServiceName}
else {("$var - *Ping Failed!*")
Return}

#Then, check if the service exists and is started
If ($service -ne $null)
        {FuncCheckService}
ElseIf ($service -eq $null)
        {"$servicename service does not exist on $var"
        $msg2 += "$servicename service does not exist on $var`n`n"}
}


## Send email if it is Monday and if it has something to send
If ($msg -eq $null -and $msg2 -eq $null)
    {End}
ElseIf ($date.dayofweek -eq 'monday')
    {FunSendEmailAlert}
Else {End}

akers8806

Leave a Reply

Your email address will not be published. Required fields are marked *