locked
How to send via email instead of CSV RRS feed

  • Question

  • I want to send the output to an email instead of CSV?

    $aPrinterList = @()

    $Results = Get-WinEvent -FilterHashTable @{LogName="Microsoft-Windows-PrintService/Operational"; ID=307; StartTime = [datetime]::Today } -ComputerName "printserver.sacs.org"

    ForEach($Result in $Results){
    $ProperyData = [xml]$Result.ToXml()
    $PrinterName = $ProperyData.Event.UserData.DocumentPrinted.Param5
    If($PrinterName.Contains("Library BW Printer")){
    $hItemDetails = New-Object -TypeName psobject -Property @{
    UserName = $ProperyData.Event.UserData.DocumentPrinted.Param3
    MachineName = $ProperyData.Event.UserData.DocumentPrinted.Param4
    PrinterName = $PrinterName
    PageCount = $ProperyData.Event.UserData.DocumentPrinted.Param8
    TimeCreated = $Result.TimeCreated
    }

    $aPrinterList += $hItemDetails
    }
    }

    #Output results to CSV file
    $aPrinterList | Export-Csv -LiteralPath C:\PrintAudit.csv

    --------------------------------------------------------------------

    I've figured out I can send an email using Send-MailMessage but the body of the email does not contain any data.

    #Output results to email

    Send-MailMessage `
        -From "User01 <printserver@domain.org>" `
        -To "Me <me@domain.org>", "Other Admin <none@contoso.com>" `
        -Subject "PrintReport" `
        -Body "$aPrinterList" `
        -SmtpServer "mail.domain.org"

       

    Monday, October 1, 2018 3:34 PM

Answers

  • -Body ($aPrinterList|Out-String)

    \_(ツ)_/

    • Marked as answer by David Hoeft Wednesday, October 3, 2018 8:02 PM
    Monday, October 1, 2018 3:41 PM
  • This will fix the text.

    $body = '<pre>' + ($aPrinterList | Format-Table | Out-String) + '</pre>'
    Send-MailMessage -Body $body -BodyAsHtml ….


    \_(ツ)_/

    • Marked as answer by David Hoeft Wednesday, October 3, 2018 8:02 PM
    Tuesday, October 2, 2018 7:55 PM

All replies

  • -Body ($aPrinterList|Out-String)

    \_(ツ)_/

    • Marked as answer by David Hoeft Wednesday, October 3, 2018 8:02 PM
    Monday, October 1, 2018 3:41 PM
  • That WORKED!!! Thanks! How can we format the data so it's output is tabular. Out-String results in this:

    UserName    : HSLibStudent

    TimeCreated : 10/1/2018 10:33:28 AM

    MachineName : LIB05

    PrinterName : Library BW Printer

    PageCount   : 1

    We are looking for columns for each header like the results in the CSV.

    Monday, October 1, 2018 3:57 PM
  • -Body ($aPrinterList | Format-Table | Out-String)

    \_(ツ)_/

    Monday, October 1, 2018 4:00 PM
  • That Worked!!! Now we are trying to get the column data to line up vertically. We added autosize to the format table but that did not provide the result. 

    -Body ($aPrinterList | Format-Table -autosize | Out-String)

    Monday, October 1, 2018 4:57 PM
  • The formatting of plain text messages is completely controlled by the mail climate and cannot be managed by the sender except for the line length.

    To send formatted email you must send it as HTML. "-BodyAsHTML".  Which will force the use an HTML wrapper.  The text can be declared in any way you want it to look.


    \_(ツ)_/

    Monday, October 1, 2018 5:05 PM
  • We added -BodyAsHTML and need help declaring text so it creates columns with headers. There does not seem to be any examples in help to help us get started.
    Tuesday, October 2, 2018 7:33 PM
  • This will fix the text.

    $body = '<pre>' + ($aPrinterList | Format-Table | Out-String) + '</pre>'
    Send-MailMessage -Body $body -BodyAsHtml ….


    \_(ツ)_/

    • Marked as answer by David Hoeft Wednesday, October 3, 2018 8:02 PM
    Tuesday, October 2, 2018 7:55 PM
  • We have a report that works perfectly! Thanks.
    Wednesday, October 3, 2018 8:03 PM