Skip to content

Commit 0ec5feb

Browse files
authored
Fix Add-MachinePath to preserve registry value type and unexpanded variables (#819)
* Fix non-ASCII characters in OpenSSHUtils.psm1 * Fix Add-MachinePath to preserve registry value type and unexpanded variables Refactor Add-MachinePath to avoid unintended modifications to the PATH registry value: - Use GetValue() with DoNotExpandEnvironmentNames to preserve unexpanded environment variables (e.g., %SystemRoot%) - Preserve original registry value type (REG_SZ vs REG_EXPAND_SZ) using GetValueKind() - Keep original PATH entry values unchanged; normalization (expansion + backslash trimming) only used for duplicate detection - Improve ShouldProcess implementation with proper message/prompt/description parameters for better -WhatIf/-Confirm support - Enhance duplicate detection to compare expanded values, catching duplicates like C:\Windows\System32 vs %SystemRoot%\System32 * address comments
1 parent ed76997 commit 0ec5feb

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

contrib/win32/openssh/OpenSSHUtils.psm1

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ function Enable-Privilege {
829829
$type[0]::EnablePrivilege($Privilege, $Disable)
830830
}
831831

832-
Function Add-MachinePath {
832+
function Add-MachinePath {
833833
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact="High")]
834834
param
835835
(
@@ -838,13 +838,30 @@ Function Add-MachinePath {
838838
)
839839

840840
if (Test-Path $FilePath) {
841-
$machinePath = (Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path
842-
if (-not ($machinePath.ToLower().Contains("$FilePath;".ToLower()) -or $machinePath.ToLower().Contains("$FilePath\;".ToLower())))
843-
{
844-
$newPath = $FilePath + ; + $machinePath
845-
Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH –Value $newPath
846-
if ((Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path -eq $newPath) {
847-
Write-Host "Updated Machine PATH to include OpenSSH directory, restart/re-login required to take effect globally" -ForegroundColor Yellow
841+
$regKey = Get-Item -Path 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment'
842+
$pathValue = $regKey.GetValue('PATH', '', 'DoNotExpandEnvironmentNames')
843+
$pathType = $regKey.GetValueKind('PATH')
844+
845+
# Normalize for comparison only (expand variables and trim trailing backslash)
846+
$normalizedFilePath = [Environment]::ExpandEnvironmentVariables($FilePath).TrimEnd('\')
847+
$normalizedEntries = $pathValue -split ';' |
848+
Where-Object { -not [string]::IsNullOrWhiteSpace($_) } |
849+
ForEach-Object { [Environment]::ExpandEnvironmentVariables($_.Trim()).TrimEnd('\') }
850+
851+
if ($normalizedEntries.Where({ $_ -ieq $normalizedFilePath }, 'First').Count -eq 0) {
852+
$newPath = $FilePath + ';' + $pathValue
853+
854+
$message = "Need to add the path to the Machine PATH environment variable."
855+
$prompt = "Shall I add '$FilePath' to Machine PATH?"
856+
$description = "Add '$FilePath' to Machine PATH."
857+
858+
if ($PSCmdlet.ShouldProcess($description, $prompt, $message)) {
859+
Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newPath -Type $pathType
860+
861+
$verifyValue = (Get-Item -Path 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment').GetValue('PATH', '', 'DoNotExpandEnvironmentNames')
862+
if ($verifyValue -eq $newPath) {
863+
Write-Host "Updated Machine PATH to include OpenSSH directory, restart/re-login required to take effect globally" -ForegroundColor Yellow
864+
}
848865
}
849866
}
850867
}

0 commit comments

Comments
 (0)