脚本的电子邮件输出

时间:2015-01-06 09:37:10

标签: email powershell output

我试图通过电子邮件发送脚本的输出,但我非常确定我的语法混乱了。

我喜欢这里:

send-mailmessage -from " Daily Check <server@company.com>" -to "Admin <admin@company.com>" -subject "Daily Check: Server Times" -body $output -priority High -dno onSuccess, onFailure -smtpServer mail.company.com

$output=

{
ForEach ($server in $servers) {
    $time = ([WMI]'').ConvertToDateTime((gwmi win32_operatingsystem -computername $server).LocalDateTime)
    $server + '  ' + $time
    }        
}
$servers = 'SVRADFS365', 'SVRAPP01', 'SVRCAS01', 'SVRCAS02

我希望通过电子邮件发送其输出的原始脚本:

$servers = 'server1', 'server2', 'server3', 'server4'
ForEach ($server in $servers) {
    $time = ([WMI]'').ConvertToDateTime((gwmi win32_operatingsystem -computername $server).LocalDateTime)
    $server + '  ' + $time
}

2 个答案:

答案 0 :(得分:2)

搞定了......

如果收到错误:

Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported.
At line:11 char:150
+ ... r Times" -body ($output ) -priority High -dno onSuccess, onFailure -smtpServer m ...
+                    ~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage

添加:-body ($output | Out-String)

答案 1 :(得分:1)

您必须先创建$output,然后才能通过send-mailmessage cmdlet发送电子邮件。

尝试使用此代码:

$servers = 'SVRADFS365', 'SVRAPP01', 'SVRCAS01', 'SVRCAS02'
$output = ForEach ($server in $servers) {
            $time = ([WMI]'').ConvertToDateTime((gwmi win32_operatingsystem -computername $server).LocalDateTime)
            $server + ' ' + $time
          }

send-mailmessage -from " Daily Check <server@company.com>" -to "Admin <admin@company.com>" -subject "Daily Check: Server Times" -body $output -priority High -dno onSuccess, onFailure -smtpServer mail.company.com