Windows Service Monitor

Hello
Below is my script which is working fine. If service is stopped i get email notification, but problem is if service is running I get blank email. How to ignore sending email if services are running. I want to get email only if service is stopped. Request your expert advice or help on below code.

#######################################################################
$EVServerList = Get-Content “C:\Monitor\server.txt”
$EVServicesList = Get-Content “C:\Monitor\services.txt”

$report = “C:\Monitor\Report.htm”

$smtphost = “my host name”
$from = “pssc786@hotmail.com
$to = “pssc786@hotmail.com

$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 “”
Add-Content $report “”
Add-Content $report “”
Add-Content $report “”
Add-Content $report “”
Add-Content $report “”

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.name t $serviceStatus.status -ForegroundColor Red
$svcName = $serviceStatus.name
$svcState = $serviceStatus.status
Add-Content $report “


Add-Content $report “”
Add-Content $report “”
Add-Content $report “”
Add-Content $report “”
}
}
}
}

servicestatus $EVServerList $EVServicesList

Add-content $report “

Server Name Service Name Status
$machineName $svcName $svcState

Add-Content $report “”
Add-Content $report “”

$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)

Power Shell,
Welcome to the forum. :wave:t4:

You have to move the command you use to send the email into the script block belonging to the condition where you check the running state of the service.

BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

$EVServerList = Get-Content “C:\Monitor\server.txt”
$EVServicesList = Get-Content “C:\Monitor\services.txt”

$report = “C:\Monitor\Report.htm”

$smtphost = “my host name”
$from = “[pssc786@hotmail.com](mailto:pssc786@hotmail.com)”
$to = “[pssc786@hotmail.com](mailto:pssc786@hotmail.com)”

$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 “”
Add-Content $report “”
Add-Content $report “”
Add-Content $report “”
Add-Content $report “”
Add-Content $report “”

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.name ` t $serviceStatus.status -ForegroundColor Red
$svcName = $serviceStatus.name
$svcState = $serviceStatus.status
Add-Content $report “

”
Add-Content $report ""
Add-Content $report ""
Add-Content $report “”
Add-Content $report “”
}
}
}
}

servicestatus $EVServerList $EVServicesList

Add-content $report “

|**Server Name**|**Service Name**|**Status**|
| --- | --- | --- |
|**$machineName**|**$svcName**|**$svcState**|

”
Add-Content $report “”
Add-Content $report “”

$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)

You could have edited your existing post. :wink: But anyway my answer is still valid. :man_shrugging:t4: Move the command into the script block of the the if statement. :point_up_2:t4:

Regardless of that - Get-Service can take more than one service name at a time. So you don’t need a loop for that.

sorry not clear could u pls highlight which line i need to move

It’s not just one line you’d need to change. You’re creating some of the output you use inside a function and some of it outside a function. That’s not a good idea.

I’d approach a task like this like this: (That’s just an example!!)

$EVServerList = 'Server01','Server02'
$EVServicesList = 'BITS','EventLog'

$Result =
Invoke-Command -ComputerName $EVServerList -ScriptBlock {
    Get-Service -Name $Using:EVServicesList
}

$Result

Of course you have to use the server names and service names from your environment. :point_up_2:t4:

Now you have the result of the query saved in the variable $Result. You can use it as often as needed for different purposses if you like. You can filter for only stopped services or you can pipe the result to ConvertTo-Html to send it by email.

Regardless of all that I’d recommend to read the following topics:

1 Like