-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added cmdlet `Set-PSModulePath`.
- Loading branch information
Showing
5 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
|
||
<# | ||
.SYNOPSIS | ||
Set environment variable PSModulePath in the current session or machine | ||
wide. | ||
.DESCRIPTION | ||
This is a wrapper to set environment variable PSModulePath in current | ||
session or machine wide. | ||
.PARAMETER Path | ||
A string with all the paths separated by semi-colons. | ||
.PARAMETER Machine | ||
If set the PSModulePath will be changed machine wide. If not set, only | ||
the current session will be changed. | ||
.EXAMPLE | ||
Set-PSModulePath -Path '<Path 1>;<Path 2>' | ||
.EXAMPLE | ||
Set-PSModulePath -Path '<Path 1>;<Path 2>' -Machine | ||
#> | ||
function Set-PSModulePath | ||
{ | ||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute( | ||
'PSUseShouldProcessForStateChangingFunctions', | ||
'', | ||
Justification = 'ShouldProcess is not supported in DSC resources.' | ||
)] | ||
[CmdletBinding()] | ||
param | ||
( | ||
[Parameter(Mandatory = $true)] | ||
[ValidateNotNullOrEmpty()] | ||
[System.String] | ||
$Path, | ||
|
||
[Parameter()] | ||
[System.Management.Automation.SwitchParameter] | ||
$Machine | ||
) | ||
|
||
if ($Machine.IsPresent) | ||
{ | ||
[System.Environment]::SetEnvironmentVariable('PSModulePath', $Path, [System.EnvironmentVariableTarget]::Machine) | ||
} | ||
else | ||
{ | ||
$env:PSModulePath = $Path | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path | ||
$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ | ||
($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and | ||
$(try | ||
{ | ||
Test-ModuleManifest $_.FullName -ErrorAction Stop | ||
} | ||
catch | ||
{ | ||
$false | ||
} ) | ||
}).BaseName | ||
|
||
Import-Module $ProjectName -Force | ||
|
||
Describe 'Set-PSModulePath' -Tag 'SetPSModulePath' { | ||
BeforeAll { | ||
$currentPSModulePath = $env:PSModulePath | ||
|
||
if ($isWindows -or $PSEdition -eq 'Desktop') | ||
{ | ||
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) | ||
|
||
$skipTest = -not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) | ||
} | ||
else | ||
{ | ||
$skipTest = $true | ||
} | ||
|
||
if (-not $skipTest) | ||
{ | ||
$currentMachinePSModulePath = [Environment]::GetEnvironmentVariable('PSModulePath', 'Machine') | ||
} | ||
} | ||
|
||
Context 'When updating the session environment variable PSModulePath' { | ||
AfterAll { | ||
$env:PSModulePath = $currentPSModulePath | ||
} | ||
|
||
It 'Should not throw an error and have set the correct value' { | ||
{ Set-PSModulePath -Path 'C:\Module' } | Should -Not -Throw | ||
|
||
$env:PSModulePath | Should -Be 'C:\Module' | ||
} | ||
} | ||
|
||
Context 'When updating the machine environment variable PSModulePath' { | ||
AfterAll { | ||
if (-not $skipTest) | ||
{ | ||
[System.Environment]::SetEnvironmentVariable('PSModulePath', $currentMachinePSModulePath, [System.EnvironmentVariableTarget]::Machine) | ||
} | ||
} | ||
|
||
It 'Should not throw an error and have set the correct value' -Skip:$skipTest { | ||
{ Set-PSModulePath -Path 'C:\Module' -Machine } | Should -Not -Throw | ||
|
||
[Environment]::GetEnvironmentVariable('PSModulePath', 'Machine') | Should -Be 'C:\Module' | ||
} | ||
} | ||
|
||
Context 'When the tests have run for Set-PSModulePath' { | ||
It 'Should have returned the session PSModulePath to the original value' { | ||
$env:PSModulePath | Should -Be $currentPSModulePath | ||
} | ||
|
||
It 'Should have returned the machine PSModulePath to the original value' -Skip:$skipTest { | ||
[Environment]::GetEnvironmentVariable('PSModulePath', 'Machine') | Should -Be $currentMachinePSModulePath | ||
} | ||
} | ||
} | ||
|