简体   繁体   English

从PowerShell设置时如何处理Windows PATH变量扩展?

[英]How do I handle Windows PATH variable expansion when set from PowerShell?

I am tweaking the PATH on several W2k3 servers in PowerShell to convert certain paths with blank spaces to their 8.3 equivalents. 我在PowerShell中的几个W2k3服务器上调整PATH,将带有空格的某些路径转换为8.3当量。 After several regex transformations, I run the following two commands: 经过几次正则表达式转换后,我运行以下两个命令:

# Set the path for this process
$env:PATH = $path
# Set the path for the Machine
[System.Environment]::SetEnvironmentVariable('PATH', $path,[System.EnvironmentVariableTarget]::Machine) 

After running them, the path is changed in ways I didn't intend. 运行它们之后,路径会以我不想要的方式改变。 %SystemRoot% is uniformly expanded to C:\\Windows. %SystemRoot%统一扩展为C:\\ Windows。 I can't see where this signals the apocalypse, but I'd rather keep the %SystemRoot%, so I fiddled until I got %SystemRoot% to appear in the path again, but when I do the path no longer expands and no longer works. 我无法看到这表示天启的意义,但我宁愿保留%SystemRoot%,所以我摆弄,直到我让%SystemRoot%再次出现在路径中,但是当我做的路径不再扩展而不再作品。 Echoing the path at the CLI returns an unexpanded string (this is wrong) and commands in the SystemRoot can no longer be found. 在CLI处回显路径会返回未展开的字符串(这是错误的),并且无法再找到SystemRoot中的命令。

If I then add a null entry to the Path ";;", without altering any other text in the PATH, it begins to work correctly. 如果我然后在路径“;;”中添加一个空条目,而不改变PATH中的任何其他文本,它开始正常工作。

So my question is how to alter the path programmatically using PowerShell so as not to muck up variable expansion within the path? 所以我的问题是如何使用PowerShell以编程方式改变路径,以免破坏路径中的变量扩展?

As far as I can tell, you can't do this with the [Environment]::SetEnvironmentVariable() method and you can't do this with the Registry provider. 据我所知,您无法使用[Environment]::SetEnvironmentVariable()方法执行此操作,并且您无法使用Registry提供程序执行此操作。 However you can access the system Path env var in the registry using the Microsoft.Win32.RegistryKey class like so: 但是,您可以使用Microsoft.Win32.RegistryKey类访问注册表中的系统Path 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()

This will allow you to save the updated PATH and preserve the variables that appear in the Path value. 这将允许您保存更新的PATH并保留Path值中显示的变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Windows上的环境变量中设置Jenkins ant构建中的build.xml文件的路径 - How do I set the path to the build.xml file in a Jenkins ant build from an environment variable on Windows C#,如何在Windows Powershell(Windows 10)上配置Mono? (环境变量PATH) - C#, How Do I Get Mono Configured on Windows Powershell (Windows 10)? (Environmental Variable PATH) 如何从Perl设置Windows PATH变量? - How can I set the Windows PATH variable from Perl? 如何将Windows Update设置为从不使用PowerShell检查更新? - How do I set Windows Update to never check for updates with PowerShell? 如何在Windows上的Java应用程序中设置/更新PATH变量? - How can I set/update PATH variable from within java application on Windows? ssh到Windows计算机时,如何在登录时自动设置路径? - How do I set the path automatically on login when ssh'ing to a Windows machine? 在PowerShell中,如何在运行脚本之前设置$ HOME变量? - In PowerShell, how do I set $HOME variable before running a script? 当记事本不是默认应用程序时,如何从 Windows 资源管理器运行 PowerShell 脚本? - How do I run a PowerShell script from Windows Explorer when Notepad is not the default application? 如何在Windows上将Ruby添加到PATH变量? - How do I add Ruby to the PATH variable on Windows? 如何在Windows上向Apache PATH变量添加路径? - How do I add paths to the Apache PATH variable on Windows?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM