Invoke-CimMethod : Unable to load PS module for validation - How to Resolve this DSC Problem

2015-11-03T10:30:00Z

After updating one of the DSC modules available on a Desired State Configuration Pull Server I found that, when forcing a DSC client to update it's configuration, I was presented with the error "Invoke-CimMethod : Unable to load PS module C:\Program Files\WindowsPowerShell\Modules\myDSCResources\DscResources\myDSCModule for validation.".

PS E:\>  Invoke-CimMethod -ComputerName somecomputer -Namespace root/Microsoft/Windows/DesiredStateConfiguration -ClassName MSFT_DSCLocalConfigurationManager -MethodName PerformRequiredConfigurationChecks -Arg @{Flags = [System.UInt32]2 } -Verbose
VERBOSE: Performing the operation "Invoke-CimMethod: PerformRequiredConfigurationChecks" on target "MSFT_DSCLocalConfigurationManager".
VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = PerformRequiredConfigurationChecks,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' = root/Microsoft/Windows/DesiredStateConfiguration'.
VERBOSE: An LCM method call arrived from computer SOMECOMPUTER with user sid S-1-5-21-0000000001-0000000001-000000001-0001.
VERBOSE: Executing Get-Action with checksum: 0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F.
VERBOSE: Executing Get-Action returned result status: GetConfiguration.
VERBOSE: [SOMECOMPUTER]:                            [] Checksum is different. LCM will execute GetConfiguration.
VERBOSE: [SOMECOMPUTER]:                            [] GetConfiguration failed.
VERBOSE: [SOMECOMPUTER]:                            [] Executing Get-Action returned success but didn't return any status.
Invoke-CimMethod : Unable to load PS module C:\Program Files\WindowsPowerShell\Modules\myDSCResources\DscResources\myDSCModule for validation.
At line:1 char:2
+  Invoke-CimMethod -ComputerName somecomputer -Namespace root/Microsoft/Windows/Desire ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (root/Microsoft/...gurationManager:String) [Invoke-CimMethod], CimException
    + FullyQualifiedErrorId : CannotLoadModuleFileForValidation,Microsoft.Management.Infrastructure.CimCmdlets.InvokeCimMethodCommand
    + PSComputerName        : somecomputer

VERBOSE: Operation 'Invoke CimMethod' complete.

I have worked through many issues creating DSC Pull configurations and custom modules, ranging from clients missing Windows Updates to modules having been compressed using IO.Compression.ZipFile instead of the Windows shell "send to Compressed folder".

"Unable to load PS module for validation is not not a packaging issue, even though running Get-DscResouce shows the resource in the list of available resources. Instead the cause is much simpler. Take the contents of the PowerShell script module (.psm1) file for your DSC module and pasted it into the PowerShell Integrated Scripted Environment (ISE). Remove or comment out the "Export-ModuleMember" line from the end of the file and try running it (hit F5). Do you see any errors? Here is what I saw:

 At line:305 char:31
 +             if($PSCmdlet.ShouldProcess(LocalisationData.CheckoutSP -f $SvnCheckoutUrl,$Sv ...
 +                                        ~
 Missing ')' in method call.
 At line:305 char:31
 +             if($PSCmdlet.ShouldProcess(LocalisationData.CheckoutSP -f $SvnCheckoutUrl,$Sv ...
 +                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Unexpected token 'LocalisationData.CheckoutSP' in expression or statement.
 At line:305 char:31
 +             if($PSCmdlet.ShouldProcess(LocalisationData.CheckoutSP -f $SvnCheckoutUrl,$Sv ...
 +                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Missing closing ')' after expression in 'if' statement.
 At line:300 char:28
 +         if($Ensure -eq "Present"){
 +                                  ~
 Missing closing '}' in statement block.
 At line:297 char:128
 + ... nsure $Ensure)){
 +                    ~
 Missing closing '}' in statement block.
 At line:272 char:1
 + {
 + ~
 Missing closing '}' in statement block.
 At line:305 char:94
 + ... SvnCheckoutPath){
 +                    ~
 Unexpected token ')' in expression or statement.
 At line:308 char:3
 +         } else {
 +         ~
 Unexpected token '}' in expression or statement.
 At line:322 char:2
 +     } else {
 +     ~
 Unexpected token '}' in expression or statement.
 At line:325 char:1
 + }
 + ~
 Unexpected token '}' in expression or statement.
     + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
     + FullyQualifiedErrorId : MissingEndParenthesisInMethodCall

It turns out I had a whole cascade of issues caused by a single missing paranthesis. At the time I was experienced this problem, a syntax error was the last possibility I considered. I could see the resource displayed when I issued a Get-DscResource so I assumed that the module itself was okay. It transpires that just because a module is recognised by Get-DscResouce, there's no guarantee it's functions can be successfully defined.