在PowerShell中的变量使用通配符

问题描述 投票:0回答:1

我有了每周报告的文件夹。每个报告都有创造的名称,例如“报告-A_2_05_19.pdf”之日起,“报告-B_2_05_19.pdf”等等,我想这些,但由于在每周报告的名称日期变更创建变量我试图做到这一点:

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

当我这样做,并尝试使用它只是打印到屏幕的外卡打开的报告:

C:\报告\报告-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
powershell variables wildcard
1个回答
1
投票

如果你看一下Google文档Send-MailMessage你会看到,-Attachments不支持通配符

类型:String []

别名:PsPath

位置:命名

默认值:无

接受管道输入:TRUE(根据值)

接受通配符:假

所以,你可以做的反而是纳入Resolve-Path这也从外推弦通配符路径。

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

虽然小心,这可能匹配多于你意。您可能需要您将附加文件之前验证结果。


提供大量的参数和值时,我也建议splatting

$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
© www.soinside.com 2019 - 2024. All rights reserved.