我正试图使用以下命令检查Windows SMTP服务器的RelayForAuth设置。Powershell似乎显示正确的结果"False",但当通过命令提示符运行相同的命令时,它会生成一个错误:
Powershell示例:
([ADSI]"IIS://localhost/smtpsvc/1".RelayForAuth -like "*0*")
输出:
False
命令提示符示例:
powershell -command "([ADSI]"IIS://localhost/smtpsvc/1".RelayForAuth -like "*0*")"
输出:
At line:1 char:8
+ ([ADSI]IIS://localhost/smtpsvc/1.RelayForAuth -like *0*)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token 'IIS://localhost/smtpsvc/1.RelayForAuth' in expression or
statement.
At line:1 char:8
+ ([ADSI]IIS://localhost/smtpsvc/1.RelayForAuth -like *0*)
+ ~
Missing closing ')' in expression.
At line:1 char:56
+ ([ADSI]IIS://localhost/smtpsvc/1.RelayForAuth -like *0*)
+ ~
Unexpected token ')' in expression or statement.
+ CategoryInfo : ParserError: (:) [],
ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
因为您正在嵌套(嵌入("
字符-要将逐字传递给PowerShell-在语法外部双引号("..."
(内,必须转义那些嵌套的"
字符。
即使PowerShell内部的`
用作转义符,从外部调用PowerShell CLI(powershell.exe
/pwsh
((cmd.exe
(也需要对
"
进行转义:
# Embedded " chars. must be -escaped
powershell -command "([ADSI]"IIS://localhost/smtpsvc/1").RelayForAuth -like "*0*""
请注意,如果对整个"..."
字符串内的字符串进行单引号,则可以避免此转义。虽然这在您的情况下很好,但考虑到您的字符串只有逐字逐句的内容,请注意,这通常只是如果不需要字符串插值时的一个选项:
# Embedded strings use '...' -> no escaping needed.
powershell -command "([ADSI]'IIS://localhost/smtpsvc/1').RelayForAuth -like '*0*'"
注意事项:使用单引号将整个命令('...'
(括起来会使无法按cmd.exe
的预期工作:后者不会将这些内容识别为引号,PowerShell只是将字符串解释为使用其语法作为逐字字符串,因此只打印字符串的内容。
有关更多信息,请参阅此答案。