none
Convert empty array to text string for HTML conversion

    Question

  • I have a custom array stored as $REPORT3 that is sent as part of an HTML formatted email body using CovertTo-HTML.  There may be instances when $REPORT3 is empty.  Rather than just passing an e-mail body that is empty I wanted to insert a text string indicating "no matching criteria".

    $REPORT3 = @()

    collect data to insert into $REPORT3

    message body = $REPORT3 | ConvertTo-HTML

    Now suppose $REPORT3.Length -eq 0, what value can I assign to $REPORT3 such that it can be converted to HTML and still work in the above framework?  I tried $REPORT3 = "no matches" and $REPORT3 = "<html> no matches /<html>" if array length is 0, but I get garbarge output from the HTML conversion (random numbers and and asterisk character).

    $REPORT3 = @()

    collect data to insert into $REPORT3

    if ($REPORT3.length -eq 0) {$REPORT3 = ???}

    message body = $REPORT3 | ConvertTo-HTML

    Thursday, June 14, 2012 4:32 PM

Answers

  • Hi,
    One of several simple solutions.

    $REPORT3 = @()
    if ($REPORT3.length -eq 0) 
    {
     $REPORT3 | ConvertTo-HTML -Body "no matching criteria"
    }
    else
    {
     $REPORT3 | ConvertTo-HTML
    } 
    

    • Marked as answer by tickermcse76 Thursday, June 14, 2012 11:09 PM
    Thursday, June 14, 2012 4:58 PM

All replies