Send-MailMessage:无法将 'System.Object[]' 转换为参数 'Body' 所需的类型 'System.String'。不支持指定的方法

Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported

我正在尝试使用 powershell 发送电子邮件。电子邮件的正文将包含存储在变量中的 Get-ADuser 命令 I 运行 的结果。当我尝试下面的代码时,出现错误 "Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported."

我在这里做的有什么问题吗?

$Value = Get-ADUser -Filter * -Properties propery.. | foreach { $_.propery..}
Send-MailMessage -From $From -To $To -Subject $Subject -Body $Value 

-body 参数需要传递给它的字符串。您需要将变量转换为字符串类型或将其值操作为字符串。您可以通过多种方式完成此操作。

Send-MailMessage -From $From -To $To -Subject $Subject -Body ($Value | Out-String)

Out-String 在这里工作是因为它获取您的对象 ($Value),这是一个包含多个 ADUser 对象的 单个数组对象 ,并将其转换为一个字符串。

有关详细信息,请参阅 Out-String