Skip to content

Commit

Permalink
Improved error handling when compiling MOF files and when calling 'Ge… (
Browse files Browse the repository at this point in the history
#27)

Co-authored-by: Gael <gaelcolas@users.noreply.github.com>
  • Loading branch information
raandree and gaelcolas authored Apr 3, 2023
1 parent 389230e commit f623bb3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Increase build speed of root configuration by only importing required Composites/Resources
- Added ''UseEnvironment'' parameter to cater for RSOP for identical node names in different environments
- Adding Home.md to wikiSource and correct casing.
- Redesign of the function Split-Array. Most of the time it was not working as expected, especially when requesting larger ChunkCounts (see AutomatedLab/AutomatedLab.Common/#118).
- Improved error handling when compiling MOF files and when calling 'Get-DscResource'.
- Redesign of the function 'Split-Array'. Most of the time it was not working as expected, especially when requesting larger ChunkCounts (see AutomatedLab/AutomatedLab.Common/#118).
- Improved error handling when compiling MOF files.

Expand Down
35 changes: 31 additions & 4 deletions source/Scripts/CompileRootConfiguration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,36 @@ foreach ($module in $BuildInfo.'Sampler.DscPipeline'.DscCompositeResourceModules
}

Write-Host -Object ''
Write-Host -Object "Preloading available resources"
Write-Host -Object 'Preloading available resources'

$availableResources = Get-DscResource
# An emptu path in the PSModulePath causes an error when loading DSC resources. Only then the PSModulePath is modified to remove the empty path.
# If you want to remove 'Program Files' or 'Documents' from the PSModulePath, please add the Sampler task 'Set_PsModulePath' to the task sequence.
if ($env:PSModulePath -like '*;;*')
{
$previousPSModulePath = $env:PSModulePath
$env:PSModulePath = $env:PSModulePath -replace "$([System.IO.Path]::PathSeparator)$([System.IO.Path]::PathSeparator)", [System.IO.Path]::PathSeparator
}

try
{
$availableResources = Get-DscResource
}
catch
{
if ($_.Exception -is [System.Management.Automation.ParameterBindingException] -and $_.Exception.ParameterName -eq 'Path')
{
Write-Error -Message "There was error while loading DSC resources because the 'PSModulePath' contained a path that does not exist. The error was: $($_.Exception.Message)" -Exception $_.Exception
}
else
{
Write-Error -Message "There was error while loading DSC resources. The error was: $($_.Exception.Message)" -Exception $_.Exception
}
}

if ($previousPSModulePath)
{
$env:PSModulePath = $previousPSModulePath
}

Write-Host -Object ''

Expand All @@ -70,10 +97,10 @@ foreach ($node in $rsopCache.GetEnumerator())
{
$importStatements = foreach ($configurationItem in $node.Value.Configurations)
{
$resource = $availableResources.Where({$_.Name -eq $configurationItem})
$resource = $availableResources.Where({ $_.Name -eq $configurationItem })
if ($null -eq $resource)
{
Write-Debug -Message "No DSC resource found for configuration $configurationItem"
Write-Debug -Message "No DSC resource found for configuration '$configurationItem'"
continue
}

Expand Down

0 comments on commit f623bb3

Please sign in to comment.