2016-04-25 25 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