如何在不扩展令牌的情况下获取 PATH 环境变量的值?
Posted
技术标签:
【中文标题】如何在不扩展令牌的情况下获取 PATH 环境变量的值?【英文标题】:How to get the value of the PATH environment variable without expanding tokens? 【发布时间】:2015-10-11 09:10:38 【问题描述】:我正在编写一个 PowerShell 脚本来将文件夹名称转换为 PATH 环境变量中的短名称并保存更改。它适用于显式路径,但它会扩展标记,因此当我保存更改时,标记将替换为显式路径。我想保留令牌。
例如,如果我的 PATH 是这样的:%SystemRoot%;%SystemRoot%\system32;C:\Program Files\TortoiseSVN\bin;C:\ProgramData\chocolatey\bin
我想要这个结果:%SystemRoot%;%SystemRoot%\system32;C:\PROGRA~1\TORTOI~1\bin;C:\PROGRA~3\CHOCOL~1\bin
但是我得到了这个:C:\Windows;C:\Windows\system32;C:\PROGRA~1\TORTOI~1\bin;C:\PROGRA~3\CHOCOL~1\bin
这是说明问题的完整脚本。
# get the current path.
# I ended up not using either of these approaches because they appear to
# be contextual; on my system, at least, they include paths that aren't
# seen when I view the PATH value from the System Properties -> Environment
# Variables dialog box.
$current_path = $env:Path
$current_path = [System.Environment]::GetEnvironmentVariable("Path")
# Instead, I get the PATH value directly from the registry, like so:
$current_path = (Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' -Name Path).Path
# The problem is that PowerShell expands the tokens even when reading from the
# registry, so I can't detect the tokens and ignore them. What I really want
# is just the literal string value, with no token evaluation.
# Here's a sample value; this is what I see in regedt32 and the system dialogs,
# but it's not what I get from the line above.
$current_path = '%SystemRoot%;%SystemRoot%\system32;C:\Program Files\TortoiseSVN\bin;C:\ProgramData\chocolatey\bin'
$new_path = ''
# the FileSystemObject has a method to convert the path using short names for the folders.
$fso = New-Object -ComObject Scripting.FileSystemObject
# Individual paths are delimited by a semicolon. Skip paths with tokens,
# and preserve trailing slashes in each path.
$current_path.Split(';') |
ForEach-Object
if ($_.StartsWith('%'))
$_
elseif ($_.EndsWith('\'))
"$($fso.GetFolder($_).ShortPath)\"
else
"$($fso.GetFolder($_).ShortPath)"
|
ForEach-Object $new_path += "$_;"
# remove the extra semicolon from the end the new PATH.
$new_path = $new_path.TrimEnd(";")
"Current PATH length: $($current_path.Length)"
"New PATH length: $($new_path.Length)"
$new_path
# commented out so you don't accidentally update your path if you try out this script
#[System.Environment]::SetEnvironmentVariable("Path", $new_path, "Machine")
如果我可以从注册表中获取文字字符串值,似乎应该很容易,但到目前为止我还没有弄清楚如何做到这一点。
【问题讨论】:
【参考方案1】:这不是 PowerShell 本身,而是底层 Windows 注册表的“功能”。 Path 变量的类型为 REG_EXPAND_SZ,它会在检索时自动扩展环境变量。我认为您无法使用内置 cmdlet 解决它,但您可以使用 .NET Microsoft.Win32.Registry
API。将RegistryKey.GetValue
重载与RegistryValueOptions.DoNotExpandEnvironmentNames
一起使用:
$regKey = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey('SYSTEM\CurrentControlSet\Control\Session Manager\Environment', $true)
$regKey.GetValue('Path', $null, "DoNotExpandEnvironmentNames")
当需要保存变量时,使用SetValue
和RegistryValueKind.ExpandString
的重载以正确的类型保存它:
$regKey.SetValue("Path", $new_path, "ExpandString")
【讨论】:
我必须在调用 OpenSubkey() 时添加一个参数,就像OpenSubKey('SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'ReadWriteSubTree')
,然后它才能让我使用 SetValue() 写回注册表,但除此之外,这正是我所做的正在寻找。非常感谢!
@Matt 哦,是的,默认情况下,注册表项是不可写的。您需要调用该重载或带布尔值的重载(如我的编辑中所示)。
使用您的答案创建了一个自动幂等添加未扩展路径的 powershell 脚本:gist.github.com/CMCDragonkai/a02d77c2d7c0799dd42fd2aab26a3cd5【参考方案2】:
不使用 .NET [Microsoft.Win32.Registry]
类型:用户和系统路径条目的单行。
(Get-Item -path "HKCU:\Environment" ).GetValue('Path', '', 'DoNotExpandEnvironmentNames')
(Get-Item -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" ).
GetValue('Path', '', 'DoNotExpandEnvironmentNames')
【讨论】:
以上是关于如何在不扩展令牌的情况下获取 PATH 环境变量的值?的主要内容,如果未能解决你的问题,请参考以下文章
System.load() 方法在不设置 LD_LIBRARY_PATH 环境变量的情况下不加载共享库
如何在不登录的情况下从 Instagram 获取 oauth 2 访问令牌(隐式流程)?
如何在不请求通知权限的情况下获取 Firebase 云消息传递的注册令牌?
SignInAsAuthenticationType 是不是允许我在不覆盖现有声明的情况下获取 OAuth 令牌?