0

I'm using the following script to get a list of all the services that are automatic but not running on a set of servers.

Code:

$Me = '[email protected]'

function Get-AutoServiceStatus
{
    $Servers = 'SRV1', 'SRV2', 'SVR3', 'SVR4'
    foreach ($Server in $Servers)
    {
        Write-Output "`nThe automatic services that aren't running on $Server are:"
        Get-WmiObject win32_service -Filter "StartMode = 'auto' AND state != 'Running'" -ComputerName $Server | select -ExpandProperty DisplayName | Format-List
    }
}

$Output = Get-AutoServiceStatus | Out-String
$Body = "Hi team,`n`nHere are the services that are set to start automatically on each of the listed servers, but aren't running.`n" + $Output + "Please take the necessary actions.`n`nRegards,`nBig Guy"

Send-MailMessage -From '[email protected]' -To $Me -SmtpServer 'smtpserver.domain.com' -Body $Body -Subject 'Post-reboot service check on Servers'

Console output:

The automatic services that aren't running on MySrv are:
Microsoft .NET Framework NGEN v4.0.30319_X86
Microsoft .NET Framework NGEN v4.0.30319_X64
Software Protection
Windows Image Acquisition (WIA)

Received email:

The automatic services that aren't running on SRV1 are:
Microsoft .NET Framework NGEN v4.0.30319_X86Microsoft .NET Framework NGEN v4.0.30319_X64Software ProtectionWindows Image Acquisition (WIA)

Desired email:

Some friendly text here

The automatic services that aren't running on MySrv are:
Microsoft .NET Framework NGEN v4.0.30319_X86
Microsoft .NET Framework NGEN v4.0.30319_X64
Software Protection
Windows Image Acquisition (WIA)

Bye

As in, I need each service name appear on a separate line. However, all the names of the services appear on the same line.

PS version: 3.0

Please help me with this. Thanks in advance!

Regards,
Ram

13
  • 1
    Without | Out-String I get the same result as you in my emails, but adding | Out-String solves this for me. I'm using PS version 5. Commented Apr 25, 2016 at 11:43
  • Have you tried using rn (with tick, can't do it here) instead ? Commented Apr 25, 2016 at 12:53
  • @GeraldSchneider Hmm... I have PS v3. Commented Apr 25, 2016 at 13:26
  • @majkinetor, backtick-m instead of backtick-n? No, the output gives about four objects in a shot. So how do I break the line between the output objects? Commented Apr 25, 2016 at 13:28
  • 1
    It is possible that the email client is trying to be "helpful" and removing line breaks in the display of the message. View the raw email content to see whether the line breaks are really there. In any case, you can use HTML format for better control of the resulting message. Commented Apr 25, 2016 at 14:35

1 Answer 1

1

Have you tried formatting the email to HTML?

Function Send-Email {
$From = "[email protected]"
$To = "[email protected]"
$Body = "<strong>Date:</strong>"+(Get-Date)+"<br>"+"<strong>Hostname:</strong>$hostname<br>"+"`<strong>The automatic services that aren't running on MySrv are:</strong>.<br>$Output<br><br><br><br><br><br><br><h3 style=""color:red;"">*** This is an automatically generated email, please do not reply ***</h3>"

Send-MailMessage -From $From -to $To -Subject $Subject `
    -Body $Body -SmtpServer $SMTPServer -BodyAsHtml
    }

Email:

Hostname: SERV1
Date:04/25/2016 11:55 AM
The automatic services that aren't running on MySrv are:
upnbphost
WinMgmt
*** This is an automatically generated email, please do not reply ***
Sign up to request clarification or add additional context in comments.

4 Comments

OK, I'm gonna ask a stupid question now... It seems like the service names are being passed as text in the email—as in you're explicitly mentioning the names of the services. How do I get it to get the output to format it rightly?
Looks like the -ExpandProperty is causing the issue.
I would suggest adding the services name to an array and then passing the array to the body of the email.
I'll try this and get back. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.