# First lets create a text file, where we will later save the Service Health info $ServiceHealthFileName = "ServiceHealth.htm" $serverlist = "yourserverlist.txt" $warning = "Stopped" New-Item -ItemType file $ServiceHealthFileName -Force # Function to write the HTML Header to the file Function writeHtmlHeader { param($fileName) $date = ( get-date ).ToString('yyyy/MM/dd') Add-Content $fileName "" Add-Content $fileName "" Add-Content $fileName "" Add-Content $fileName ' Server Health' add-content $fileName '" Add-Content $fileName "" Add-Content $fileName "" add-content $fileName "" add-content $fileName "" add-content $fileName "" add-content $fileName "" add-content $fileName "
" add-content $fileName " Server Health - $date" add-content $fileName "
" } # Function to write the HTML Header to the file Function writeTableHeader { param($fileName) Add-Content $fileName "" Add-Content $fileName "Name" Add-Content $fileName "ProcessId" Add-Content $fileName "State" Add-Content $fileName "StartMode" Add-Content $fileName "ExitCode" Add-Content $fileName "Status" Add-Content $fileName "" } Function writeHtmlFooter { param($fileName) Add-Content $fileName "" Add-Content $fileName "" } Function writeServiceInfo { param($fileName,$SvcName,$SvcPID,$SvcSM,$SvcExCd,$ServiceState,$SvcStatus) $SvcName= $Item.Name $SvcPID= $Item.ProcessId $SvcSM= $Item.StartMode $SvcExCd= $Item.ExitCode $ServiceState= $Item.State $SvcStatus= $Item.Status #You can add multiple elseif statements if you wish to display certain events. I have server state of STOPPED in Red. if ($ServiceState -eq $warning) { Add-Content $fileName "" Add-Content $fileName "$SvcName" Add-Content $fileName "$SvcPID" Add-Content $fileName "$ServiceState" #FF4C4C RED #FBB917 ORANGE Add-Content $fileName "$SvcSM" Add-Content $fileName "$SvcExCd" Add-Content $fileName "$SvcStatus" Add-Content $fileName "" } else { Add-Content $fileName "" Add-Content $fileName "$SvcName" Add-Content $fileName "$SvcPID" Add-Content $fileName "$ServiceState" Add-Content $fileName "$SvcSM" Add-Content $fileName "$SvcExCd" Add-Content $fileName "$SvcStatus" Add-Content $fileName "" } } #The function for the sendemail at the end. #Function sendEmail #{ param($from,$to,$subject,$smtphost,$htmlFileName) #$body = Get-Content $htmlFileName #$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) writeHtmlHeader $ServiceHealthFileName foreach ($server in Get-Content $serverlist) { #This adds the Uptime for the server you will need $Display in your add-content. $hostname = get-wmiobject win32_computersystem -computername $server | fl model $os = Get-WmiObject win32_operatingsystem -ComputerName $server $uptime = (Get-Date) - ($os.ConvertToDateTime($os.lastbootuptime)) $Display = "Uptime: " + $Uptime.Days + " days, " + $Uptime.Hours + " hours, " + $Uptime.Minutes + " minutes" #This builds the header for each server and include the name and uptime. Add-Content $ServiceHealthFileName "" Add-Content $ServiceHealthFileName "" Add-Content $ServiceHealthFileName "" Add-Content $ServiceHealthFileName "" #This writes the header and builds the body of each server health writeTableHeader $ServiceHealthFileName $store = @() $store += Get-WmiObject -Class Win32_Service -ComputerName $server -Filter "Name LIKE 'yourservice'" $store += Get-WmiObject -Class Win32_Service -ComputerName $server -Filter "Name LIKE 'yourservice'" $store += Get-WmiObject -Class Win32_Service -ComputerName $server -Filter "Name LIKE 'yourservice'" $store += Get-WmiObject -Class Win32_Service -ComputerName $server -Filter "Name LIKE 'yourservice'" $store += Get-WmiObject -Class Win32_Service -ComputerName $server -Filter "Name LIKE 'yourservice'" $store += Get-WmiObject -Class Win32_Service -ComputerName $server -Filter "Name LIKE 'yourservice'" $store += Get-WmiObject -Class Win32_Service -ComputerName $server -Filter "Name LIKE 'yourservice'" $store += Get-WmiObject -Class Win32_Service -ComputerName $server -Filter "Name LIKE 'yourservice'" #This write the body of each header in the order you defined foreach ($item in $store) { #If you dont want on screen read out as the script progresses; comment out the write-host line. #Write-Host $server $item.Name $item.ProcessId $item.State writeServiceInfo $ServiceHealthFileName $item.Name $item.ProcessId $item.State $item.StartMode $Item.ExitCode $Item.Status } } #This writes the footer writeHtmlFooter $ServiceHealthFileName $date = ( get-date ).ToString('yyyy/MM/dd') #Use the below line to setup an email of the report, #sendEmail emailaddress emailaddress "Server Status Report - $Date" EmailServer $ServiceHealthFileName
$server $Display