Skip to content

Add-MachinePath function in OpenSSHUtils.psm1 used by install-sshd.ps1 overwrites Path unexpanded string values #2454

Description

@jjcousinne

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions