Help with export-csv (multiple csv files) and sending them via email

Welcome Forums General PowerShell Q&A Help with export-csv (multiple csv files) and sending them via email

This topic contains 2 replies, has 3 voices, and was last updated by

 
Participant
1 month ago.

  • Author
    Posts
  • #122612

    Participant
    Points: 76
    Rank: Member

    Below script exports results into multiple csv files saves them locally, but sends only one csv in the email

    not sure what I'm missing here, appreciate any help

     

    $CSV = $AZVMs  | Export-Csv $OutCSV -NoTypeInformation
    | Send-Mailmessage -SmtpServer $SMTPHost -From $SendingEmail -to $Email -subject ($EmailSubject -join "
    ") -body $Message -BodyAsHtml -Priority High -Credential $credential -Port 587 -Attachments $OutCSV
  • #122619

    Participant
    Points: 1
    Rank: Member

    Hi,

    As per your code snippet, Your $OutCSV variable will only contain a single CSV file. you can try something like below.

    $OutCSV = get-childitem (pathtocsv) -filter *.csv
    Send-Mailmessage -SmtpServer $SMTPHost -From $SendingEmail -to $Email -subject ($EmailSubject -join "") -body $Message -BodyAsHtml -Priority High -Credential $credential -Port 587 -Attachments $OutCSV.Fullname
    
    

     

  • #122646

    Participant
    Points: 305
    Helping Hand
    Rank: Contributor

    Just like the To, Attachments is looking for an array, albeit an array of paths.

    #Everyone that uses splatting saves a baby seal
    $mailParams = @{
        SmtpServer  = $SMTPHost 
        From        = $SendingEmail 
        To          = $Email 
        Subject     = ($EmailSubject -join "") 
        Body        = $Message 
        BodyAsHtml  = $true
        Priority    = 'High'
        Credential  = $credential 
        Port        = 587 
        Attachments = "C:\CSV1.csv", "C:\CSV2.csv"
    }
    
    Send-Mailmessage @mailParams
    

    Even in @shihanpietersz example, I'm not sure Attachments would take a file PSObject, so you may need to only provide the FullName as an array:

    $OutCSV = get-childitem (pathtocsv) -filter *.csv | Select-Object -ExpandProperty FullName
    

You must be logged in to reply to this topic.