PowerShell- Monitoring Multiple Services On Multiple Servers Using WMI Class -Win32_Service

The requirement is to check only those services where startup mode set to Auto and services that stopped. In my previous post have used Get-Service cmdlet which do not bind any such information hence I’m querying Win32_Service. This class has StartMode and State attributes.

Function Get-ServiceStatusReport  
$script:list = $ComputerList   
$ServiceFileName= "c:\ServiceFileName.htm"  
New-Item -ItemType file $ServiceFilename -Force  
# Function to write the HTML Header to the file  
Function writeHtmlHeader  
$date = ( get-date ).ToString('yyyy/MM/dd')  
Add-Content $fileName "<html>"  
Add-Content $fileName "<head>"  
Add-Content $fileName "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"  
Add-Content $fileName '<title>Service Status Report </title>'  
add-content $fileName '<STYLE TYPE="text/css">'  
add-content $fileName  "<!--"  
add-content $fileName  "td {"  
add-content $fileName  "font-family: Tahoma;"  
add-content $fileName  "font-size: 11px;"  
add-content $fileName  "border-top: 1px solid #999999;"  
add-content $fileName  "border-right: 1px solid #999999;"  
add-content $fileName  "border-bottom: 1px solid #999999;"  
add-content $fileName  "border-left: 1px solid #999999;"  
add-content $fileName  "padding-top: 0px;"  
add-content $fileName  "padding-right: 0px;"  
add-content $fileName  "padding-bottom: 0px;"  
add-content $fileName  "padding-left: 0px;"  
add-content $fileName  "}"  
add-content $fileName  "body {"  
add-content $fileName  "margin-left: 5px;"  
add-content $fileName  "margin-top: 5px;"  
add-content $fileName  "margin-right: 0px;"  
add-content $fileName  "margin-bottom: 10px;"  
add-content $fileName  ""  
add-content $fileName  "table {"  
add-content $fileName  "border: thin solid #000000;"  
add-content $fileName  "}"  
add-content $fileName  "-->"  
add-content $fileName  "</style>"  
Add-Content $fileName "</head>"  
Add-Content $fileName "<body>"  
add-content $fileName  "<table width='100%'>"  
add-content $fileName  "<tr bgcolor='#CCCCCC'>"  
add-content $fileName  "<td colspan='4' height='25' align='center'>"  
add-content $fileName  "<font face='tahoma' color='#003399' size='4'><strong>Service Stauts Report - $date</strong></font>"  
add-content $fileName  "</td>"  
add-content $fileName  "</tr>"  
add-content $fileName  "</table>"  
# Function to write the HTML Header to the file  
Function writeTableHeader  
Add-Content $fileName "<tr bgcolor=#CCCCCC>"  
Add-Content $fileName "<td width='10%' align='center'>ServerName</td>"  
Add-Content $fileName "<td width='50%' align='center'>Service Name</td>"  
Add-Content $fileName "<td width='10%' align='center'>status</td>"  
Add-Content $fileName "</tr>"  
Function writeHtmlFooter  
Add-Content $fileName "</body>"  
Add-Content $fileName "</html>"  
Function writeDiskInfo  
if( $status -eq "Stopped")  
 Add-Content $fileName "<tr>"  
 Add-Content $fileName "<td bgcolor='#FF0000' align=left ><b>$servername</td>"  
 Add-Content $fileName "<td bgcolor='#FF0000' align=left ><b>$name</td>"  
 Add-Content $fileName "<td bgcolor='#FF0000' align=left ><b>$Status</td>"  
 Add-Content $fileName "</tr>"  
Add-Content $fileName "<tr>"  
 Add-Content $fileName "<td >$servername</td>"  
 Add-Content $fileName "<td >$name</td>"  
 Add-Content $fileName "<td >$Status</td>"  
Add-Content $fileName "</tr>"  
writeHtmlHeader $ServiceFileName  
 Add-Content $ServiceFileName "<table width='100%'><tbody>"  
 Add-Content $ServiceFileName "<tr bgcolor='#CCCCCC'>"  
 Add-Content $ServiceFileName "<td width='100%' align='center' colSpan=3><font face='tahoma' color='#003399' size='2'><strong> Service Details</strong></font></td>"  
 Add-Content $ServiceFileName "</tr>"  
 writeTableHeader $ServiceFileName  
#Change value of the following parameter as needed  
#List of programs to exclude  
#$InlcudeArray = $inlcudeService  
Foreach($ServerName in (Get-Content $script:list))  
$service = Get-WmiObject Win32_Service -ComputerName $servername |Where-Object { $_.StartMode -eq 'Auto' -and $_.state -eq 'Stopped'} 
if ($Service -ne $NULL)  
foreach ($item in $service)  
 Foreach($include in $includeService)   
 write-host $inlcude                                      
 if(($item.name).Contains($include) -eq $TRUE)  
    Write-Host  $servername $item.name $item.Status   
    writeDiskInfo $ServiceFileName $servername $item.name $item.Status   
Add-Content $ServiceFileName "</table>"   
writeHtmlFooter $ServiceFileName  
function Validate-IsEmail ([string]$Email)  
                return $Email -match "^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"  
Function sendEmail    
$body = Get-Content $htmlFileName   
$body = New-Object System.Net.Mail.MailMessage $from, $receipients, $subject, $body   
$body.isBodyhtml = $true  
$smtpServer = $MailServer  
$smtp = new-object Net.Mail.SmtpClient($smtphost)  
$validfrom= Validate-IsEmail $from  
if($validfrom -eq $TRUE)  
$validTo= Validate-IsEmail $to  
if($validTo -eq $TRUE)  
write-output "Email Sent!!"  
write-output "Invalid entries, Try again!!"  
$date = ( get-date ).ToString('yyyy/MM/dd')  
sendEmail -from $From -to $to -subject "Service Status - $Date" -smtphost $SMTPMail -htmlfilename $ServiceFilename  

The Function Get-ServiceStatusReport contains five parameters

    1. ComputerList – List of Servers


  • ServiceName – Name of Services separated by comma



  • SMTPMail – SMTP mail address



  • FromID – Valid Email ID



  • ToID – Valid Email ID



Function call –

Get-ServiceStatusReport -ComputerList C:\server.txt -includeService  "MySQL","MpsSvc","W32Time" -To pjayaram@app.com -From pjayaram@ app.com -SMTPMail  app01. app.com


Get-ServiceStatusReport -ComputerList C:\server.txt -includeService  MySQL,MpsSvc,W32Time -To pjayaram@app.com -From pjayaram@ app.com -SMTPMail  app01. app.com



About Prashanth Jayaram

DB Technologist, Author, Blogger, Service Delivery Manager at CTS, Automation Expert, Technet WIKI Ninja, MVB and Powershell Geek My Profile: https://social.technet.microsoft.com/profile/prashanth jayaram/ http://www.sqlshack.com/author/prashanth/ http://codingsight.com/author/prashanthjayaram/ https://www.red-gate.com/simple-talk/author/prashanthjayaram/ http://www.sqlservercentral.com/blogs/powersql-by-prashanth-jayaram/ Connect Me: Twitter @prashantjayaram GMAIL powershellsql@gmail.com The articles are published in: http://www.ssas-info.com/analysis-services-articles/ http://db-pub.com/ http://www.sswug.org/sswugresearch/community/
This entry was posted in PowerShell and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s