在Send-MailMessage中为正文使用数组

时间:2019-06-12 14:22:23

标签: powershell

我有一个小的脚本,可以通过AD查找并找到用户的EmployeeID号。但是当尝试发送邮件时,我收到如下错误消息。

错误消息:

Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported.
At line:53 char:93
+ ... maddress -Body ($expiredaccounts) -BodyAsHtml
+                    ~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage

这是我的代码:

$expiredaccounts = Search-ADAccount -AccountExpiring -TimeSpan 30.00:00:00 | Where-Object { $_.Enabled -eq $true } 
$expiredaccounts | Select-Object name, SamAccountName, AccountExpirationDate, @{Name='EmployeeID';Expression={($_ |Get-ADUser -Properties employeeID).employeeID}}, @{Name='EmployeeID2';Expression={($_ |Get-ADUser -Properties pager).pager}} |  ConvertTo-Html | Out-String

Send-MailMessage -to $emailto -Subject $subject -SmtpServer $smtp -From $fromaddress -Body ($expiredaccounts) -BodyAsHtml

0 个答案:

没有答案