gpt4 book ai didi

具有特殊字符的PowerShell参数

转载 作者:行者123 更新时间:2023-12-04 22:12:58 24 4
gpt4 key购买 nike

我正在使用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

我试过用`转义&。但是该参数仍不会正确包含在脚本中。

我正在寻找一种方法来确保将参数(包括特殊字符)正确传递给脚本。我正在考虑使用encodecommand,或者可能将参数放置在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.exe -command "& .\sendmail.ps1 -to user@something.com -body 'Special character & causing trouble.' -subject 'PowerShell email'"

关于具有特殊字符的PowerShell参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18824205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com