括号在Send-MailMessage -Attachment中导致错误

时间:2015-10-15 12:28:46

标签: email powershell wildcard attachment brackets

我正在处理一个脚本,该脚本获取文件夹中最旧的日志文件,将其邮寄给我,然后删除该文件。

获取文件名并在之后删除它,但是,当日志文件包含[]括号时,它会无法发送邮件,而文件仍会被删除...

我知道括号是通配符,我需要在尝试附加文件之前重命名这些文件但是,我不是一个经验丰富的Powershell脚本编写者,无法使用我的代码获取示例...有人可以帮助我?

我的代码:

$Item = Get-ChildItem -Path "C:\backup log" -filter "*.log" | Sort CreationTime | select -First 1
Send-MailMessage -to "Jason <jason@company.com>" -from "Backupmaster <backupmaster@company.net>" -Subject "sync log: $($Item.Name)" -SmtpServer "172.24.1.x" -body "Attached is the sync log.`nFilename: $($Item.Name). `nNote that the oldest log is sent first, newer logs may arrive later.`nThis log will be deleted from the server after sending.`n`n-Backupmaster" -attachments "$($Item.FullName)"
Remove-Item $($Item.FullName)

-Jason

1 个答案:

答案 0 :(得分:1)

我没有对此进行测试,但如果您使用`转义括号,我希望它能够正常工作:

$Attachment = $Item.FullName -replace "(\[|\])",'`$1'

然后

Send-MailMessage -Attachments $Attachment