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

Eメール: `なし

 
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

OK、私は今、ばかげた質問をするつもりです...サービス名が電子メールのテキストとして渡されているようです。サービスを正しくフォーマットするための出力を得るにはどうすればよいでしょうか? –

+0

'-ExpandProperty'のように見えます。 –

+0

サービス名を配列に追加し、配列を本文に渡すことをお勧めします。電子メール。 – Syrplex

関連する問題