2015-07-21 44 views
5

我正在编写一个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") 

现在看来似乎应该是很容易,如果我能先手从注册表字符串值,但我至今一直无法弄清楚如何做到这一点。

回答

6

这不是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") 
+0

我不得不添加参数到OpenSubkey()的调用,就像'OpenSubKey('SYSTEM \ CurrentControlSet \ Control \ Session Manager \ Environment','ReadWriteSubTree')',在它让我使用SetValue()写回注册表之前,正是我所期待的。非常感谢! – Matt

+0

@Matt哦是的默认情况下,注册表项不可写。你需要调用那个重载或者需要一个bool的那个(就像在我的编辑中那样)。 –

+0

用你的答案来创建一个powershell脚本,自动添加未扩展路径的幂等:https://gist.github.com/CMCDragonkai/a02d77c2d7c0799dd42fd2aab26a3cd5 – CMCDragonkai

相关问题