Hi All,
Below scrip is created to check list of windows services running or stopped. If any one services in list is stopped send an email. If all services are running i don't want to send any email. In below script is working fine send an email notification if any services are stopped, but if all services are running fine also i'm getting blank email.
Kindly advice or share the working script.
TIA
Call Function
servicestatus $EVServerList $EVServicesList
$EVServerList = Get-Content "C:\LMSupport\Monitor\qaserver.txt"
$EVServicesList = Get-Content "C:\LMSupport\Monitor\qaservices.txt"
$report = "C:\Monitor\Report.htm"
$smtphost = "My smtp host is given here"
$from = "From Email ID given here"
$to = "To Email ID given here"
$checkrep = Test-Path "C:\Monitor\Report.htm"
If ($checkrep -like "True")
{
Remove-Item "C:\Monitor\Report.htm"
}
New-Item "C:\Monitor\Report.htm" -type file
ADD HTML Content
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>3DExperience Service Status Report</title>'
Add-Content $report "</head>"
Add-Content $report "<body>"
add-content $report "<table width='100%'>"
add-content $report "<tr bgcolor='Lavender'>"
add-content $report "<td colspan='7' height='25' align='center'>"
add-content $report "<font face='tahoma' color='#003399' size='4'><strong>Service Status 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='IndianRed'>"
Add-Content $report "<td width='10%' align='center'><B>Server Name</B></td>"
Add-Content $report "<td width='50%' align='center'><B>Service Name</B></td>"
Add-Content $report "<td width='10%' align='center'><B>Status</B></td>"
Add-Content $report "</tr>"
Get Services Status
Function servicestatus ($serverlist, $serviceslist)
{
foreach ($machineName in $serverlist)
{
foreach ($service in $serviceslist)
{
$serviceStatus = get-service -ComputerName $machineName -Name $service
while ($serviceStatus.status -ne "Running")
{
Start-Service $service
Start-Sleep -seconds 3
$serviceStatus.Refresh()
if ($serviceStatus.Status -eq 'Running')
{
Write-Host $machineName t $serviceStatus.name t $serviceStatus.status -ForegroundColor Green
$svcName = $serviceStatus.name
$svcState = $serviceStatus.status
Add-Content $report "<tr>"
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B> $machineName</B></td>"
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B>$svcName</B></td>"
Add-Content $report "<td bgcolor= 'Aquamarine' align=center><B>$svcState</B></td>"
Add-Content $report "</tr>"
}
}
}
}
}
Close HTMl Tables
Add-content $report "</table>"
Add-Content $report "</body>"
Add-Content $report "</html>"
Send Email
$subject = "Service Monitor Alert"
$body = Get-Content "C:\Monitor\Report.htm"
$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)