Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow PowerShellGroup to override psmodulepath #312

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions powershellgroup/Tests/powershellgroup.config.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,37 @@ Describe 'PowerShellGroup resource tests' {
$res.resources[0].properties.Name | Should -Be "Object1"
$res.resources[0].properties.Prop1 | Should -Be "Property of object1"
}

It 'Custom psmodulepath in config works' -Skip:(!$IsWindows){

$OldPSModulePath = $env:PSModulePath
Copy-Item -Recurse -Force -Path "$PSScriptRoot/PSTestModule" -Destination $TestDrive
Rename-Item -Path "$PSScriptRoot/PSTestModule" -NewName "_PSTestModule"

try {
$yaml = @"
`$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json
resources:
- name: Working with class-based resources
type: DSC/PowerShellGroup
properties:
psmodulepath: `$env:PSModulePath;$TestDrive
resources:
- name: Class-resource Info
type: PSTestModule/TestClassResource
"@
$out = $yaml | dsc config export
$LASTEXITCODE | Should -Be 0
$res = $out | ConvertFrom-Json
$res.'$schema' | Should -BeExactly 'https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/config/document.json'
$res.'resources' | Should -Not -BeNullOrEmpty
$res.resources.count | Should -Be 5
$res.resources[0].properties.Name | Should -Be "Object1"
$res.resources[0].properties.Prop1 | Should -Be "Property of object1"
}
finally {
Rename-Item -Path "$PSScriptRoot/_PSTestModule" -NewName "PSTestModule"
$env:PSModulePath = $OldPSModulePath
}
}
}
35 changes: 11 additions & 24 deletions powershellgroup/powershellgroup.resource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ if (($PSVersionTable.PSVersion.Major -eq 7) -and ($PSVersionTable.PSVersion.Mino
throw "PowerShell 7.4-previews are not supported by PowerShellGroup resource; please use PS 7.4.0-rc.1 or newer."
}

$inputobj_pscustomobj = $null
if ($stdinput)
{
$inputobj_pscustomobj = $stdinput | ConvertFrom-Json
$new_psmodulepath = $inputobj_pscustomobj.psmodulepath
if ($new_psmodulepath)
{
$env:PSModulePath = $ExecutionContext.InvokeCommand.ExpandString($new_psmodulepath)
}
}

$DscModule = Get-Module -Name PSDesiredStateConfiguration -ListAvailable |
Sort-Object -Property Version -Descending |
Select-Object -First 1
Expand Down Expand Up @@ -105,12 +116,6 @@ if ($Operation -eq 'List')
}
elseif ($Operation -eq 'Get')
{
$inputobj_pscustomobj = $null
if ($stdinput)
{
$inputobj_pscustomobj = $stdinput | ConvertFrom-Json
}

$result = @()

RefreshCache
Expand Down Expand Up @@ -181,12 +186,6 @@ elseif ($Operation -eq 'Get')
}
elseif ($Operation -eq 'Set')
{
$inputobj_pscustomobj = $null
if ($stdinput)
{
$inputobj_pscustomobj = $stdinput | ConvertFrom-Json
}

$result = @()

RefreshCache
Expand Down Expand Up @@ -255,12 +254,6 @@ elseif ($Operation -eq 'Set')
}
elseif ($Operation -eq 'Test')
{
$inputobj_pscustomobj = $null
if ($stdinput)
{
$inputobj_pscustomobj = $stdinput | ConvertFrom-Json
}

$result = @()

RefreshCache
Expand Down Expand Up @@ -329,12 +322,6 @@ elseif ($Operation -eq 'Test')
}
elseif ($Operation -eq 'Export')
{
$inputobj_pscustomobj = $null
if ($stdinput)
{
$inputobj_pscustomobj = $stdinput | ConvertFrom-Json
}

$result = @()

RefreshCache
Expand Down