locked
Alert of auto services that are stopped and email results RRS feed

  • Question

  • Hi,

    I've been working on a fairly basic script that can be setup to run on a PC or Server to start any servers are that currently stopped but are set to auto start.

    Currently I can get the info needed with various services excepts pipe it to start service and that run it again to reveal the services are didn't start or have trouble starting and might need to be looked at by a tech.

    For the next part I've like to email the results of the still failing services to a helpdesk, I know this can be done by exporting them to a txt or csv file but my question is can this be done without needing to save anything to a file??

    #Email Settings $MailFrom = “xxxxx@xxxx.com” $Mailto = “xxxxxxx@xxx.com” $MailSubject = Hostname # get Auto that are not Running: Get-CimInstance Win32_Service | Where-Object { $_.StartMode -eq 'Auto' -and $_.State -ne 'Running' } | Where-Object -FilterScript { ($_.Name -notlike 'gpsvc') -and ($_.Name -notlike 'DoSvc') -and ($_.Name -notlike 'MapsBroker') -and ($_.Name -notlike 'RemoteRegistry') -and ($_.Name -notlike 'sppsvc')

    -and ($_.Name -notlike 'TrustedInstaller') } | Start-Service Get-CimInstance Win32_Service | Where-Object { $_.StartMode -eq 'Auto' -and $_.State -ne 'Running' } | Where-Object -FilterScript { ($_.Name -notlike 'gpsvc') -and ($_.Name -notlike 'DoSvc') -and ($_.Name -notlike 'MapsBroker') -and ($_.Name -notlike 'RemoteRegistry') -and ($_.Name -notlike 'sppsvc')

    -and ($_.Name -notlike 'TrustedInstaller') } | Select-Object @{l='Computer Name';e={$env:computername}}, @{l='Service Name';e={$_}} #How can I now get the results to the send mail? #Send Email Send-MailMessage -to $Mailto -from $MailFrom -subject $MailSubject -body "The following services are stopped $results" -smtpServer xxxx.xxxxx.com



      
    Sunday, January 14, 2018 1:55 PM

All replies

  • You definitely do NOT want to do this.  If you have services that are supposed to be running and stop for no reason you need to fix those services.

    There are many services that are set to auto but will always stop after they have been run.  This is normal.

    To learn how to get output to text see the following:

    help Format-Table -online
    help Out-String -online

    To learn how to use PowerShell to generate various types of output see: Learn PowerShell  


    \_(ツ)_/


    Sunday, January 14, 2018 2:53 PM
  • Where is $Results coming from? if you want you can take $results first and see if it will give you a good output using  convertto-HTML -fragment. Then dump it into the email body as HTML with the -bodyasHTML. I agree with the last guy, you should investigate as to why they are stopped before just starting them.

    as an example i use this:

    $smtpsettings = @{
    SmtpServer = "xxx.xxx.xxx.xxx"
    From = "ServiceNotRunningReport@abc.com"
    To = "HelpDesk@abc.com"
    Subject = "Services not Running Report"
    }
    
    $PC = Get-Content 'C:\Scripts\Jobs\PC-list2.txt'
    
    $head2 = @"
    <Title>Services Not Running Report </Title>
    <style>
    body { background-color:#FFFFFF;
           font-family:Tahoma;
           font-size:12pt; }
    td, th { border:1px solid black; 
             border-collapse:collapse; }
    th { color:white;
         background-color:black; }
    table, tr, td, th { padding: 2px; margin: 0px }
    tr:nth-child(odd) {background-color: lightgray}
    table { width:95%;margin-left:5px; margin-bottom:20px;}
    </style>
    <br>
    <H1>Services Not Running Report</H1>
    "@
    
    $paramHash2 = @{
    Head = $head2
    Title = "Services Not Running Report"
    As = "Table"
    PostContent = "<H6>$(Get-Date)</H6>"
    Property = "ComputerName","Caption", "State"
    }
    
    $ServiceCheck2 = @()
    
    $Report = Foreach ($a in $pc) { 
    
    
    $ServiceCheck = Get-WmiObject Win32_Service -ComputerName $a |Where-Object { $_.StartMode -eq 'Auto' -and $_.State -ne 'Running' } |Select-Object PSComputerName, Caption, Name, State
    
        foreach ($sc in $ServiceCheck)
        {
            $ServiceCheckItem = new-object PSObject  
            $ServiceCheckItem | add-member NoteProperty "ComputerName" $sc.PSComputerName
            $ServiceCheckItem | add-member NoteProperty "Caption" $sc.Caption
            $ServiceCheckItem | add-member NoteProperty "Name" $sc.Name
            $ServiceCheckItem | add-member NoteProperty "State" $sc.State
    
            $ServiceCheck2 += $ServiceCheckItem
        }
    
    }
    
    $ServiceCheck2 = $ServiceCheck2 | ConvertTo-Html @paramHash2    
    
    $Report = $ServiceCheck2
    
    Send-MailMessage @smtpsettings -Body "$Report" -BodyAsHtml


    • Edited by Jeff_at_ZF Tuesday, January 16, 2018 8:37 PM
    • Proposed as answer by Jeff_at_ZF Tuesday, January 16, 2018 8:38 PM
    Tuesday, January 16, 2018 8:36 PM