在PowerShell

时间:2016-04-25 11:33:30

标签: powershell

我正在使用以下脚本获取自动但未在一组服务器上运行的所有服务的列表。

代码:

$Me = 'me@domain.com'

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 'BigGuy@domain.com' -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 个答案:

答案 0 :(得分:1)

您是否尝试将电子邮件格式化为HTML?

Function Send-Email {
$From = "email1@domain.com"
$To = "bigguy@domain.com"
$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 ***