From f623bb3e3e6e10ff67310763a4fc34880d2ef70d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raimund=20Andr=C3=A9e=20=5BMSFT=5D?= Date: Mon, 3 Apr 2023 05:48:27 +0000 Subject: [PATCH] =?UTF-8?q?Improved=20error=20handling=20when=20compiling?= =?UTF-8?q?=20MOF=20files=20and=20when=20calling=20'Ge=E2=80=A6=20(#27)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Gael --- CHANGELOG.md | 2 ++ source/Scripts/CompileRootConfiguration.ps1 | 35 ++++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63112a6..32e8c2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/source/Scripts/CompileRootConfiguration.ps1 b/source/Scripts/CompileRootConfiguration.ps1 index e9eb8c7..602f138 100644 --- a/source/Scripts/CompileRootConfiguration.ps1 +++ b/source/Scripts/CompileRootConfiguration.ps1 @@ -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 '' @@ -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 }