【发布时间】:2013-09-16 18:06:12
【问题描述】:
我正在编写一个用于发送电子邮件的 PowerShell 脚本,其中一些参数可能包含特殊字符,例如 % 和 &。
powershell.exe .\sendmail.ps1 -to "user@something.com" -body "Special character & causing trouble." -subject 'PowerShell email'
这会产生以下错误:
Ampersand not allowed. The & operator is reserved for future use; use "&" to pass ampersand as a string.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : AmpersandNotAllowed
我尝试使用 ` 转义 &。但该参数仍不会正确包含在脚本中。
我正在寻找一种方法来确保将参数正确传递给脚本,包括特殊字符。我正在考虑使用编码命令,或者可能将参数放在 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)
【问题讨论】:
标签: powershell