Impact of the valuemap and values

This topic contains 5 replies, has 2 voices, and was last updated by Profile photo of Justin King Justin King 2 years, 2 months ago.

  • Author
    Posts
  • #19190
    Profile photo of Justin King
    Justin King
    Participant

    POssibly an easy one but I figured I'd throw it out there anyway.

    I'm working on an SCCM DSC module to lock down some collections (stop tweaking my queries!!!) and ran into something I just wanted a bit of clarification on.

    In SCCM, there are basically two types of collections that Admins refer to as "User" and "Device", but in WMI are actaully stored as types "1" and "2".

    So in my fancy collection resource schema, I defined what is now my first question:

    [Write, ValueMap{"1","2"}, Values{"User","Device"}] String CollectionType;

    My *hope* is that this will allow me to use friendly names in the DSC configuration, while still passing on the raw 1/2 back to my resource. Is this assumption correct or am I misusing this schema definition?

    If I'm correct that leads me o the resource itself and the get-targetresource function. Documentation here is a little fuzzy so when returning the settings back to DSC ... should I return the raw value ("1","2") or should I translate it as these values will be matched against the configuration ("user","device") as typed?

    With all the "work" seemingly being done in the other two functions I get a bit confused when/how the test-targetresource gets called.

    Thanks in advanced!

  • #19199
    Profile photo of Steven Murawski
    Steven Murawski
    Participant

    The valuemap will work for passing in parameters to your configuration – if a configuration specified User, your functions would get the "1" back.

    Get-DscConfiguration (which is what calls Get-TargetResource) does NOT translate those values back and returns the numerical or string value assigned in your Get-TargetResource function.

    Test-TargetResource is one of the most important functions. That runs to determine whether or not to run Set-TargetResource. If Test-TargetResource returns $false, then Set-TargetResource will run. If it returns $true, then Set-TargetResource will be skipped.

  • #19200
    Profile photo of Steven Murawski
    Steven Murawski
    Participant

    Connect bug filed on the valuemap not being applied from Get-DscConfiguration.

    https://connect.microsoft.com/PowerShell/feedbackdetail/view/985463

  • #19219
    Profile photo of Justin King
    Justin King
    Participant

    Thanks for the reply!

    So because the valuemap isn't used properly on a Get-DSCConfiguration return, , does this mean for now in the get-targetresource I should set my variable to send back the "friendly" variable rather than the actual? For example I should use a response such as:

    CollectionType = if($CMCollection.CollectionType -eq '1'){'User'}elseif($CMCollection.CollectionType -eq '2'){'Device'}else{'Unknown'}
    

    Basically as opposed to something much simpler like so:

    CollectionType = $CMCollection.CollectionType
    

    Apologize for my questions ... just trying to make sure I understand "where" the valuemap is applied (and not returned) in this whole process ... aka when I need to make sure I call things appropriately.

  • #19511
    Profile photo of Steven Murawski
    Steven Murawski
    Participant

    Justin, I would use the first example (though a switch statement might make more sense for samples with more than a couple of values), unless you already have an enumeration that you could return.

    You could also create a hashtable in your resource module and use that for lookups.. like $CollectionTypeLookup = @{ 1 = 'User'; 2 = 'Device' } then just return $CollectiontTypeLookup[$Cmcollection.CollectionType]

  • #19513
    Profile photo of Justin King
    Justin King
    Participant

    Yeah I could see a switch value being cleaner ... my powershell training wheels are still on at times:)

    Thanks for the response.

You must be logged in to reply to this topic.