I am trying to get a PowerShell command together that will show, If the user has OOF enabled , the user's display name, primary SMTP address , StartTime and EndTime.
I have a command I found that will show the user's name. But I need to add in the the user's display name, primary SMTP address , StartTime and EndTime.
My question is: when attempting to send mail HTML formatted then I got the following the error message.
Code:
$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
Error message is:
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
$masteris an array -- And you're trying to use it as a string -- Try ouputting your$masterarray, and pull the pertinent info (or build the appropriate string)-Body $($master -join [Environment]::NewLine)or similar....in other words, converting it to a string instead.