Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to send an email message using powershell. That the body of the email will contain the results of the Get-ADuser command I run that is stored in a variable. When I try the code below I get the error "Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported."

Is there something wrong with what I am doing here?

$Value = Get-ADUser -Filter * -Properties propery.. | foreach { $_.propery..}
Send-MailMessage -From $From -To $To -Subject $Subject -Body $Value 
like image 268
user1342164 Avatar asked Mar 19 '26 06:03

user1342164


1 Answers

The -body parameter expects a string to be passed to it. You will need to convert your variable to be type string or manipulate its value to be a string. You can accomplish this in numerous ways.

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

Out-String works here because it takes your object ($Value), which is a single array object containing multiple ADUser objects, and converts it into a single string.

See Out-String for more details.

like image 143
AdminOfThings Avatar answered Mar 20 '26 20:03

AdminOfThings