Dark Developments Where Knowledge Meets Power

28Sep/110

PS – PowerShell Service Check

Because of some recent issues regarding automatic services not starting up after a reboot, I wrote a small PowerShell script that checks a service if it's running.

Select All Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function CheckService{ param($ServiceName)
    $srvServiceDetails = Get-Service -Name $ServiceName
 
    if ($srvServiceDetails.Status -ne "Running"){
        SendMail -ServiceDetails $srvServiceDetails
    }
}
 
function SendMail{ param($ServiceDetails)
    $smtpServer = "out.darkdevelopments.com"
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $msg = new-object Net.Mail.MailMessage
    $strComputer = gc env:computername
    $smtpSubject = "[SYSMON] SERVICE ALERT @ " + $strComputer
    $msg.From = "sysmon@darkdevelopments.com"
    $msg.To.Add("darkbasics@darkdevelopments.com")
    $msg.Subject = $smtpSubject
    $msg.Body = "Hostname: " + $strComputer + "`nDisplayName: " + $ServiceDetails.displayname + "`nName: " + $ServiceDetails.name + "`nStatus: " + $ServiceDetails.status
    $smtp.Send($msg)
}
 
CheckService -ServiceName "TheServiceNameHere"
Print Friendly
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.