Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
环境变量,PowerShell会话与从CMD启动的会话_Powershell_Variables_Cmd_Environment - Fatal编程技术网

环境变量,PowerShell会话与从CMD启动的会话

环境变量,PowerShell会话与从CMD启动的会话,powershell,variables,cmd,environment,Powershell,Variables,Cmd,Environment,我正在尝试从命令提示符窗口(以管理员身份运行)中运行PowerShell命令,但失败。而当我在PowerShell窗口中运行相同的命令时,它运行良好 以下是PowerShell窗口中无错误的命令: Powershell [Environment]::SetEnvironmentVariable("HostIPv4", "192.168.255.14:", "Machine") C:\test>powershell [Environment]::SetEnvironmentVariable

我正在尝试从命令提示符窗口(以管理员身份运行)中运行PowerShell命令,但失败。而当我在PowerShell窗口中运行相同的命令时,它运行良好

以下是PowerShell窗口中无错误的命令:

Powershell [Environment]::SetEnvironmentVariable("HostIPv4", "192.168.255.14:", "Machine")
C:\test>powershell [Environment]::SetEnvironmentVariable("HostIPv4", "192.168.255.14:", "Machine")
At line:1 char:39
+ [Environment]::SetEnvironmentVariable(HostIPv4, 192.168.255.14:, Mach ...
+                                       ~
Missing ')' in method call.
At line:1 char:39
+ [Environment]::SetEnvironmentVariable(HostIPv4, 192.168.255.14:, Mach ...
+                                       ~~~~~~~~
Unexpected token 'HostIPv4' in expression or statement.
At line:1 char:47
+ [Environment]::SetEnvironmentVariable(HostIPv4, 192.168.255.14:, Mach ...
+                                               ~
Missing argument in parameter list.
At line:1 char:73
+ ... ironment]::SetEnvironmentVariable(HostIPv4, 192.168.255.14:, Machine)
+                                                                         ~
Unexpected token ')' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndParenthesisInMethodCall
在命令提示窗口中失败:

Powershell [Environment]::SetEnvironmentVariable("HostIPv4", "192.168.255.14:", "Machine")
C:\test>powershell [Environment]::SetEnvironmentVariable("HostIPv4", "192.168.255.14:", "Machine")
At line:1 char:39
+ [Environment]::SetEnvironmentVariable(HostIPv4, 192.168.255.14:, Mach ...
+                                       ~
Missing ')' in method call.
At line:1 char:39
+ [Environment]::SetEnvironmentVariable(HostIPv4, 192.168.255.14:, Mach ...
+                                       ~~~~~~~~
Unexpected token 'HostIPv4' in expression or statement.
At line:1 char:47
+ [Environment]::SetEnvironmentVariable(HostIPv4, 192.168.255.14:, Mach ...
+                                               ~
Missing argument in parameter list.
At line:1 char:73
+ ... ironment]::SetEnvironmentVariable(HostIPv4, 192.168.255.14:, Machine)
+                                                                         ~
Unexpected token ')' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndParenthesisInMethodCall

可能是什么问题?

PowerShell的命令行解析会删除双引号,请尝试使用单引号:

powershell [Environment]::SetEnvironmentVariable('HostIPv4', '192.168.255.14:', 'Machine')
还请注意,您必须重新打开一个新的进程窗口以查看结果(这是命令shell的已知行为,另请参见:)

解释了问题并起作用,但我建议采用通常更稳健的方法从
cmd.exe
调用PowerShell命令:

  • 明确使用
    -Command
    ,因为在PSv6中,默认值将更改为
    -File
    ,需要脚本文件名而不是命令

  • 使用
    -NoProfile
    ,以避免不必要地加载PowerShell配置文件,并获得更可预测的执行环境

  • 双引号引用整个PowerShell命令,以通过
    cmd.exe
    保护它不受可能不需要的预先解释的影响


在命令字符串中使用
而不是
是一种简单的方法,可以避免转义嵌入的
字符,这在这里很好,但是如果您确实需要嵌入的
(用于插入字符串),请将它们转义为
(sic)或
(sic).

谢谢你,Iron,你的回答。如此简单有效-有点尴尬,我自己都没有想到。它是有效的!谢谢你,mklement0,我会研究你的建议。我眼前的问题解决了。再次感谢。感谢你的反馈。请允许我给你给新来者的标准建议:如果答案解决了你的问题lem,请单击大复选标记接受它(✓) 在它旁边,也可以选择向上投票(向上投票需要至少15个信誉点)。如果您发现其他答案有帮助,请向上投票。接受(为此您将获得2个信誉点)向上投票有助于未来的读者。请看。是否有理由使用PowerShell而不是
SETX
?嗯,我知道我需要它的目标机器始终有PowerShell可用。PS不必在机器上获得批准。SETX必须获得批准才能使用。