2015-10-15 75 views
1

我正在研究一个脚本,它获取文件夹中最旧的日志文件,将它邮寄给我,然后删除文件。方括号导致Send-MailMessage中的错误 - 附件

获取文件名并删除它之后的作品,但是,当一个日志文件中包含括号[]中,它无法发送邮件,而文件仍然被删除...

我知道括号是通配符和我需要重新命名文件才尝试附加它们,但是,我不是一个有经验的Powershell脚本编写人员,无法使用我的代码处理示例...有人可以帮助我前进吗?

我的代码:

$Item = Get-ChildItem -Path "C:\backup log" -filter "*.log" | Sort CreationTime | select -First 1 
Send-MailMessage -to "Jason <[email protected]>" -from "Backupmaster <[email protected]>" -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

我没有测试过这一点,但我希望它,如果你与`逃避括号工作:

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

然后

Send-MailMessage -Attachments $Attachment 
+0

那么简单是吧? :) 非常感谢! – user3113241

+0

谢谢,解决这个问题的简单方法。 – JaredStroeb