0

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
2
  • 1
    It looks to me like $master is an array -- And you're trying to use it as a string -- Try ouputting your $master array, and pull the pertinent info (or build the appropriate string) Commented Jul 23, 2019 at 20:06
  • Yeah....you might even be able to say: -Body $($master -join [Environment]::NewLine) or similar....in other words, converting it to a string instead. Commented Jul 23, 2019 at 20:50

1 Answer 1

1

The statement

$master | ConvertTo-Html | Out-String

doesn't convert the content of $master in-place. It just writes it to the output stream. You need to assign the result back to the variable before using it in the Send-MailMessage statement:

$master = $master | ConvertTo-Html | Out-String
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.