@@ -18,8 +18,9 @@ If(!(get-module Pester -ListAvailable)) {
18
18
}
19
19
}
20
20
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 {
23
24
[CmdletBinding (SupportsShouldProcess )]
24
25
param (
25
26
[ValidateScript ({-not [string ]::IsNullOrWhiteSpace($_ )})][Parameter (Mandatory )][string ]$Name ,
@@ -33,14 +34,16 @@ Function Set-EnvironmentVariable {
33
34
}
34
35
setx.exe $Name $Value $scopeArgs | Out-Null
35
36
Set-Item - Path Env:$Name - Value $Value
36
- }
37
37
}
38
38
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 "
44
47
}
45
48
}
46
49
0 commit comments