Arbelac
Arbelac

Reputation: 1904

PowerShell script output to email

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

Upvotes: 0

Views: 1412

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

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

Upvotes: 1

Related Questions