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

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 "<br>") -body $Message -BodyAsHtml -Priority High -Credential $credential -Port 587 -Attachments $OutCSV

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

 

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