2016-04-25 24 views
0

我正在使用以下腳本來獲取所有自動但未在一組服務器上運行的服務的列表。在PowerShell中的輸出中刪除換行符

代碼:

$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' 

控制檯輸出:

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) 

收到的電子郵件:

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) 

所需的電子郵件:

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 

因爲,我需要每個服務名稱都顯示在一個單獨的行中。但是,所有服務的名稱都顯示在同一行上。

PS版本:3.0

請幫我這個。提前致謝!

問候,
拉姆

+1

沒有'| Out-String'我在電子郵件中獲得與您相同的結果,但添加了| | Out-String'爲我解決了這個問題。我正在使用PS版本5. –

+0

您是否嘗試過使用rn(帶勾號,無法在此處執行)? – majkinetor

+0

@GeraldSchneider嗯......我有PS v3。 –

回答

1

您是否嘗試過格式化電子郵件到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 
    } 

電子郵件:

 
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

好吧,我現在要問一個愚蠢的問題......看起來服務名稱是作爲電子郵件中的文本傳遞的 - 就像你明確提到服務。我怎樣才能得到它的輸出格式正確? –

+0

看起來像'-ExpandProperty'是造成這個問題。 –

+0

我建議將服務名稱添加到數組,然後將數組傳遞到電子郵件 – Syrplex