问题描述
||
Windows 2008R2
Powershell v2.0
原始路径(从高级系统设置/环境变量中可以看到)为:
%SystemRoot%\\ system32;%SystemRoot%;%SystemRoot%\\ System32 \\ Wbem;%SYstemROOT%\\ System32 \\ WindowsPowerShell \\ v1.0 \\
在Powershell中,我运行:
@H_502_0@
[Environment]::SetEnvironmentvariable(\"PATH\",\"$($env:path;C:\\Temp\",\"Machine\")要么 @H_502_0@
[Environment]::SetEnvironmentvariable(\"PATH\",\"$($([Environment]::GetEnvironmentvariable(\'PATH\',\'MACHINE\')));C:\\Temp\",\"Machine\")现在,我的路径(从高级系统设置/环境变量中可以看到)是: C:\\ Windows \\ system32; C:\\ Windows; C:\\ Windows \\ System32 \\ Wbem; C:\\ Windows \\ System32 \\ WindowsPowerShell \\ v1.0 \\; C:\\温度 有没有一种方法可以在不评估现有路径的情况下检索现有路径,以便保留在原始路径中嵌入的现有环境变量?
解决方法
有点中世纪,但似乎起作用:
(((reg query \"HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\") |
select-string \"\\s+path\\s+REG_EXPAND_SZ\").line -split \" \")[3]
, 我认为问题在于PS如何获取环境变量。
此S / O帖子可能包含您的答案(希望如此):
PowerShell:获取“ tmp”环境变量原始值
, 试试看(转换为PS后):
string originalPath = Environment.GetEnvironmentVariable(\"PATH\");
string path = originalPath + \";\" + \"NEW_PATH_BIT\";
Environment.SetEnvironmentVariable(\"PATH\",path);
, 我不确定是否有-我尝试过:
PS C:\\> $string1 = \" World\"
PS C:\\> $string2 = \"Hello$string1\"
PS C:\\> $string2
Hello World
看看它是否与环境变量或其他有关。
我想这是在命令行上进行编译的方式。在评估整个表达式之前,先评估所有变量。因此最终结果不知道需要花多少功夫。