【问题标题】:Brackets cause error in Send-MailMessage -Attachment括号导致 Send-MailMessage -Attachment 中的错误
【发布时间】:2015-10-15 12:28:45
【问题描述】:

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

获取文件名并在之后删除它是可行的,但是当日志文件包含 [ ] 括号时,发送邮件失败,而文件仍然被删除...

我知道括号是通配符,我需要在尝试附加文件之前重命名文件,但是,我不是经验丰富的 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)

-杰森

【问题讨论】:

    标签: email powershell wildcard attachment brackets


    【解决方案1】:

    我尚未对此进行测试,但如果您使用 ` 转义括号,我希望它可以工作:

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

    然后

    Send-MailMessage -Attachments $Attachment 
    

    【讨论】:

    • 这么简单吧? :) 非常感谢!
    • 感谢您对这个问题的简单解决方案。
    猜你喜欢
    • 2017-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多