Hi RichMatheisen-8856
As per your advice i've modified the script as below, but still output is not as per my expectation.
Pls help I'm checking list of services where if any one service in services.txt file is stopped only i should get email. If all services are running i dont want to get email.
$EVServerList = Get-Content "C:\Monitor\server.txt"
$EVServicesList = Get-Content "C:\Monitor\services.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-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>"
Function servicestatus ($serverlist, $serviceslist)
{
foreach ($machineName in $serverlist)
{
foreach ($service in $serviceslist)
{
$serviceStatus = get-service -ComputerName $machineName -Name $service
if ($serviceStatus.status -ne "Running")
{
Write-Host $machineName t $serviceStatus.namet $serviceStatus.status -ForegroundColor Red
$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= 'Red' align=center><B>$svcState</B></td>"
Add-Content $report "</tr>"
}
}
}
}
$ShouldSendEmail = $false
servicestatus $EVServerList $EVServicesList
if (-not $ShouldSendEmail){ return }
Add-content $report "</table>"
Add-Content $report "</body>"
Add-Content $report "</html>"
$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)