我正在使用以下腳本來獲取所有自動但未在一組服務器上運行的服務的列表。在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
請幫我這個。提前致謝!
問候,
拉姆
沒有'| Out-String'我在電子郵件中獲得與您相同的結果,但添加了| | Out-String'爲我解決了這個問題。我正在使用PS版本5. –
您是否嘗試過使用rn(帶勾號,無法在此處執行)? – majkinetor
@GeraldSchneider嗯......我有PS v3。 –