我正在使用PowerShell脚本发送电子邮件,并且某些参数可能包含特殊字符,例如%和&.
powershell.exe .\sendmail.ps1 -to "user@something.com" -body "Special character & causing trouble." -subject 'PowerShell email'
Run Code Online (Sandbox Code Playgroud)
这会出现以下错误:
Ampersand not allowed. The & operator is reserved for future use; use "&" to pass ampersand as a string.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : AmpersandNotAllowed
Run Code Online (Sandbox Code Playgroud)
我试过用`来逃避&.但该参数仍未正确包含在脚本中.
我正在寻找一种方法来确保将参数正确传递给脚本,包括特殊字符.我正在考虑使用encodedcommand,或者可能将参数放在xml文件中.
任何建议都是最受欢迎的.
这是我正在使用的脚本:
param (
[string]$body = "",
[string]$subject = $(throw "-subject is required."),
[string]$attachment = "",
[string]$to = ""
)
Try
{
$o = [Runtime.InteropServices.Marshal]::GetActiveObject("Outlook.Application")
}
Catch
{
$o = New-Object -com Outlook.Application
}
$mail = $o.CreateItem(0)
$mail.subject = "Auto Build Test"
$mail.body = $body
#for multiple email, use semi-colon ; to separate
$mail.To = $to
$mail.Attachments.Add($attachment)
$mail.Display(0)
Run Code Online (Sandbox Code Playgroud)
CB.*_*CB. 10
尝试以这种方式调用脚本并让我知道:
powershell.exe -command "& .\sendmail.ps1 -to user@something.com -body 'Special character & causing trouble.' -subject 'PowerShell email'"
Run Code Online (Sandbox Code Playgroud)
简单地使用'而不是"禁用扩展
powershell.exe .\sendmail.ps1 -to "user@something.com" -body 'Special character & causing trouble.' -subject 'PowerShell email'
| 归档时间: |
|
| 查看次数: |
20055 次 |
| 最近记录: |