none
DSC: What does Get-TargetResource do?

    Question

  • I have been creating a collection of Azure related DSC resources, and mostly because it isn't clear what the purpose is, I've been leaving Get-TargetResource with a default

    I imagine (read am hoping) that this can be used to get values from the resource that can then be piped back into the logic.

    I'm poking around trying to figure out how, but it would be good if anyone has been there already and can point me in a direction...

    Friday, March 28, 2014 6:50 AM

Answers

  • Having done a little debugging and poking around in the innards of DSC it seems that Get-TargetResource is only called by the cmdlet  Get-DscConfiguration  

    It doesn't appear to play any part in the automation aspect of DSC, instead it is merely there for an admin to check how a server is configured at that point in time. Hopefully someone can tell me if this is not the case but until I see a reason for not doing so - or a reason that actually is of benefit to me as an admin - I'm going to write a generic Get-TargetResource that simply returns what it is fed and paste it into every resource I write. 

    So now I *completely* understand that aspect of DSC I'll get back to writing some providers! 

    • Marked as answer by a-z-l Tuesday, April 01, 2014 1:23 PM
    Tuesday, April 01, 2014 12:13 AM

All replies

  • Is their a question in this discussion?

    Start here: http://technet.microsoft.com/en-us/library/dn249927.aspx

    Get-Target Resource is a dummy function. You must write it to supply arguments to the custom MOF.


    ¯\_(ツ)_/¯

    Friday, March 28, 2014 12:43 PM
  • In theory, you'd have the following pseudocode for your Test-TargetResource, Get-TargetResource and Set-TargetResource functions:

    function Get-TargetResource
    {
        return CurrentStatus
    }
    
    function Test-TargetResource
    {
        return (Get-TargetResource equals Params)
    }
    
    function Set-TargetResource
    {
        CurrentStatus = Get-TargetResource
    
        For Each Difference Between Params and CurrentStatus
        {
            Fix Difference
        }
    }

    In practice, people seem to just put most of the logic straight into Test-TargetResource and Set-TargetResource, and don't do much with the Get- function.
    Friday, March 28, 2014 1:12 PM
  • Is their a question in this discussion?

    Start here: http://technet.microsoft.com/en-us/library/dn249927.aspx

    Get-Target Resource is a dummy function. You must write it to supply arguments to the custom MOF.


    ¯\_(ツ)_/¯

    The question was, can anyone point me in a direction (I'm not entirely expecting a right direction as yet!)

    And according to that very page (which I have spent far too long studying incase I missed something obvious!) Get-TargetResource has some very stringent requirements considering it is a dummy function.

    I personally don't think it is a dummy function, I'm just not sure its been very well implemented yet.

    Friday, March 28, 2014 1:55 PM
  • It is a template.  You have to write the function.

    As David pointed out.  Most just include it in other functions.


    ¯\_(ツ)_/¯

    Friday, March 28, 2014 2:01 PM
  • The problem with approaching it from that angle is that you can't have variables that cross between resources.

    What I'd really like to do is something akin to this -

    Configuration BuildEnv
    {
     Import-DscResource -module ConfigureAzure
       Node $AllNodes.Where{$_.Role -eq "DeploymentServer"}.NodeName
       {
    
        CreateService Sname
            {
                Key = "111364875" 
                Name = "somename"  
                Ensure = "present" 
            } 
        Deploy Server1
            {
                Key = "111364876" 
                ServiceName = $Sname.output 
                Ensure = "present" 
                Locations = "West Europe"
                DependsOn = "[CreateService]Sname"
                ComputerName = "AZTest"
                Subnet = "London-Servers" 
                AzureImageName = $node.AzureImageName
                Size = "Small" 
    
            }
       }
    }
    

    Where a Azure service is being created to deploy multiple Servers into. So it checks the name, realises it can't use the one specified and increments it with something.

    What I'd really like is in my deploy script to be able to call that output (i.e. $Sname.output) and use that in future parts of the scripts.

    If it comes to it I will have to build some external db etc to keep track of such things. But it would be really nice if it could be managed within DSC

    Friday, March 28, 2014 2:08 PM
  • That's the point I'm trying to get at, what is the purpose of writing that function. What does it do, what calls it and what decisions are made as a result of it being called. Because from what I have gleaned it is a function that is never called in the normal running of DSC.

    As my previous post explains I am trying to understand if it is possible to use Get-TargetResource to deliver configuration data to resources further down the chain because this would make configuration writing considerable more streamlined. (the alternative is that there needs to be some backend data store to keep the data that can't be tracked via DSC

    Friday, March 28, 2014 2:11 PM
  • That's the point I'm trying to get at, what is the purpose of writing that function. What does it do, what calls it and what decisions are made as a result of it being called. Because from what I have gleaned it is a function that is never called in the normal running of DSC.


    You've basically nailed it right there.  The reason people have been so lazy with that function so far is that it doesn't really matter at the moment.  As long as your Test-TargetResource and Set-TargetResource functions do their jobs well, no one is likely to notice if you don't even bother with Get-TargetResource.
    Friday, March 28, 2014 2:33 PM
  • The thing I am trying to find out is how can it be used, I can't imagine it was just thrown in there to make up the numbers! Yes maybe it is only very lightly supported, maybe there are much bigger plans for it in future PS versions. But it is there, I can't believe it is a completely dead function! Especially when documents like the one referenced earlier specify how important it is!

    I suppose I'd like to understand what its current use case is, so that I can figure out if it will help make my scripts more streamlined.

    Friday, March 28, 2014 3:01 PM
  • Many design patterns are defined with parts that are disconnected. This is to allow for easier generalization of process components. The Test/Get/Set pattern is a common design pattern. If you are writing a single use script you can ignore any of the steps if it gets the job done.

    The link posted is telling you how to structure a module that will allow you to write a custom provider for DSC PowerShell CmdLets.  If you are not well versed in how DSC works or how to use it you should ignore this until you fully understand DSC.

    It is my belief that you cannot write a provider module until you completely understand how to use DSC.


    ¯\_(ツ)_/¯

    Friday, March 28, 2014 3:01 PM
  • I am trying to find out the answer to a reasonably simple question. Under what circumstances is the Get-TargetResource called.

    I am currently writing a considerable number of resource modules, and they are doing exactly as I expect them to do. However I am also leaving the Get-TargetResource function very sparse (I currently leave a throw in there so I find out if it is called - which it isn't)

    I know how to structure the resource file. I am not asking for that. I am asking for the context that the function is called under so that I can write a function that actually provides some function!

    I do not believe that anyone understands this particular aspect of DSC because there is no documentation regarding its implementation. (I have spent a lot of time looking now)

    Please if you are as fully conversant with DSC as you suggest maybe you could enlighten us as to the conditions that must be set for Get-TargetResource to be called.

    (that does seem an awfully peculiar belief though, surely that would mean that nobody would ever be able to learn a new technology like DSC, because they wouldn't be able to write Resources because they didn't understand it because they couldn't write resources - or maybe I'm misunderstanding your point)

    Friday, March 28, 2014 3:22 PM
  • Get-TargetResource is called as part of a provider module.  It is called authomatically by the stock CmdLets when the provider is specified.

    Later I will put together a simple example.  The example in the link is complete but you need to follow it and biold all of thepieces to see howit works.

    Perhaps David has an example he can post.  You can also search for an example.  I will try to build one later this afternoon.


    ¯\_(ツ)_/¯

    Friday, March 28, 2014 3:41 PM
  • Oops.  Spoke too soon.  Looked in mu links and here is the example:

    The Get-TargetResource function implements all Key properties defined in the resource schema file. If a DSC resource requires the Required and/or Write properties to successfully fetch the state of the modeled entity, then Required or Write properties can also be in the Get-TargetResource input parameter list, and the values for these properties are propagated to Get-TargetResource during the command execution.

    http://blogs.msdn.com/b/powershell/archive/2013/11/15/hungry-for-more-windows-powershell-desired-state-configuration-resources.aspx


    ¯\_(ツ)_/¯

    Friday, March 28, 2014 3:44 PM
  • Try this:

    A rest asks "can I do this?"  If the answer is yes then we need to know what resources are needed to do it so we "get" the resources.  We canthen cakll "set" to configure the resources.

    A DSC uses all of these decision and implementation points to "enforce" a desired state.


    ¯\_(ツ)_/¯

    Friday, March 28, 2014 3:49 PM
  • The Get-TargetResource function implements all Key properties defined in the resource schema file. If a DSC resource requires the Required and/or Write properties to successfully fetch the state of the modeled entity, then Required or Write properties can also be in the Get-TargetResource input parameter list, and the values for these properties are propagated to Get-TargetResource during the command execution.

    For a moment I thought I'd got it solved! but it seems that doesn't mean what I (for a moment) think it mean. 

    I originally read that as 'put key values in Get, and when Get is called, Key values can be verified too' 

    For the moment I thought it might be suggesting that any missing attribute will be collected via Get (if it has a write or required property) Alas this isn't the case and get is only called under whatever unbeknown circumstances exist for calling it. 

    So thanks for the pointer! but it doesn't actually implement the functionality I'm looking for. 

    All I'm really looking for is this in the DSC output 

    VERBOSE: [WINDEV]: LCM:  [ Start  Get      ]  [[Deploy]Server1]

    But it is very very elusive 

    Friday, March 28, 2014 4:38 PM
  • Try this:

    A rest asks "can I do this?"  If the answer is yes then we need to know what resources are needed to do it so we "get" the resources.  We canthen cakll "set" to configure the resources.

    A DSC uses all of these decision and implementation points to "enforce" a desired state.

    Alas it doesn't appear to run like that. It uses Test to see what needs doing then Set to make it so! If you don't provide the initial parameters there is an error to say 

    The part that does intrigue me though, is the Read attribute of the schema.mof file. I suspect that has something to do with extracting data from Get, but it is far too vague to be useful. 

    Friday, March 28, 2014 4:48 PM
  • Sorry for the typos before.

    I think David has better advice.  Just dummy the function as you have done and use Test.  Passing things between functions is probably not possible because all they share is in the MOF and in the system being targeted.  Th eMOF is read only at execution time.

    When we set up a DSC we process the config and generate a MOF. My sense is that this would be where we would set all values that would act like "globals".

    Since I do not really understand what you purpose is I am not sure how to react. It appears that you may be trying to use DSC to do things that it is not intended to do.


    ¯\_(ツ)_/¯

    Friday, March 28, 2014 5:08 PM
  • Well my question is simply, under what circumstances does Get-* get called! It seems from digging around that the Mof Read property has something to do with how it works. 

    And mof files do understand the concept of variables, there is no reason why output from one mof element couldn't be passed to another. 

    The purpose is that once we have a good understanding of how these mechanisms work, we can write better scripts with them. We can start to use DSC to its full potential instead of ditching a third of its potential because we don't understand how it works. 

    And don't worry, we've all had the joys of attempting to reply from mobiles! Thanks for your help 

    Friday, March 28, 2014 11:15 PM
  • Having done a little debugging and poking around in the innards of DSC it seems that Get-TargetResource is only called by the cmdlet  Get-DscConfiguration  

    It doesn't appear to play any part in the automation aspect of DSC, instead it is merely there for an admin to check how a server is configured at that point in time. Hopefully someone can tell me if this is not the case but until I see a reason for not doing so - or a reason that actually is of benefit to me as an admin - I'm going to write a generic Get-TargetResource that simply returns what it is fed and paste it into every resource I write. 

    So now I *completely* understand that aspect of DSC I'll get back to writing some providers! 

    • Marked as answer by a-z-l Tuesday, April 01, 2014 1:23 PM
    Tuesday, April 01, 2014 12:13 AM
  • Wow, interesting thread - surprising that the question is not obvious. I've had the same question for a while, and also been looking for usage. I'm still not clear why "Get-DscConfiguration" even calls the Get-Resource, considering it's not even actually testing anything - it's telling you what was -planned- to be configured, but not the status (that's up to Get-DscConfigurationstatus, of course, and it doesn't even call the Get or the Test!).

    So, a-z-l, I see why you're asking, and I think the content within the answers is a little sad, because it's true - I can't easily find any resources at all that use their own "Get". In my mind, I'd -imagine- the Get was originally intended to support DRY code - in most cases, Test and Set are doing a lot of very similar retrieve / validate work and I imagine that that shared code was intended to be put into the Get. Obviously the resources function 100% without it, but it would make more sense to centralize potentially complex "Get" logic.

    Monday, September 07, 2015 11:56 AM
  • Incidentally, a follow up here as I recheck the guidelines at http://blogs.msdn.com/b/powershell/archive/2014/11/18/powershell-dsc-resource-design-and-testing-checklist.aspx and probably a correction on my earlier post - if Get returns the -actual- state of the item, it's useful for reporting not just if the resource is -in- compliance or not (i.e. Test), but essentially what the system state's actual values are, in order to do a property-by-property comparison.

    Monday, September 07, 2015 5:10 PM