我正在使用以下脚本来获取所有自动但未在一组服务器上运行的服务的列表。在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。 –