【发布时间】:2017-05-18 03:02:49
【问题描述】:
我花了一些时间找出 Powershell 脚本的正确语法。但是最后它是反复试验的方法,我想知道为什么下面的语法不起作用。
脚本以提升模式启动新的 Powershel 并设置环境变量。摘录如下:
$x = "NewValue"
$arguments = "-NoExit", "-command", "&{ [Environment]::SetEnvironmentVariable(`"MyVar1`", `"$x`", [EnvironmentVariableTarget]::Machine) }"
Start-Process powershell -Verb runAs -ArgumentList $arguments
如果我只是打印出变量$arguments,它就像我所期望的那样是一个数组:
-NoExit
-command
&{ [Environment]::SetEnvironmentVariable("MyVar1", "NewValue", [EnvironmentVariableTarget]::Machine) }
但是,在子 Powershell 中,双引号以某种方式被吃掉并且丢失了。为什么?这是预期的行为吗?它输出:
At line:1 char:42
+ &{ [Environment]::SetEnvironmentVariable(MyVar1, NewValue, [EnvironmentVariableT ...
+ ~
Missing ')' in method call.
At line:1 char:42
+ &{ [Environment]::SetEnvironmentVariable(MyVar1, NewValue, [EnvironmentVariableT ...
+ ~~~~~~
Unexpected token 'MyVar1' in expression or statement.
At line:1 char:48
+ &{ [Environment]::SetEnvironmentVariable(MyVar1, NewValue, [EnvironmentVariableT ...
+ ~
Missing argument in parameter list.
At line:1 char:2
+ &{ [Environment]::SetEnvironmentVariable(MyVar1, NewValue, [EnvironmentVariableT ...
+ ~
Missing closing '}' in statement block.
At line:1 char:96
+ ... arget]::Machine) }
+ ~
Unexpected token ')' in expression or statement.
At line:1 char:98
+ ... get]::Machine) }
+ ~
Unexpected token '}' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndParenthesisInMethodCall
我的环境:
> $PSVersionTable
Name Value
---- -----
PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.42000
BuildVersion 6.3.9600.17400
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2
================================================ ================
作为参考,这里是使用单引号而不是双引号的工作版本(我还删除了 -NoExit 参数,该参数仅用于调试):
$x = "NewValue"
$arguments = "-command", "&{ [Environment]::SetEnvironmentVariable('MyVar1', `'$x`', [EnvironmentVariableTarget]::Machine) }"
Start-Process powershell -Verb runAs -ArgumentList $arguments
【问题讨论】:
-
$arguments = "-NoExit", "-command", "`"&{ [Environment]::SetEnvironmentVariable(\`"MyVar1\`", \`"$x\`", [EnvironmentVariableTarget]::Machine) }`"" -
@PetSerAl 你为什么要回答?作为 cmets,这很令人困惑。
-
@JPBlanc 恕我直言,它需要不仅仅是一个代码 sn-p 才能成为正确的答案。
-
您可以将您的代码作为答案,稍后再编辑您的答案。
标签: powershell