0

I have a folder that has weekly reports. Each report has the date of creation in the name example "report-A_2_05_19.pdf", "report-B_2_05_19.pdf", etc. I would like to create variables for these but since the date changes in the name of the reports each week I was trying to do this:

$rA = "c:\reports\report-A*.pdf"
$rb = "C:\reports\report-B*.pdf

When I do this and try to open the report using the wild cards it just prints to the screen:

c:\reports\report-A*.pdf

$pw = Get-Content C:\MailPW.txt | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PSCredential [email protected], $pw
Send-MailMessage -To [email protected] -from [email protected] -Subject "Attachments" -Body "Attachments." -attachments $rA, $rB -Smtpserver mail.domain.com -UseSsl -credential $cred
3
  • See How to Ask. Edit the question and show your minimal reproducible example and some example output showing the problem. Commented Feb 5, 2019 at 16:01
  • I am confused by the question.Like where do you use the variables $rA and $rb Commented Feb 5, 2019 at 16:19
  • $pw = Get-Content C:\MailPW.txt | ConvertTo-SecureString;$cred = New-Object System.Management.Automation.PSCredential [email protected], $pw; Send-MailMessage -To [email protected] -from [email protected] -Subject "Attachments" -Body "Attachments." -attachments $rA, $rB -Smtpserver mail.domain.com -UseSsl -credential $cred; Commented Feb 5, 2019 at 16:34

1 Answer 1

2

If you look at the Docs for Send-MailMessage you will see that -Attachments does not support wildcards

Type: String[]

Aliases: PsPath

Position: Named

Default value: None

Accept pipeline input: True (ByValue)

Accept wildcard characters: False

So what you could do instead is incorporate Resolve-Path which does extrapolate paths from wildcards strings.

Send-MailMessage .... -attachments (Resolve-Path $rA, $rB).Path

Beware though that this could match more than you intended. You might need to validate the results before you attach the files.


I would also recommend splatting when provided large amounts of parameters and values.

$sendMailMessageParameters = @{
    To          = "[email protected]"
    from        = "[email protected]" 
    Subject     = "Attachments" 
    Body        = "Attachments." 
    attachments = (Resolve-Path $rA, $rB).Path
    Smtpserver  = "mail.domain.com "
    UseSsl      = $true
    credential  = $cred
}

Send-MailMessage @sendMailMessageParameters
Sign up to request clarification or add additional context in comments.

2 Comments

Actually I ended up not using the Variables and just used the file path of the report in the -attachments with the wild card and that worked. My issue is not with the send-mailmessage but trying to get a wildcard to work in a variable.
@irishombian I don't understand then. Do you want PowerShell to interpret your string as a file path outright? You need to tell PowerShell how to deal with that. Get-Item would also work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.