Powershell:命令通过启动过程失败

时间:2014-06-23 14:46:01

标签: powershell start-process

首先,我分配这个字符串变量:

PS> $newpath="C:\Program Files (x86)\Intel\iCLS Client"

这有效:

PS> [Environment]::SetEnvironmentVariable('Path', $newpath, 'Machine')

这不是:

PS> Start-Process Powershell -ArgumentList "-NoExit -noprofile -noninteractive -command [Environment]::SetEnvironmentVariable('Path', $newpath, 'Machine')" -Wait

第二个例子返回:

At line:1 char:46
+ [Environment]::SetEnvironmentVariable('Path', C:\Program Files (x86)\Intel\iCLS  ...
+                                              ~
Missing expression after ','.
At line:1 char:47
+ [Environment]::SetEnvironmentVariable('Path', C:\Program Files (x86)\Intel\iCLS  ...
+                                               ~~~~~~~~~~
Unexpected token 'C:\Program' in expression or statement.
At line:1 char:98
+ ... ient, 'Machine')
+                    ~
Unexpected token ')' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingExpressionAfterToken

我做错了什么?我认为它与字符串,空格或引号有关......

THX

1 个答案:

答案 0 :(得分:0)

你是对的。因为您的路径包含空格,所以必须引用它。这应该有效:

Start-Process Powershell -ArgumentList "-NoExit -noprofile -noninteractive -command [Environment]::SetEnvironmentVariable('Path', '$newpath', 'Machine')" -Wait