Skip to content

Commit

Permalink
Add cmdlet Set-PSModulePath (#38)
Browse files Browse the repository at this point in the history
- Added cmdlet `Set-PSModulePath`.
  • Loading branch information
johlju authored May 18, 2020
1 parent 7c1f340 commit f6ecdd5
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Added cmdlet `Set-PSModulePath`.

## [0.8.0] - 2020-05-11

- Added a default value of `en-US` to the `DefaultUICulture` parameter of the `Get-LocalizedData` function
Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,37 @@ Remove-CommonParameter -Hashtable $PSBoundParameters

Returns a new hashtable without the common and optional common parameters.

### `Set-PSModulePath`

This is a wrapper to set environment variable PSModulePath in the current
session or machine wide.

#### Syntax

```plaintext
Set-PSModulePath [-Path] <String> [-Machine] [<CommonParameters>]
```

### Outputs

None.

### Example

```powershell
Set-PSModulePath -Path '<Path 1>;<Path 2>'
```

Sets the session environment variable `PSModulePath` to the specified path
or paths (separated with semi-colons).

```powershell
Set-PSModulePath -Path '<Path 1>;<Path 2>' -Machine
```

Sets the machine environment variable `PSModulePath` to the specified path
or paths (separated with semi-colons).

### `Test-DscParameterState`

This function is used to compare the values in the current state against
Expand Down
1 change: 1 addition & 0 deletions build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Pester:
Script:
- tests/QA
- tests/Unit
- tests/Integration
ExcludeTag:
Tag:
CodeCoverageThreshold: 50
Expand Down
52 changes: 52 additions & 0 deletions source/Public/Set-PSModulePath.ps1
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
}
}
74 changes: 74 additions & 0 deletions tests/Integration/Public/Set-PSModulePath.Tests.ps1
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
}
}
}

0 comments on commit f6ecdd5

Please sign in to comment.