Giter VIP home page Giter VIP logo

Comments (1)

briantist avatar briantist commented on July 18, 2024

This turned out to be more complicated than expected. The implementation of -NoValidateSet added in ed54f45 was easy enough, but it exposed a deeper problem with using -AsCustomObject.

Because of the way PowerShell generates a Script Method, my default template will always fail with these methods.

The reason is that the param() block specified in a Script Method is not implemented as such; instead each parameter is created as a local variable and its value is set to whatever the parameter supplied to the method is. This is why the [ValidateSet()] attributes fail too, so it's probably the same logic as a closure or perhaps internally it's just creating a closure.

But what this means is that there's no way inside of the method to programmatically determine what the parameters are; $MyInvocation.MyCommand has an empty .Parameters set.. because there are no parameters, just local variables. This means using $PSBoundParameters and even $args fails.

Because of that, there's no good way to use a templating system like this to dynamically build the [hashtable] that needs to be provided to Invoke-DscResource, because you'd need to know all the parameter names in advance or tease them out from the local variables.

As a result, the entire concept of using -AsCustomObject for what we're doing here is probably useless 99% of the time.

The one instance where I could see it being useful is when you have a very specific custom template definition that knows the parameter names already, so it doesn't need to use dynamic discovery of them. It might supply values for nearly all of them and maybe it uses a few dynamically by direct name.

Behavior Changes

  • -NoValidateSet is available for all parameter sets so it can be used for the other modes (not using a custom object) if that's desired.
  • When -AsCustomObject is specified, -NoValidateSet is automatically enabled, but it can be overridden with -NoValidateSet:$false if there's some reason for that.
  • When -AsCustomObject is specified and the default template is used, the call is allowed, but a warning is emitted explaining that the resulting methods will not work.

from idempotion.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.