question

SamPeter-2101 avatar image
0 Votes"
SamPeter-2101 asked NewbieJones-6218 edited

Windows Services Status

Hi
I have list of services in notepad need powershell script to check if those services are running. If any one service is stopped then i should get an email notification. If all services running no need to get any email.
Pls share the script.

Thanks
Sam

windows-server-powershell
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Fairly certain this is a common activity, so perhaps try searching the web and adopting a script.

If you run into problems, you can then post what you've tried here which will be more likely to get a response.

Your starter for 10 is the Get-Service Cmdlet.

0 Votes 0 ·
SamPeter-2101 avatar image
0 Votes"
SamPeter-2101 answered NewbieJones-6218 commented

Thanks NewbieJones-6218
In fact i tried google a lot but i can see there are scripts which sends email if specific service is stopped.
But could not get the script I'm looking for?

My requirement is i have set of services in notepad if all services are running no email should be triggered, if any one service is stopped i need to an email notification this my query, could you pls help me with the script.

As I'm new to powershell script need expert advice. So posted my query here.

Thanks
Sam

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

A quick google search.

https://4sysops.com/archives/monitoring-windows-services-with-powershell
https://devblogs.microsoft.com/scripting/send-an-email-message-with-service-status-via-powershell/

Basic process will be to import the service list from the file. (This would be easier if they were saved in a CSV File with a header called services) and pipe that into a ForEach loop.
The ForEach loop should check to see if the services is running which you can use the first link to show you how. (Hint: Using an IF statement).
If its not running, then use the second link to show how to send an email.

If you are new to PowerShell, I would recommend starting by learning the basics. Import commands, loops, etc. Otherwise this won't be a fun experience.
Not sure if anyone will just write you a full working script. But people will help you with fixing\refactoring one you already have.


0 Votes 0 ·
NewbieJones-6218 avatar image
0 Votes"
NewbieJones-6218 answered NewbieJones-6218 edited

This is a starter for 10.

 # $list = Import-CSV "path\filename.csv" 
 # $list = "WSearch","WinDefend" # all of these should be running
    
 $list = "XblGameSave", "WSearch", "WinDefend" # Xbox service should be stopped
    
    
 $computer =$env:computername # just running this against the local machine for testing purposes
    
 $ShouldSendEmail = $false
    
 $failedServices=@()
    
    
 ForEach ($service in $list) { 
    
     $status = (Get-Service -Name $service -ComputerName $computer).Status
     # If you give the file a header for example Name, then you need to change $service to $service.Name
        
     If ($status -ne 'Running') {
         #email should be sent.
         $ShouldSendEMail = $true
         $failedServices +=  New-Object -TypeName PSObject -property @{
              Computer=$computer
              Name=$service;
              Status=$status}  
         } 
            
     Else {
         # Do something or nothing
     }
    
 }
    
 If ($ShouldSendEMail) {
     $failedServices
     Write-Host Send email
     } 
        
     Else {
     Write-Host Dont send email
     }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.