PS code for window service check for automatic start and those services with that that are not started and notify via E-mail

Dwight Snow 21 Reputation points
2021-09-23T17:50:38.137+00:00

I am not a PS guru, hence the request. I have a scenario to check all windows service on a window host that are set for automatic startup, however are not started for whatever reason.

I have a programming background, however just not PS syntax friendly. I have gathered these two pieces and need to put them in a PS script that is PS v4 or higher.

Get-wmiobject win32_service -Filter “startmode = ‘auto’ AND state != ‘running’ AND Exitcode !=0 ” -ComputerName sql1 | select name, startname, exitcode

C:\> Get-PSServiceStatus -ComputerName test-01, -ServiceName ‘RasMan' -Path ‘C:\PSServiceStatus\' -FromAddress ‘alerts@emailsrv.com' ‑ToAddress ‘ITadmin@emailsrv.com' -SmtpServer ‘smtp.domain.com'

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,362 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 44,776 Reputation points
    2021-09-24T02:07:50.79+00:00

    Okay . . . bare bones it is!

    $NotRunning = Get-WmiObject win32_service -Filter "startmode = 'auto' AND state != 'running' AND Exitcode !=0 " -ComputerName sql1 | 
                    Select-Object name, startname, exitcode
    If ($NotRunning) {
        $s = $NotRunning | Out-String
        $body = @"
    Dear ITadmin,
    
    Your machine is in trouble! The following services aren't running:
    $s
    "@
        Send-MailMessage -To 'ITadmin@emailsrv.com' -From alerts 'alerts@emailsrv.com' -Body $body -SmtpServer 'smtp.domain.com'
    }
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2021-09-23T19:09:25.407+00:00

    That Get-PSServiceStatus might be a bit schizophrenic. It uses ForEach with the -Parallel parameter (which is only present in version 7) and Get-Service with the -ComputerName parameter which isn't present in version 7!

    It also accepts only a single service name in each use of the function. And, it reads and writes a file for each computer and each service. If you're working with a lot of computers and every service on each of them that has a start mode of 'auto' that's a lot of reading of individual files together with the writing/removal of those files when service status changes.


  2. Limitless Technology 39,351 Reputation points
    2021-09-24T09:41:49.573+00:00

    Hello Dwight,

    I would phrase it in this way:

    Get-Service | Select-Object -Property Name,Status,StartType | Where-Object {$.Status -eq "Stopped" -and $.StartType -eq "Automatic"}

    Hope this helps,

    Get-Service | Select-Object -Property Name,Status,StartType | Where-Object {$.Status -eq "Stopped" -and $.StartType -eq "Automatic"}


    --If the reply is helpful, please Upvote and Accept as answer-