Skip to content

Instantly share code, notes, and snippets.

@mwallner
Last active November 4, 2018 14:38
Show Gist options
  • Save mwallner/5736ad119398decbe538bbbcbd1b8978 to your computer and use it in GitHub Desktop.
Save mwallner/5736ad119398decbe538bbbcbd1b8978 to your computer and use it in GitHub Desktop.
check how PSModulePath can be modified | ref https://github.com/chocolatey/choco/issues/1668
$ErrorActionPreference = "Stop"
function Out-EnvValues($valName, $txt) {
$envVarMachine = [Environment]::GetEnvironmentVariable($valName, 'Machine')
$envVarUser = [Environment]::GetEnvironmentVariable($valName, 'User')
$envVar = Get-Content env:$valName
Write-Output "-------------------------------------------"
Write-Output "Out-EnvValues: variable '$valName' $txt"
Write-Output "machine: '$envVarMachine'"
Write-Output "user : '$envVarUser'"
Write-Output "env : '$envVar'"
Write-Output "-------------------------------------------"
}
Out-EnvValues "Path"
Out-EnvValues "PSModulePath"
Write-Output "... subprocess vals ..."
powershell.exe -command "write-host env:PSModulePath in subprocess: `$env:PSModulePath"
cmd.exe /c "powershell.exe -command ""write-host env:PSModulePath via cmd-subprocess: `$env:PSModulePath"""
Write-Output "`r`naction happening...`r`n"
$userPSPathOld = [Environment]::GetEnvironmentVariable('PSModulePath', 'User')
$userPSPathNew = if ($userPSPathOld -eq "") { "c:/secretModulesPath" } else { "$userPSPathOld;C:/secretModulesPath" }
[Environment]::SetEnvironmentVariable('PSModulePath', "$userPSPathNew", 'User')
Write-Output "set user/PSModulePath to '$userPSPathNew'"
Out-EnvValues "PSModulePath"
Write-Output "... subprocess vals ..."
powershell.exe -command "write-host env:PSModulePath in subprocess: `$env:PSModulePath"
cmd.exe /c "powershell.exe -command ""write-host env:PSModulePath via cmd-subprocess: `$env:PSModulePath"""
@mwallner
Copy link
Author

mwallner commented Nov 4, 2018

Adding Update-SessionEnvironment did not change anything, $env:PSModulePath still points to the old USER environment

[EnvVarCheck] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\Users\BUILDU~1\AppData\Local\Temp\jenkins7098772948215386428.ps1'"
-------------------------------------------
Out-EnvValues: variable 'Path' 
machine: 'C:\ProgramData\Boxstarter;C:\oracle\product\10.2.0\client_1\bin;C:\oracle\product\10.2.0\client_1\bin;C:\oracle\product\10.2.0\client_1\bin;C:\oraclexe\app\oracle\product\11.2.0\server\bin;;C:\Program Files (x86)\Eziriz\.NET Reactor;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\chocolatey\bin;C:\Program Files (x86)\Git\cmd;C:\Program Files (x86)\AccuRev\bin;C:\Program Files (x86)\GnuWin32\bin;C:\Program Files\doxygen\bin;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files (x86)\IncrediBuild;C:\Program Files (x86)\SLN File Tools\;C:\kse\miktex\texmfs\install\miktex\bin\x64;'
user   : 'C:\Users\builduser\AppData\Local\Microsoft\WindowsApps;C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.6\bin;'
env    : 'C:\ProgramData\Boxstarter;C:\oracle\product\10.2.0\client_1\bin;C:\oraclexe\app\oracle\product\11.2.0\server\bin;;C:\Program Files (x86)\Eziriz\.NET Reactor;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\chocolatey\bin;C:\Program Files (x86)\Git\cmd;C:\Program Files (x86)\AccuRev\bin;C:\Program Files (x86)\GnuWin32\bin;C:\Program Files\doxygen\bin;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files (x86)\IncrediBuild;C:\Program Files (x86)\SLN File Tools\;C:\kse\miktex\texmfs\install\miktex\bin\x64;C:\Users\builduser\AppData\Local\Microsoft\WindowsApps;C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.6\bin'
-------------------------------------------
-------------------------------------------
Out-EnvValues: variable 'PSModulePath' 
machine: 'C:\ProgramData\Boxstarter;C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules'
user   : 'C:\Users\builduser\Documents\WindowsPowerShell\Modules;C:/secretModulesPath'
env    : 'C:\Users\builduser\Documents\WindowsPowerShell\Modules'
-------------------------------------------
... subprocess vals ...
env:PSModulePath in subprocess: C:\Users\builduser\Documents\WindowsPowerShell\Modules
env:PSModulePath via cmd-subprocess: C:\Users\builduser\Documents\WindowsPowerShell\Modules

action happening...

set user/PSModulePath to 'C:\Users\builduser\Documents\WindowsPowerShell\Modules;C:/secretModulesPath;C:/secretModulesPath'
-------------------------------------------
Out-EnvValues: variable 'PSModulePath' 
machine: 'C:\ProgramData\Boxstarter;C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules'
user   : 'C:\Users\builduser\Documents\WindowsPowerShell\Modules;C:/secretModulesPath;C:/secretModulesPath'
env    : 'C:\Users\builduser\Documents\WindowsPowerShell\Modules'
-------------------------------------------
... subprocess vals ...
env:PSModulePath in subprocess: C:\Users\builduser\Documents\WindowsPowerShell\Modules
env:PSModulePath via cmd-subprocess: C:\Users\builduser\Documents\WindowsPowerShell\Modules
Finished: SUCCESS

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