PowerShell 脚本输出到电子邮件
PowerShell script output to email
我正在尝试将一个 PowerShell 命令放在一起,它将显示,如果用户启用了 OOF,则用户的显示名称、主 SMTP 地址、开始时间和结束时间。
我发现了一个可以显示用户名的命令。但我需要添加用户的显示名称、主 SMTP 地址、StartTime 和 EndTime。
我的问题是:尝试发送 HTML 格式的邮件时,我收到以下错误消息。
代码:
$master = [collections.arraylist]@()
$mailboxlist = Get-Mailbox -ResultSize Unlimited
foreach ($mailbox in $mailboxlist) {
$reply = $mailbox | Get-MailboxAutoReplyConfiguration
if ($reply.AutoReplyState -in "Scheduled","Enabled") {
$output = [ordered]@{
'DisplayName' = $null
'Email' = $null
'Start' = $null
'End' = $null
}
$output.DisplayName = $mailbox.DisplayName
$output.Email = $mailbox.PrimarySMTPAddress
switch ($reply.AutoReplyState) {
"Scheduled" {
$output.Start = $reply.StartTime
$output.End = $reply.EndTime
}
"Enabled" {
$output.Start = "Not Scheduled"
$output.End = "Not Scheduled"
}
}
$output = [PSCustomObject]$output
$master.Add($output) | Out-Null
}
}
$master | ConvertTo-Html | Out-String
Send-MailMessage -To $emailto -Subject $subject -SmtpServer $smtp -From $fromaddress -Body ($master) -BodyAsHtml -Credential $UserCredential
错误信息是:
Send-MailMessage : Cannot convert 'System.Collections.ArrayList' to the type
'System.String' required by parameter 'Body'. Specified method is not supported.
At line:67 char:92
+ ... maddress -Body ($master) -BodyAsHtml -Credential $UserCredential
+ ~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage
声明
$master | ConvertTo-Html | Out-String
不转换 $master
in-place 的内容。它只是将其写入输出流。在 Send-MailMessage
语句中使用它之前,您需要将结果分配回变量:
$master = $master | ConvertTo-Html | Out-String
我正在尝试将一个 PowerShell 命令放在一起,它将显示,如果用户启用了 OOF,则用户的显示名称、主 SMTP 地址、开始时间和结束时间。
我发现了一个可以显示用户名的命令。但我需要添加用户的显示名称、主 SMTP 地址、StartTime 和 EndTime。
我的问题是:尝试发送 HTML 格式的邮件时,我收到以下错误消息。
代码:
$master = [collections.arraylist]@()
$mailboxlist = Get-Mailbox -ResultSize Unlimited
foreach ($mailbox in $mailboxlist) {
$reply = $mailbox | Get-MailboxAutoReplyConfiguration
if ($reply.AutoReplyState -in "Scheduled","Enabled") {
$output = [ordered]@{
'DisplayName' = $null
'Email' = $null
'Start' = $null
'End' = $null
}
$output.DisplayName = $mailbox.DisplayName
$output.Email = $mailbox.PrimarySMTPAddress
switch ($reply.AutoReplyState) {
"Scheduled" {
$output.Start = $reply.StartTime
$output.End = $reply.EndTime
}
"Enabled" {
$output.Start = "Not Scheduled"
$output.End = "Not Scheduled"
}
}
$output = [PSCustomObject]$output
$master.Add($output) | Out-Null
}
}
$master | ConvertTo-Html | Out-String
Send-MailMessage -To $emailto -Subject $subject -SmtpServer $smtp -From $fromaddress -Body ($master) -BodyAsHtml -Credential $UserCredential
错误信息是:
Send-MailMessage : Cannot convert 'System.Collections.ArrayList' to the type 'System.String' required by parameter 'Body'. Specified method is not supported. At line:67 char:92 + ... maddress -Body ($master) -BodyAsHtml -Credential $UserCredential + ~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage
声明
$master | ConvertTo-Html | Out-String
不转换 $master
in-place 的内容。它只是将其写入输出流。在 Send-MailMessage
语句中使用它之前,您需要将结果分配回变量:
$master = $master | ConvertTo-Html | Out-String