I am coding a small script used to send emails. It accepts 3 parameters, subject, HTML body & recipient.
HTML body is being read from a file to a string. Troubles come in when HTML code contains special characters, quotes, etc., although HTML is encoded before being passed to the script. Powershell throws this error:
"The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to pass it as part of a string."
test.ps1
$theBody = Get-Content ".\welcomeMessageP1.htm" ## HTML !!!
$encodedBody = [System.Net.WebUtility]::HtmlEncode($theBody)
$command = ".\sendmail.ps1 –subject 'test email' –body '$encodedBody' -recipient 'someuser@mydomain.com' "
Invoke-Expression $command
答案 0 :(得分:1)
Invoke-Expression
is considered harmful。不要使用它。无论如何,您都不需要它。只需按原样运行命令行(当然要减去变量$encodedBody
周围的引号)。
$theBody = Get-Content '.\welcomeMessageP1.htm'
$encodedBody = [Net.WebUtility]::HtmlEncode($theBody)
.\sendmail.ps1 –subject 'test email' –body $encodedBody -recipient 'someuser@mydomain.com'