fix(env): Avoid automatic expansion of %% in env#5395
fix(env): Avoid automatic expansion of %% in env#5395niheaven merged 11 commits intoScoopInstaller:developfrom
%% in env#5395Conversation
Use the Registry to get the unexpanded environment variable string inspired by PowerShell/PowerShell#17518
On the one hand, it remains the same style as the `Get-Item`. On the other hand, this modification is necessary due to the `REG_EXPAND_SZ` type.
|
Probably fixes #5059 and with this implemented we should look into using dedicated It should also be fixed in https://github.com/ScoopInstaller/Install as well |
Thank you for pointing out the related issue for me. I haven't found it after searching for a long time, so I specially raised a new issue. I don't know what other unrelated functions my modification will affect. Perhaps more detailed testing is needed before it can be applied to ScoopInstaller/Install or used to implement |
Use `OpenSubKey('Environment', $true)` to gain write access.
Do not expand environment names when getting env, use expand string when setting env.
% substitutions in env% substitutions in env
Try to get the value before getting the new type, if successful then get the type, otherwise the default type is `ExpandString`.
|
LGTM! Please update the changelog. |
% substitutions in env%% in env
|
So we need this change in the Installer as well - https://github.com/ScoopInstaller/Install Similar to |
Modified in ScoopInstaller/Install#44 |
|
If
function env($name, $global, $val = '__get') {
$RegisterKey = if ($global) {
Get-Item -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager'
} else {
Get-Item -Path 'HKCU:'
}
if ($val -eq '__get') {
$EnvRegisterKey = $RegisterKey.OpenSubKey('Environment')
$RegistryValueOption = [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames
$EnvRegisterKey.GetValue($name, $null, $RegistryValueOption)
} else {
$EnvRegisterKey = $RegisterKey.OpenSubKey('Environment', $true)
if($null -eq $val) {
$EnvRegisterKey.DeleteValue($name)
} else {
if($val.Contains('%')) {
$EnvRegisterKey.SetValue($name, $val, [Microsoft.Win32.RegistryValueKind]::ExpandString)
} else {
$EnvRegisterKey.SetValue($name, $val, [Microsoft.Win32.RegistryValueKind]::String)
}
}
}
} |
If the environment variable value is empty, delete the environment variable If the value of the environment variable contains `%`, the type sets to `ExpandString`; otherwise, the original type of the environment variable is retained; the default type of the new environment variable is `String`.
bc1d22f

Description
For example, the following command is used to obtain system environment variables.
The string obtained by it will not replace the
%%.Motivation and Context
Use the Registry to get the unexpanded environment variable string inspired by PowerShell/PowerShell#17518.
Fixes #5394
Related to #1813
How Has This Been Tested?
Checklist:
developbranch.