Hello,
in my Powershell script some logs are detected to be wrong, hence I want them to be sent via email. Every time the script runs the list of logs can be different, so I store the log names in an array. Then I create a long comma separated string with file names and paste the output string after "-Attachment" when sending an email.
However Powershell detects the comma separated string as a single filename / filepath and throws an error. I could overcome the problem by writing single filenames separated with comma, but it does not fit in my script because filenames and number of files can be different every run of the script.
Here is my code, rn ... relative name (path), fn ... full name (path)
$rn1="imp_qn03.log"
$rn2="imp_ssp1.log"
$rn3="imp_ssw2.log"
$rn4="imp_t2ns01.log"
$rn_all=$rn1+','+$rn2+','+$rn3+','+$rn4
$base_dir='D:\TaskScheduler\PadrSQL\ZMENY'
$fn1=$base_dir+'\'+$rn1
$fn2=$base_dir+'\'+$rn2
$fn3=$base_dir+'\'+$rn3
$fn4=$base_dir+'\'+$rn4
$fn_all=$fn1+','+$fn2+','+$fn3+','+$fn4
Write-Debug "rn_all=$rn_all"
Write-Debug "fn_all=$fn_all"
# Email 1
Send-MailMessage -To $to -From $from -Subject "more variables after Attachment rn1..rn4" -Body $body -SmtpServer $smtp_server -Port 25 -BodyAsHtml -Priority Normal -Attachments $rn1,$rn2,$rn3,$rn4
# Email 2
Send-MailMessage -To $to -From $from -Subject "single variable after Attachment rn_all" -Body $body -SmtpServer $smtp_server -Port 25 -BodyAsHtml -Priority Normal -Attachments $rn_all
# Email 3
Send-MailMessage -To $to -From $from -Subject "more variables after Attachment fn1..fn4" -Body $body -SmtpServer $smtp_server -Port 25 -BodyAsHtml -Priority Normal -Attachments $fn1,$fn2,$fn3,$fn4
# Email 4
Send-MailMessage -To $to -From $from -Subject "single variable after Attachment fn_all" -Body $body -SmtpServer $smtp_server -Port 25 -BodyAsHtml -Priority Normal -Attachments $fn_all
# debug Error email 2 with DEBUG info
DEBUG: rn_all=imp_qn03.log,imp_ssp1.log,imp_ssw2.log,imp_t2ns01.log
DEBUG: fn_all=D:\TaskScheduler\PadrSQL\ZMENY\imp_qn03.log,D:\TaskScheduler\PadrSQL\ZMENY\imp_ssp1.log,D:\TaskScheduler\PadrSQL\ZMENY\imp_ssw2.log,D:\TaskScheduler\PadrSQL\ZMENY\imp_t2ns01.log
Send-MailMessage : Could not find file 'D:\TaskScheduler\PadrSQL\ZMENY\imp_qn03.log,imp_ssp1.log,imp_ssw2.log,imp_t2ns01.log'.
At D:\TaskScheduler\PadrSQL\kontrola_logu1.ps1:210 char:5
+ Send-MailMessage -To $to -From $from -Subject "single variable af ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Send-MailMessage], FileNotFoundException
+ FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.SendMailMessage
# Error email 4 with DEBUG info
DEBUG: rn_all=imp_qn03.log,imp_ssp1.log,imp_ssw2.log,imp_t2ns01.log
DEBUG: fn_all=D:\TaskScheduler\PadrSQL\ZMENY\imp_qn03.log,D:\TaskScheduler\PadrSQL\ZMENY\imp_ssp1.log,D:\TaskScheduler\PadrSQL\ZMENY\imp_ssw2.log,D:\TaskScheduler\PadrSQL\ZMENY\imp_t2ns01.log
Send-MailMessage : The given path's format is not supported.
At D:\TaskScheduler\PadrSQL\kontrola_logu1.ps1:212 char:5
+ Send-MailMessage -To $to -From $from -Subject "single variable af ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Send-MailMessage], NotSupportedException
+ FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.SendMailMessage
Email 1 and Email 3 were sent and delivered without problems.
Email 2 error message reveals PowerShell prefixed the whole string with current directory
Email 4 error message does not reveal the problem, it only complains about wrong path format.
Update 1:
Though I mentioned array in the text above, it was not used in mentioned code block, because the root of the problem could be shown with string variables rn1... rn4, fn1...fn4. In my original script I select wrong logs with [Regex]::Matches into variable $rn_files. It is a list of filenames separated by space. Then I create an array $rn_array from $rn_files with Split. Then I form variable $attachment by concatenating array items together with comma as delimiter. However variable $attachment cannot be used after -Attachments when sending emails.
$rn_files=[Regex]::Matches((Select-String -Path *.log -Pattern $regex),'(imp_[A-Z a-z 0-9 _]*.log):[0-9]*:[0-3][0-9].[0-1][0-9].20[2-9][0-9]-[0-9]*:[0-5][0-9]:[0-5][0-9] import with errors')|ForEach-Object {$_.Groups[1].Value}
$rn_array=$rn_files.Split(" ")
$attachment=$rn_array -join ','
...
Send-MailMessage -To ... -Attachments $attachment #throws an error
My goal is to find a way how to use $rn_files or its derivates (e.g. array $rn_array) to send multiple logs as an attachment.
Update 2
Passing filenames with pipe does not solve the problem.
# No error
$rn1,$rn2 | Send-MailMessage -To $to -From $from -Subject "more variables after Attachment rn1..rn2" -Body $body-SmtpServer $smtp_server -Port 25 -BodyAsHtml -Priority Normal
# An error is thrown
$rn_all | Send-MailMessage -To $to -From $from -Subject "single variable after Attachment rn_all" -Body $body -SmtpServer $smtp_server -Port 25 -BodyAsHtml -Priority Normal
# Error content
Send-MailMessage : Could not find file 'D:\TaskScheduler\PadrSQL\ZMENY\imp_qn03.log,imp_ssp1.log,imp_ssw2.log,imp_t2ns01.log'.
At D:\TaskScheduler\PadrSQL\kontrola_logu.ps1:210 char:22
+ [array]$rn_all | Send-MailMessage -To $to -From $from -Subject "s ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Send-MailMessage], FileNotFoundException
+ FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.SendMailMessage
Solution
Send-MailMessage accepts array of strings, not a single string. Here is a functional code:
# Get a single string of filenames separated with space
$rn_files=[Regex]::Matches((Select-String -Path *.log -Pattern $regex),'(imp_[A-Z a-z 0-9 _]*.log):[0-9]*:[0-3][0-9].[0-1][0-9].20[2-9][0-9]-[0-9]*:[0-5][0-9]:[0-5][0-9] import with errors')|ForEach-Object {$_.Groups[1].Value}
# Create an array of strings using space as delimiter for array items - each item of array contains one filename
$rn_array=$rn_files.Split(" ")
# Send email with attachments
Send-MailMessage ... -Attachments $rn_array
Leos