question

DwightSnow-9524 avatar image
0 Votes"
DwightSnow-9524 asked DwightSnow-9524 commented

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

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
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.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

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'
 }
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.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered DwightSnow-9524 commented

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.

· 3
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.

Hey RIch,

so what would you recommend. Like I said I can get the general logic, but need a few hints to put together a proper script. What i want to accomplish is to find windows services who are set to auto start and not started, and then notify via email with exit code.


Thanks


Dwight

0 Votes 0 ·

Are you only interested in services that auto-start? Are you interested in ALL of them, or just some subset of critical ones? Are you interested in whether they get restarted? If so, what do you want to do? Are you going to run this checking on local machines, or are you going to do the checking from a central location? Is WinRM usable (enabled, port not blocked on intervening or local firewalls) on all of the target machines? What do you want to do if a machine in inaccessible (maybe offline temporarily, rebooting, crashed, etc.)? Will you be running the monitoring from an account that has administrative access on the remote machines? Will you attempt to restart the services? If so, how many times before you give up and notify someone? Are you interested in achieving some level of parallel work (it's not that hard) -- that's something you should care about if the number of monitored machines is more than just a few.

Your objective sounds so simple, but admin/monitor things are rarely that easy!

0 Votes 0 ·

find windows services who are set to auto start and not started, and then notify via email with exit code

0 Votes 0 ·
LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered DwightSnow-9524 commented

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-

· 2
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.

The operational difference is between WinRM and RPC.

However, your example doesn't get both names associated with the service, not does it get the exit code. Oh, and it doesn't send any e-mail.

0 Votes 0 ·

Thanks Rich,

Dwight

0 Votes 0 ·