簡體   English   中英

括號導致Send-MailMessage-附件出現錯誤

[英]Brackets cause error in Send-MailMessage -Attachment

我正在使用一個腳本,該腳本獲取文件夾中最舊的日志文件,然后將其郵寄給我,然后刪除該文件。

獲取文件名並隨后將其刪除是可行的,但是,當日志文件包含[]括號時,它將無法發送郵件,而該文件仍會被刪除...

我知道方括號是通配符,在嘗試附加它們之前,我需要重命名文件,但是,我不是經驗豐富的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

我還沒有測試過,但是我希望它能在您使用`轉義括號時起作用:

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

接着

Send-MailMessage -Attachments $Attachment 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM