【问题标题】:Email output of script脚本的电子邮件输出
【发布时间】:2015-01-06 09:37:10
【问题描述】:

我正在尝试通过电子邮件发送脚本的输出,但我很确定我的语法都搞混了。

这里是我想要的:

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
}

【问题讨论】:

    标签: email powershell output


    【解决方案1】:

    搞定了……

    如果出现错误:

    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 <b>| Out-String</b>)

    【讨论】:

      【解决方案2】:

      在通过send-mailmessage cmdlet 发送电子邮件之前,您必须创建$output

      试试这个代码:

      $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
      

      【讨论】:

      • 我试过你推荐的,它有效,但 ForEach 只运行一次。 ' $servers = 'SVRADFS365', 'SVRAPP01', 'SVRCAS01', 'SVRCAS02' $output= "{ ForEach ($servers in $servers) { $time = ([WMI]'').ConvertToDateTime((gwmi win32_operatingsystem - computername $server).LocalDateTime) $server + ' ' + $time } }" send-mailmessage -from " Daily Check" -to "Admin" -subject "Daily Check: Server Times" -body $output -priority High -dno onSuccess, onFailure -smtpServer mail.company.com'
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-20
      • 2017-03-20
      • 2013-01-11
      • 1970-01-01
      • 2015-10-16
      • 2012-10-16
      相关资源
      最近更新 更多