Windows Services Status

Sam Peter 1 Reputation point
2022-05-20T03:39:42.677+00:00

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
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,344 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Sam Peter 1 Reputation point
    2022-05-20T12:14:10.28+00:00

    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


  2. Newbie Jones 1,306 Reputation points
    2022-05-20T17:15:14.643+00:00

    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
        }
    
    0 comments No comments