Monitoring Services - Help with conditonal logic


hi all,

compiled posh script gathering ideas other scripts , putting together. goal monitor automatic services on 2008r2 servers exceptions , if service down send email identified services down on particular machine.

the group of servers want monitor, have several services exceptioned. in case biztalk , sql services.

this script;

cls $serverlist = get-content "d:\sysappz\servicemonitorchecks\servers.txt" $report = "report.htm" clear-content $report add-content $report "<html>" add-content $report "<head>" add-content $report "<meta http-equiv='content-type' content='text/html; charset=iso-8859-1'>" add-content $report '<title>multiserver service monitor report</title>' add-content $report '<style type="text/css">' add-content $report  "<!--" add-content $report  "td {" add-content $report  "font-family: tahoma;" add-content $report  "font-size: 11px;" add-content $report  "border-top: 1px solid #999999;" add-content $report  "border-right: 1px solid #999999;" add-content $report  "border-bottom: 1px solid #999999;" add-content $report  "border-left: 1px solid #999999;" add-content $report  "padding-top: 0px;" add-content $report  "padding-right: 0px;" add-content $report  "padding-bottom: 0px;" add-content $report  "padding-left: 0px;" add-content $report  "}" add-content $report  "body {" add-content $report  "margin-left: 5px;" add-content $report  "margin-top: 5px;" add-content $report  "margin-right: 0px;" add-content $report  "margin-bottom: 10px;" add-content $report  "" add-content $report  "table {" add-content $report  "border: thin solid #000000;" add-content $report  "}" add-content $report  "-->" add-content $report  "</style>" add-content $report "</head>" add-content $report "<body>" add-content $report  "<table width='100%'>" add-content $report  "<tr bgcolor='#cccccc'>" add-content $report  "<td colspan='7' height='25' align='center'>" add-content $report  "<font face='tahoma' color='#003399' size='4'><strong>multiserver service monitor report</strong></font>" add-content $report  "</td>" add-content $report  "</tr>" add-content $report  "</table>"  add-content $report  "<table width='100%'>" add-content $report "<tr bgcolor=#cccccc>" add-content $report  "<td width='10%' align='center'>server name</td>" add-content $report "<td width='50%' align='center'>service name</td>" add-content $report  "<td width='10%' align='center'>status</td>" add-content $report "</tr>" $servicedowncount = 0 foreach ($machinename in $serverlist) { $servicestatus = get-wmiobject win32_service -computername $machinename | { { ($_.startmode -eq "auto") -and ($_.state -ne "running") } { ($_.name -ne "sppsvc") -or ($_.name -ne "shellhwdetection") -or ($_.name -match "entsso") -or ($_.name -notmatch "btssvc*") -or ($_.name -notmatch "msolap*") -or ($_.name -notmatch "mssql*") -or ($_.name -notmatch "reportserver*") -or ($_.name -notmatch "sqlagent*") -or ($_.name -notmatch "clr_optimization*") -or ($_.name -eq "msdtsserver100") -or ($_.name -eq "ruleengineupdateservice") } } if ($servicestatus -ne $null) { foreach ($service in $servicestatus) { write-host $machinename `t $service.name `t $service.state -foregroundcolor red $svcname = $service.name $svcstate = $service.state add-content $report "<tr>" add-content $report "<td>$machinename</td>" add-content $report "<td>$svcname</td>" add-content $report "<td bgcolor='#ff0000' align=center>$svcstate</td>" add-content $report "</tr>" $servicedowncount += 1 } } else { write-output "all automatic services on $machinename running." } } add-content $report  "</table>" add-content $report "</body>" add-content $report "</html>" write-output $servicedowncount if ($servicedowncount -ge 1) { $smtphost = "mailrelay" $from = "sysinfo@company.com" $to = "systemsteam@company.com" $subject = "service monitor report" $body = get-content $report $smtp= new-object system.net.mail.smtpclient $smtphost $msg = new-object system.net.mail.mailmessage $from, $to, $subject, $body $msg.isbodyhtml = $true $smtp.send($msg) }

i tested on win2008r2 machine without exceptions , works ok.

if use 1 kind of exception, ## -notmatch "bts*" ##, works ok.

as put in these exceptions not.

need here...and when working can come here , copy themselves.

thanks !!!


team group in members work achieve common goal.


do in wmi, pull less data way, fill in rest of services
and should good
 gwmi win32_service -computername $machinename -filter "(startmode='auto')
and (state='running') , (not name 'btssvc%')"
 
 

justin rich
http://jrich523.wordpress.com
powershell v3 guide (technet)
please remember mark replies answers if , unmark them if provide no help.


Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Error: 0x80073701 when trying to add Print Services Role in Windows 2012 Standard

Disconnecting from a Windows Server 2012 R2 file sharing session on a Windows 7,8,10 machine

Event ID 64,77,1008 Certificates Events Windows Server 2008, 2008R2