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.
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.
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!!)
Of course you have to use the server names and service names from your environment.
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: