Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions contrib/win32/openssh/OpenSSHUtils.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ function Enable-Privilege {
$type[0]::EnablePrivilege($Privilege, $Disable)
}

Function Add-MachinePath {
function Add-MachinePath {
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact="High")]
param
(
Expand All @@ -838,13 +838,30 @@ Function Add-MachinePath {
)

if (Test-Path $FilePath) {
$machinePath = (Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path
if (-not ($machinePath.ToLower().Contains("$FilePath;".ToLower()) -or $machinePath.ToLower().Contains("$FilePath\;".ToLower())))
{
$newPath = $FilePath + ’;’ + $machinePath
Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH –Value $newPath
if ((Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path -eq $newPath) {
Write-Host "Updated Machine PATH to include OpenSSH directory, restart/re-login required to take effect globally" -ForegroundColor Yellow
$regKey = Get-Item -Path 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment'
$pathValue = $regKey.GetValue('PATH', '', 'DoNotExpandEnvironmentNames')
$pathType = $regKey.GetValueKind('PATH')

# Normalize for comparison only (expand variables and trim trailing backslash)
$normalizedFilePath = [Environment]::ExpandEnvironmentVariables($FilePath).TrimEnd('\')
$normalizedEntries = $pathValue -split ';' |
Where-Object { -not [string]::IsNullOrWhiteSpace($_) } |
ForEach-Object { [Environment]::ExpandEnvironmentVariables($_.Trim()).TrimEnd('\') }

if ($normalizedEntries.Where({ $_ -ieq $normalizedFilePath }, 'First').Count -eq 0) {
$newPath = $FilePath + ';' + $pathValue

$message = "Need to add the path to the Machine PATH environment variable."
$prompt = "Shall I add '$FilePath' to Machine PATH?"
$description = "Add '$FilePath' to Machine PATH."

if ($PSCmdlet.ShouldProcess($description, $prompt, $message)) {
Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newPath -Type $pathType

$verifyValue = (Get-Item -Path 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment').GetValue('PATH', '', 'DoNotExpandEnvironmentNames')
if ($verifyValue -eq $newPath) {
Write-Host "Updated Machine PATH to include OpenSSH directory, restart/re-login required to take effect globally" -ForegroundColor Yellow
}
}
}
}
Expand Down