PowerShell / PowerShell/Win32-OpenSSH
Add-MachinePath function in OpenSSHUtils.psm1 used by install-sshd.ps1 overwrites Path unexpanded string values
Nobody has claimed this yet.
- Dominant language
- No language data
- Stars
- 8.3k
- Forks
- 819
- PR merge metrics
- No merged PRs in 30d
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest version
- Search the existing issues.
Steps to reproduce
The Add-MachinePath function in the OpenSSHUtils.psm1 module used by the install.sshd.ps1 script uses GetEnvironmentVariable to read the machine Path environment variable and SetEnvironmentVariable to set it with the SSH service folder appended, but this writes and reads the value as a REG_SZ rather than a REG_EXPAND_SZ type value, so expands and replaces path values such as "%SystemRoot%\system32". To update machine or user scope Path environment values without replacing expandable string values with expanded values you need to read the underlying registry property without expanding the values and then having prefixed it with an additional path set the new value as a REG_EXPAND_SZ property value. For example, $UnexpandedPathString = (Get-Item -LiteralPath 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment').GetValue('Path','','DoNotExpandEnvironmentNames'); $UnexpandedPathString = $UnexpandedPathString + ";C:\Program Files\Publisher\Product\bin"; Set-ItemProperty -Type ExpandString -LiteralPath 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name Path -Value $UnexpandedPathString , then if you want a value update broadcast either set and remove a dummy environment variable using SetEnvironmentVariable or using P/Invoke to load and call the equivalent Win32 API broadcast trigger. Note that this issue with the GetEnvironmentVariable and SetEnvironmentVariable .NET methods is a long standing problem that has yet to be corrected despite long-standing feature requests and multiple active community issue threads on GitHub (such as dotnet/runtime #1442 and PowerShell #26725).
Expected behavior
PS C:\> (Get-Item -LiteralPath 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment').GetValue('Path','','DoNotExpandEnvironmentNames')
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\
PS C:\> Add-MachinePath -FilePath $scriptdir @psBoundParameters
PS C:\> (Get-Item -LiteralPath 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment').GetValue('Path','','DoNotExpandEnvironmentNames')
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files\OpenSSH
Actual behavior
PS C:\> (Get-Item -LiteralPath 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment').GetValue('Path','','DoNotExpandEnvironmentNames')
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\
PS C:\> Add-MachinePath -FilePath $scriptdir @psBoundParameters
PS C:\> (Get-Item -LiteralPath 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment').GetValue('Path','','DoNotExpandEnvironmentNames')
C:\Windows\System32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\OpenSSH
Error details
No error produced but behaviour does not preserve unexpanded embedded environment variables.
Environment data
PS C:> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.26100.9168
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.26100.9168
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Version
10.0.0.0p2-Preview (Latest)
Visuals
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in OpenSSHUtils.psm1 at Add-MachinePath and trace how install-sshd.ps1 calls it. Reproduce the issue with the registry GetValue commands shown, then verify that the machine Path retains expandable values while appending the OpenSSH directory. Done means the registry value remains expandable and the existing installation flow still updates Path.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- powershell
- Domain
- operating-systems
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 74/100