Unable to Set Module PrivateData within DSC

Welcome Forums DSC (Desired State Configuration) Unable to Set Module PrivateData within DSC

This topic contains 6 replies, has 3 voices, and was last updated by

 
Participant
2 years, 7 months ago.

  • Author
    Posts
  • #45969

    Participant
    Points: 0
    Rank: Member

    I'm having a real problem with a DSC Resource i'm writing. I have a module split into various script files that get import from the psd1(NestedModules). Everything seemingly works accept that I can't set data stored in the PrivateData object of the psd1. From PowerShell elsewhere the command works fine, for example:

    $script:ModuleData = [ref]$MyInvocation.MyCommand.Module.PrivateData
    Write-verbose $ModuleData.Value['Setting']
    #Prints true
    $ModuleData.Value['Setting'] = $false
    

    In PowerShell the value is set fine and can be called again within or outside the module. However in DSC I get a Cannot index into a null array when setting the value but it can still be called.

    Help would be much appreciated.

  • #45992

    Participant
    Points: 0
    Rank: Member

    Ok, so you're writing a custom resource?

    Either way, is the Private Data read only? If so, can it simply be copied to another variable or something?

    • #46051

      Participant
      Points: 0
      Rank: Member

      Yes its a custom script resource that I am writing. The object shouldn't be readonly and the error "Cannot index into a null array" would not indicate that. If I make a copy of the variable I have no doubt that I would be able to set it but I was to be able to set the actual PSData so that it can be accessed by other scripts running on that machine. For full disclosure I will explain my solution more thoroughly.

      I have already written various DSC Resources to perform CRUD actions in VMware such as create VMs, datastores, dvports etc. In the first few versions of the resource I have common functions residing in every dscresource script for example connect-vcenter would be in vmwarevm, vmwarenetwork and vmwaredisk resources. To check whether I was already connected or not I had the state saved as a Boolean in the globalscope. I thought a lot of these things were bad practice so I separated the functions into various module and loaded them within nestedmodules of the psd1. Instead of making boolean public I wanted to store it in the PrivateData. Like I said before I have used this for other cases when its simply a script that's getting executed but when I try to set it in DSC it just doesn't work. I thought it may be that DSC doesn't support reference variables but trying to set it directly like below doesn't work either...

      (Get-Module Module1 -ListAvailable).PrivateData.Value['Setting'] = $true
      
  • #46164

    Participant
    Points: 0
    Rank: Member

    DSC Resources must be different. Maybe they load modules differently? Is it possible to use the $using: scope? I don't know if that will work between Resources, or if you need it to. Since I don't know the exact code, it's just a guess.

    With the $Using scope, you can at least store a variable safely within a Resource while it runs, no?

    Perhaps simply run a check at the start of each resource to check on the connection and set it? That's the best I have at this point.

    • #46167

      Participant
      Points: 0
      Rank: Member

      Thanks for you comments. I used the Debugger to step through the resource as it was running and it wasn't importing the whole module only the individual psm1. That became a problem when it tried to load the PrivateData because since the psd1 was never imported the PrivateData was never available under $MyInvocation.MyCommand.Module.PrivateData. After fully qualifying I was able to get and set the data I just now have a problem that its not seeing commands from a module I'm importing from my Base Module. A real pain but one I hopefully solve by simplying stepping through the code and analyzing as which points its available and at which not

  • #46169

    Participant
    Points: 0
    Rank: Member

    "I just now have a problem that its not seeing commands from a module I'm importing from my Base Module."

    DSC is still experimental and not working great. 🙁

  • #46623

    Participant
    Points: 0
    Rank: Member

    Ryan, can you share you resource?

The topic ‘Unable to Set Module PrivateData within DSC’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort