Skip to content

Commit 2744ca4

Browse files
committed
Minor updates to Setup.ps1 - which setups Modules directory to be in the PSModulePath for the machine.
1 parent 2f2a785 commit 2744ca4

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

Setup.ps1

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ If(!(get-module Pester -ListAvailable)) {
1818
}
1919
}
2020

21-
if(-not (Test-Path Function:Set-EnvironmentVariable)) {
22-
Function Set-EnvironmentVariable {
21+
# Note: This function is defined by Chocolatey as well but we have a script local version in case Chocolatey is not installed.
22+
# Also, this version sets the session instance of the enviroment variable.
23+
Function Script:Set-EnvironmentVariable {
2324
[CmdletBinding(SupportsShouldProcess)]
2425
param(
2526
[ValidateScript({-not [string]::IsNullOrWhiteSpace($_)})][Parameter(Mandatory)][string]$Name,
@@ -33,14 +34,16 @@ Function Set-EnvironmentVariable {
3334
}
3435
setx.exe $Name $Value $scopeArgs | Out-Null
3536
Set-Item -Path Env:$Name -Value $Value
36-
}
3737
}
3838

39-
$PSToolboxPath=Join-Path $PSScriptRoot Modules
40-
if(!($env:PSModulePath -like "*$PSToolboxPath*")) {
41-
Set-EnvironmentVariable -Name PSModulePath -Value "$PSToolboxPath;$env:PSModulePath"
42-
if(!($env:PSModulePath -like "*$PSToolboxPath*")) {
43-
throw "PSModulePath not set with $PSToolboxPath" #NOTE: This does not test the change from [System.Environment]::SetEnvironmentVariable is permanent.
39+
40+
$PSToolboxPath=Join-Path $PSScriptRoot Modules
41+
if($env:PSModulePath -notlike "*$PSToolboxPath*") {
42+
Script:Set-EnvironmentVariable -Name 'PSModulePath' -Value "$PSToolboxPath;$env:PSModulePath"
43+
if($PSToolboxPath -notin ($env:PSModulePath -split ';')) {
44+
Write-Host -foreground Cyan $PSToolboxPath
45+
#NOTE: This does not test the change from [System.Environment]::SetEnvironmentVariable is permanent.
46+
throw "PSModulePath ('$env:PSModulePath') is not set with $PSToolboxPath"
4447
}
4548
}
4649

0 commit comments

Comments
 (0)