2016-04-25 13 views
0

Ich verwende das folgende Skript, um eine Liste aller Dienste zu erhalten, die zwar automatisch sind, aber nicht auf einer Reihe von Servern ausgeführt werden.Unterbrechung in Ausgabe in PowerShell entfernt

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 Ausgabe:

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) 

Empfangene E-Mail:

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) 

gewünschte E-Mail:

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 

Wie in, ich brauche jeden Dienstnamen in einer separaten Zeile angezeigt. Alle Namen der Dienste werden jedoch in derselben Zeile angezeigt.

PS-Version: 3.0

Bitte helfen Sie mir mit diesem. Danke im Voraus!

Grüße,
Ram

+1

Ohne '| Out-String "Ich erhalte das gleiche Ergebnis wie in meinen E-Mails, aber ich addiere' | Out-String "löst das für mich. Ich benutze PS Version 5. –

+0

Haben Sie versucht, stattdessen rn (mit Tick, kann es hier nicht tun)? – majkinetor

+0

@GeraldSchneider Hmm ... Ich habe PS v3. –

Antwort

1

Haben Sie die E-Mail HTML-Formatierung versucht?

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 
    } 

E-Mail:

 
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 *** 
+0

OK, ich werde jetzt eine dumme Frage stellen ... Es scheint, als ob die Servicenamen als Text in der E-Mail übergeben werden - wie in den Namen der Wie bekomme ich es, um die Ausgabe zu bekommen, um es richtig zu formatieren? –

+0

Sieht aus wie die '-ExpandProperty' das Problem verursacht. –

+0

Ich würde vorschlagen, den Namen des Services zu einem Array hinzuzufügen und dann das Array an den Körper der email: – Syrplex

Verwandte Themen