次のスクリプトを使用して、自動的に実行されているが一連のサーバーで実行されていないすべてのサービスの一覧を取得しています。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を持っています。 –