Skip to content

Esoteric Error when a class-based DSC Resource has a property named Scope #115

@michaeltlombardi

Description

@michaeltlombardi

Prerequisites

  • Write a descriptive title.
  • Make sure you are able to repro it on the latest released version
  • Search the existing issues.

Steps to reproduce

When I define a DSC Resource as a PowerShell class, I should be able to use arbitrary names for the properties, so long as they're not one of PowerShell's automatic variables.

  1. Create a new folder, dsc-repro:

    New-Item ./dsc-repro -ItemType Directory
    Push-Location ./dsc-repro
  2. Define a new PowerShell module manifest, dsc-repro.psd1, with the following manifest:

    New-ModuleManifest -RootModule dsc-repro.psm1 -DscResourcesToExport ReproInvalid, ReproValid
  3. Define the root module with the following code:

    [DscResource()] class ReproInvalid {
        [DscProperty(key)] [string] $Scope
        [ReproInvalid] Get() {
            $actual =  [ReproInvalid]::new()
            $actual.Scope = $this.Scope
            return $actual
        }
        [bool] Test() { return $true }
        [void] Set() { }
    }
    
    [DscResource()] class ReproValid {
        [DscProperty(key)] [string] $ConfigurationScope
        [ReproValid] Get() {
            $actual =  [ReproValid]::new()
            $actual.Scope = $this.ConfigurationScopeScope
            return $actual
        }
        [bool] Test() { return $true }
        [void] Set()  { }
    }
  4. Call Get-DscResource for the module's DSC Resources:

    # Add the local folder to the PSModulePath
    $env:PSModulePath += [System.IO.Path]::PathSeparator + $pwd.Path
    Get-DscResource -Module dsc-repro
    MethodInvocationException:
      Exception calling "ImportClassResourcesFromModule" with "3" argument(s):
        "Syntax error:   At line:4, char:21  Buffer:  {     [key]string Scope^; }; "
    
  5. Comment out the definition for ReproInvalid:

    # [DscResource()] class ReproInvalid {
    #     [DscProperty(key)] [string] $Scope
    #     [ReproInvalid] Get() {
    #         $actual =  [ReproInvalid]::new()
    #         $actual.Scope = $this.Scope
    #         return $actual
    #     }
    #     [bool] Test() { return $true }
    #     [void] Set() { }
    # }
    
    [DscResource()] class ReproValid {
        [DscProperty(key)] [string] $ConfigurationScope
        [ReproValid] Get() {
            $actual =  [ReproValid]::new()
            $actual.Scope = $this.ConfigurationScopeScope
            return $actual
        }
        [bool] Test() { return $true }
        [void] Set()  { }
    }
  6. Call Get-DscResource for the module's DSC Resources:

    Get-DscResource -Module dsc-repro
    ImplementationDetail : ClassBased
    ResourceType         : ReproValid
    Name                 : ReproValid
    FriendlyName         :
    Module               : dsc-repro
    ModuleName           : dsc-repro
    Version              : 0.0.1
    Path                 : C:\code\dsc-repro\dsc-repro.psd1
    ParentPath           : C:\code\dsc-repro
    ImplementedAs        : PowerShell
    CompanyName          : Unknown
    Properties           : {ConfigurationScope, DependsOn, PsDscRunAsCredential}
    

Expected behavior

PS> Get-DscResource -Module dsc-repro

ImplementationDetail : ClassBased
ResourceType         : ReproInvalid
Name                 : ReproInvalid
FriendlyName         :
Module               : dsc-repro
ModuleName           : dsc-repro
Version              : 0.0.1
Path                 : C:\code\dsc-repro\dsc-repro.psd1
ParentPath           : C:\code\dsc-repro
ImplementedAs        : PowerShell
CompanyName          : Unknown
Properties           : {Scope, DependsOn, PsDscRunAsCredential}

ImplementationDetail : ClassBased
ResourceType         : ReproValid
Name                 : ReproValid
FriendlyName         :
Module               : dsc-repro
ModuleName           : dsc-repro
Version              : 0.0.1
Path                 : C:\code\dsc-repro\dsc-repro.psd1
ParentPath           : C:\code\dsc-repro
ImplementedAs        : PowerShell
CompanyName          : Unknown
Properties           : {ConfigurationScope, DependsOn, PsDscRunAsCredential}

Actual behavior

PS> Get-DscResource -Module dsc-repro

MethodInvocationException:
  Exception calling "ImportClassResourcesFromModule" with "3" argument(s):
    "Syntax error:   At line:4, char:21  Buffer:  {     [key]string Scope^; }; "

Error details

Exception             : 
    Type           : System.Management.Automation.MethodInvocationException
    ErrorRecord    : 
        Exception             : 
            Type    : System.Management.Automation.ParentContainsErrorRecordException
            Message : Exception calling "ImportClassResourcesFromModule" with "3" argument(s): "Syntax error:
                      At line:4, char:21
                      Buffer:
                      {
    [key]string Scope^;
};
                      "
            HResult : -2146233087
        CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
        FullyQualifiedErrorId : CimException
        InvocationInfo        : 
            ScriptLineNumber : 2496
            OffsetInLine     : 5
            HistoryId        : -1
            ScriptName       : C:\Users\mikey\Documents\PowerShell\Modules\PSDesiredStateConfiguration\2.0.7\PSDesiredStateConfiguration.psm1
            Line             : $resourcesFound = [Microsoft.PowerShell.DesiredStateConfiguration.Internal.DscClassCache]::ImportClassResourcesFromModule($Module, $Resources, $functionsToDefine)  

            PositionMessage  : At C:\Users\mikey\Documents\PowerShell\Modules\PSDesiredStateConfiguration\2.0.7\PSDesiredStateConfiguration.psm1:2496 char:5
                               +     $resourcesFound = [Microsoft.PowerShell.DesiredStateConfiguration …
                               +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            PSScriptRoot     : C:\Users\mikey\Documents\PowerShell\Modules\PSDesiredStateConfiguration\2.0.7
            PSCommandPath    : C:\Users\mikey\Documents\PowerShell\Modules\PSDesiredStateConfiguration\2.0.7\PSDesiredStateConfiguration.psm1
            CommandOrigin    : Internal
        ScriptStackTrace      : at ImportClassResourcesFromModule, C:\Users\mikey\OneDrive -
Microsoft\Documents\PowerShell\Modules\PSDesiredStateConfiguration\2.0.7\PSDesiredStateConfiguration.psm1: line 2496
                                at Get-DscResource<Begin>, C:\Users\mikey\OneDrive - 
Microsoft\Documents\PowerShell\Modules\PSDesiredStateConfiguration\2.0.7\PSDesiredStateConfiguration.psm1: line 3967
                                at <ScriptBlock>, <No file>: line 1
    TargetSite     : 
        Name          : ConvertToMethodInvocationException
        DeclaringType : System.Management.Automation.ExceptionHandlingOps, System.Management.Automation, Version=7.3.6.500, Culture=neutral, PublicKeyToken=31bf3856ad364e35
        MemberType    : Method
        Module        : System.Management.Automation.dll
    Message        : Exception calling "ImportClassResourcesFromModule" with "3" argument(s): "Syntax error:
                     At line:4, char:21
                     Buffer:
                     {
    [key]string Scope^;
};
                     "
    Data           : System.Collections.ListDictionaryInternal
    InnerException : 
        Type            : Microsoft.Management.Infrastructure.CimException
        NativeErrorCode : Failed
        ErrorData       : OMI_Error
        StatusCode      : 1
        TargetSite      : 
            Name          : DeserializeClassHandle
            DeclaringType : Microsoft.Management.Infrastructure.Serialization.CimMofDeserializer, Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35
            MemberType    : Method
            Module        : Microsoft.Management.Infrastructure.dll
        Message         : Syntax error:
                          At line:4, char:21
                          Buffer:
                          {
    [key]string Scope^;
};

        Source          : Microsoft.Management.Infrastructure
        HResult         : -2146233088
        StackTrace      : 
   at Microsoft.Management.Infrastructure.Serialization.CimMofDeserializer.DeserializeClassHandle(Byte[] serializedData, UInt32& offset, IEnumerable`1 cimClasses, String computerName, String     
namespaceName, OnClassNeeded onClassNeededCallback, GetIncludedFileContent getIncludedFileCallback)
   at Microsoft.Management.Infrastructure.Serialization.CimMofDeserializer.DeserializeClasses(Byte[] serializedData, UInt32& offset, IEnumerable`1 classes, String computerName, String
namespaceName, OnClassNeeded onClassNeededCallback, GetIncludedFileContent getIncludedFileCallback)
   at Microsoft.PowerShell.DesiredStateConfiguration.CimDSCParser.ParseSchemaMofFileBuffer(String mof)
   at Microsoft.PowerShell.DesiredStateConfiguration.Internal.DscClassCache.ProcessMofForDynamicKeywords(PSModuleInfo module, ICollection`1 resourcesFound, Dictionary`2 functionsToDefine,        
CimDSCParser parser, String mof, DSCResourceRunAsCredential runAsBehavior)
   at Microsoft.PowerShell.DesiredStateConfiguration.Internal.DscClassCache.ImportKeywordsFromScriptFile(String fileName, PSModuleInfo module, ICollection`1 resourcesToImport, ICollection`1      
resourcesFound, Dictionary`2 functionsToDefine, List`1 errorList, IScriptExtent extent)
   at Microsoft.PowerShell.DesiredStateConfiguration.Internal.DscClassCache.LoadPowerShellClassResourcesFromModule(PSModuleInfo primaryModuleInfo, PSModuleInfo moduleInfo, ICollection`1
resourcesToImport, ICollection`1 resourcesFound, List`1 errorList, Dictionary`2 functionsToDefine, Boolean recurse, IScriptExtent extent)
   at CallSite.Target(Closure, CallSite, Type, PSModuleInfo, List`1, Dictionary`2)
    Source         : System.Management.Automation
    HResult        : -2146233087
    StackTrace     : 
   at System.Management.Automation.ExceptionHandlingOps.ConvertToMethodInvocationException(Exception exception, Type typeToThrow, String methodName, Int32 numArgs, MemberInfo memberInfo)
   at CallSite.Target(Closure, CallSite, Type, PSModuleInfo, List`1, Dictionary`2)
   at System.Management.Automation.Interpreter.DynamicInstruction`5.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : CimException
InvocationInfo        : 
    ScriptLineNumber : 2496
    OffsetInLine     : 5
    HistoryId        : -1
    ScriptName       : C:\Users\mikey\Documents\PowerShell\Modules\PSDesiredStateConfiguration\2.0.7\PSDesiredStateConfiguration.psm1
    Line             : $resourcesFound = [Microsoft.PowerShell.DesiredStateConfiguration.Internal.DscClassCache]::ImportClassResourcesFromModule($Module, $Resources, $functionsToDefine)

    PositionMessage  : At C:\Users\mikey\Documents\PowerShell\Modules\PSDesiredStateConfiguration\2.0.7\PSDesiredStateConfiguration.psm1:2496 char:5
                       +     $resourcesFound = [Microsoft.PowerShell.DesiredStateConfiguration …
                       +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    PSScriptRoot     : C:\Users\mikey\Documents\PowerShell\Modules\PSDesiredStateConfiguration\2.0.7
    PSCommandPath    : C:\Users\mikey\Documents\PowerShell\Modules\PSDesiredStateConfiguration\2.0.7\PSDesiredStateConfiguration.psm1
    CommandOrigin    : Internal
ScriptStackTrace      : at ImportClassResourcesFromModule, C:\Users\mikey\OneDrive -
Microsoft\Documents\PowerShell\Modules\PSDesiredStateConfiguration\2.0.7\PSDesiredStateConfiguration.psm1: line 2496
                        at Get-DscResource<Begin>, C:\Users\mikey\Documents\PowerShell\Modules\PSDesiredStateConfiguration\2.0.7\PSDesiredStateConfiguration.psm1: line   
3967
                        at <ScriptBlock>, <No file>: line 1

Environment data

Name                           Value
----                           -----
PSVersion                      7.3.6
PSEdition                      Core
GitCommitId                    7.3.6
OS                             Microsoft Windows 10.0.22621
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Version

2.0.7

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions