Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.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设置时,如何处理Windows路径变量扩展?_Windows_Powershell_Environment Variables - Fatal编程技术网

从PowerShell设置时,如何处理Windows路径变量扩展?

从PowerShell设置时,如何处理Windows路径变量扩展?,windows,powershell,environment-variables,Windows,Powershell,Environment Variables,我正在调整PowerShell中几个W2k3服务器上的路径,以将某些带有空格的路径转换为其8.3等效路径。经过几次正则表达式转换后,我运行以下两个命令: # Set the path for this process $env:PATH = $path # Set the path for the Machine [System.Environment]::SetEnvironmentVariable('PATH', $path,[System.EnvironmentVariableTarget

我正在调整PowerShell中几个W2k3服务器上的路径,以将某些带有空格的路径转换为其8.3等效路径。经过几次正则表达式转换后,我运行以下两个命令:

# Set the path for this process
$env:PATH = $path
# Set the path for the Machine
[System.Environment]::SetEnvironmentVariable('PATH', $path,[System.EnvironmentVariableTarget]::Machine) 
运行它们后,路径将以我不希望的方式更改。%SystemRoot%统一扩展为C:\Windows。我看不出这意味着世界末日的到来,但我宁愿保留%SystemRoot%,所以我一直摆弄,直到%SystemRoot%再次出现在路径中,但当我这样做时,路径不再扩展,也不再工作。在CLI上回显路径将返回未展开的字符串(这是错误的),并且无法再在SystemRoot中找到命令

如果我随后向路径“;”添加一个空条目,而不更改路径中的任何其他文本,则它将开始正常工作


因此,我的问题是如何使用PowerShell以编程方式更改路径,从而不破坏路径中的变量扩展

据我所知,您不能使用
[Environment]::SetEnvironmentVariable()
方法执行此操作,也不能使用注册表提供程序执行此操作。但是,您可以使用
Microsoft.Win32.RegistryKey
类访问注册表中的系统路径env var,如下所示:

C:\PS> $key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey('SYSTEM\CurrentControlSet\Control\Session Manager\Environment', $true) C:\PS> $path = $key.GetValue('Path',$null,'DoNotExpandEnvironmentNames') C:\PS> $path ...;%systemroot%\System32\WindowsPowerShell\v1.0\ C:\PS> $key.SetValue('Path', $path + ';%Windir%\Symbols', 'ExpandString') C:\PS> $key.Dispose() C:\PS>$key=[Microsoft.Win32.Registry]::LocalMachine.OpenSubKey('SYSTEM\CurrentControlSet\Control\Session Manager\Environment',$true) C:\PS>$path=$key.GetValue('path',$null,'DoNotExpandEnvironmentNames')) C:\PS>$path ...;%systemroot%\System32\WindowsPowerShell\v1.0\ C:\PS>$key.SetValue('Path',$Path+';%Windir%\Symbols','ExpandString')) C:\PS>$key.Dispose()
这将允许您保存更新的路径并保留路径值中显示的变量。

2注意:1)我确实为每个测试打开了一个新的CLI,2)我从$env:PATH读取的路径已展开,因此,我正在阅读c:\windows并手动将该值更改为%SystemRoot%,然后将该值推回到环境中。当您将
$path
设置为
path
时,
中有什么内容?在每台服务器上都不同,但是…;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SystemRoot%\system32\WindowsPowerShell\v1.0;。。。是所有组件上的一个组件。当我读取$path时,在读取过程中它会扩展到c:\Windows。在设置之前,我必须-I将扩展值与其等效变量一起替换。带变量的$path值不会再次展开,除非我通过属性对话框手动触摸它。太棒了,基思。再次感谢。仅供参考,该脚本可能应该放在try/finally中,然后放在finally中对注册表项执行Dispose。你知道,我差点问起这个问题,但由于我的脚本很短,而且每个变量都会立即消失,所以我没有担心。但是自从你提起这件事以来。NET 4.0包括您调用的手动Dispose()方法,但2.0没有。在没有dispose()方法的情况下,我应该如何处理密钥?在.NET 2.0中,使用Close()方法。您可以在此基础上展开用户路径和系统路径吗?我假设您在这里显示的是系统路径,而不是用户路径。