Skip to content

Error handling of custom DSC resources in config #42

@ssoabx

Description

@ssoabx

Using 2.0.0.3 of the module via the recently published Microsoft365DSC release I'm getting lots of errors. One is related to our own DSC resource being present in the master config. The following error related to this is thrown (German sorry..):

Get-Module : Das Argument für den Parameter "Name" kann nicht überprüft werden. Das Argument ist NULL oder leer. Geben Sie ein Argument an, das nicht NULL oder leer ist, und führen Sie den Befehl    erneut aus. 
In C:\Program Files\WindowsPowerShell\Modules\DSCParser\2.0.0.3\Modules\DSCParser.psm1:341 Zeichen:46
+ ... $loadedModuleTest = Get-Module -Name $moduleToLoad.ModuleName -ListAv ...
+                                          ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidData: (:) [Get-Module], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetModuleCommand
Get-DscResource : Das Argument für den Parameter "Module" kann nicht überprüft werden. Das Argument ist NULL oder leer. Geben Sie ein Argument an, das nicht NULL oder leer ist, und führen Sie den    Befehl erneut aus.
In C:\Program Files\WindowsPowerShell\Modules\DSCParser\2.0.0.3\Modules\DSCParser.psm1:349 Zeichen:57
+ ...  $currentResources = Get-DSCResource -Module $moduleToLoad.ModuleName
+                                                  ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidData: (:) [Get-DscResource], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Get-DscResource

$DSCResources = @()
foreach ($moduleToLoad in $ModulesToLoad)
{
$loadedModuleTest = Get-Module -Name $moduleToLoad.ModuleName -ListAvailable | Where-Object -FilterScript {$_.Version -eq $moduleToLoad.ModuleVersion}
if ($null -eq $loadedModuleTest -and -not [System.String]::IsNullOrEmpty($moduleToLoad.ModuleVersion))
{
throw "Module {$($moduleToLoad.ModuleName)} version {$($moduleToLoad.ModuleVersion)} specified in the configuration isn't installed on the machine/agent. Install it by running: Install-Module -Name '$($moduleToLoad.ModuleName)' -RequiredVersion '$($moduleToLoad.ModuleVersion)'"
}
else
{
$currentResources = Get-DSCResource -Module $moduleToLoad.ModuleName
if (-not [System.String]::IsNullOrEmpty($moduleToLoad.ModuleVersion))
{
$currentResources = $currentResources | Where-Object -FilterScript {$_.Version -eq $moduleToLoad.ModuleVersion}
}
$DSCResources += $currentResources
}
}

Start of said MasterConfig.ps1:

Configuration MasterConfig
{
    param (
    )

    $OrganizationName = $ConfigurationData.NonNodeData.OrganizationName
    if ($ConfigurationData.NonNodeData.Shortname) {$short=$ConfigurationData.NonNodeData.Shortname}
    Else {$short="Org"}

    Import-DscResource -ModuleName 'Microsoft365DSC','Microsoft365DSCCustomRes'

    Node localhost
    {
        
        ......

On the old 1.4.0.4 version this didn't result in an error.

Removing it from the config removes the error (expected).
Adding it as a separate line in the expected format ("Import-DscResource -ModuleName 'Microsoft365DSCCustomRes'") also works.

$moduleName = $statement.CommandElements[$i+1].Value
$currentModule.Add('ModuleName', $moduleName)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions