Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install-Boxstarter expands variables in PATH and PSModulePath, overwriting preexisting variables #403

Open
cmconti opened this issue Aug 26, 2019 · 1 comment
Labels
0 - _Triaging Issue is accepted, but a milestone has yet to be added for the issue Bug Issues where something has happened which was not expected or intended

Comments

@cmconti
Copy link

cmconti commented Aug 26, 2019

Install-Boxstarter will expand all variables in your PATH and PSModulePath, and then write back the expanded values, overwriting any unexpanded variables that existed in PATH/PSModulePath prior to Install-Boxstarter being called.

This is a very similar issue to chocolatey/choco#303 (plus the patch in chocolatey/choco#699)

before installing boxstarter:

PS C:\> (get-item 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment').GetValue('Path','','DoNotExpandEnvironmentNames')
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;%SYSTEMROOT%\System32\OpenSSH\;C:\ProgramData\chocolatey\bin;
PS C:\> (get-item 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment').GetValue('PSModulePath','','DoNotExpandEnvironmentNames')
%ProgramFiles%\WindowsPowerShell\Modules;%SystemRoot%\system32\WindowsPowerShell\v1.0\Modules

after installing boxstarter (using: choco install -y boxstarter --params "nodesktopicon"):

PS C:\> (get-item 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment').GetValue('Path','','DoNotExpandEnvironmentNames')
C:\ProgramData\Boxstarter;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\ProgramData\chocolatey\bin;
PS C:\> (get-item 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment').GetValue('PSModulePath','','DoNotExpandEnvironmentNames')
C:\ProgramData\Boxstarter;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules

I'm not in a position to fully test this myself (I have at least tested it on a fresh install of windows 10 1903), but I think this will address the issue:

function PersistBoxStarterPathToEnvironmentVariable($variableName, $boxstarterPath) {
    # Remove user scoped vars from previous releases
    $keyUser = (get-item 'HKCU:\').OpenSubKey('Environment', $true)
    $oldval = $keyUser.GetValue($variableName,'','DoNotExpandEnvironmentNames')
    if ($oldval) {
        $tmp = ($oldval.Split(';', [StringSplitOptions]::RemoveEmptyEntries) | Where-Object { !($_.ToLower() -match "\\boxstarter$")})
        $newval = $tmp -join ';'
    }
    elseif($variableName -eq "PSModulePath") {
        $newval = [environment]::getfolderpath("mydocuments")
        $newval +="\WindowsPowerShell\Modules"
    }
    $keyUser.SetValue($variableName,$newval,[Microsoft.Win32.RegistryValueKind]::ExpandString)

    $keyMachine = (get-item 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\').OpenSubKey('Environment', $true)
    $oldval = $keyMachine.GetValue($variableName,'','DoNotExpandEnvironmentNames')
    if ($oldval) {
        $tmp = ($oldval.Split(';', [StringSplitOptions]::RemoveEmptyEntries) | Where-Object { !($_.ToLower() -match "\\boxstarter$")})
        $tmp += $boxstarterPath
        $newval = $tmp -join ';'
    }
    elseif($variableName -eq "PSModulePath") {
        $newval = "$boxstarterPath;"
        $newval += "%ProgramFiles%\WindowsPowerShell\Modules"
    }
    else {
        $newval ="$boxstarterPath"
    }
    $keyMachine.SetValue($variableName,$newval,[Microsoft.Win32.RegistryValueKind]::ExpandString)

    $varValue = Get-Content env:\$variableName
    $varValue = "$boxstarterPath;$varValue"
    Set-Content env:\$variableName -value $varValue
}

2 notes:

  1. The above code adds $boxstarterPath to the end of an existing path. I don't know if that is important or not
  2. Question: if PSModulePath does not exist for the user (it doesn't on my fresh 1903 install), is there a reason to add [environment]::getfolderpath("mydocuments")+"\WindowsPowerShell\Modules" ?
@pauby pauby added 0 - _Triaging Issue is accepted, but a milestone has yet to be added for the issue Bug Issues where something has happened which was not expected or intended Up For Grabs labels Aug 26, 2019
@pauby
Copy link
Member

pauby commented Aug 26, 2019

I'll take a look at this soon. I think the function should be rewritten. To answer your questions:

  1. This is just going to add $boxstarterPath (which is the new location of Boxstarter) onto the end of the PATH ;

  2. If PSModulePath doesn't exist then we need to create it so the path to the modules have somewhere to sit. By default the user path for Windows PowerShell modules is in Documents\WindowsPowerShell\Modules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0 - _Triaging Issue is accepted, but a milestone has yet to be added for the issue Bug Issues where something has happened which was not expected or intended
Projects
None yet
Development

No branches or pull requests

2 participants