Setter function

Welcome Forums General PowerShell Q&A Setter function

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

 
Participant
2 weeks, 2 days ago.

  • Author
    Posts
  • #126707

    Participant
    Points: 52
    Rank: Member

    Hello

    I am building an API wrapper, with 3 simple functions "Get-MyObject", "New-MyObject" and "Set-MyObject".

    Set-MyObject issues a PUT request to update the object, with approx. 20 fields available to be updated, each represented by an optional parameter. Is there a built-in way to inspect the parameters that the user has supplied, rather than checking each parameter in turn, i.e. I don't want 20 instances of

    if($property1){do something}

    In the psuedo-code below, I'm looking for "$supplied_params":

    foreach($param in $supplied_params)
    {
      if($param){
        $myobject.property1=$param
      }
    }
    

    Many thanks in advance.

  • #126708

    Participant
    Points: 866
    Helping Hand
    Rank: Major Contributor

    You can use $PSBoundParameters, it is an automatic variable which gets populated inside a script/function.

    an example below.

    function test {
        param($r,$t)
        $PSBoundParameters
    }
    
    #below executions
    test
    test 1
    tert 1 2
    
  • #126720

    Participant
    Points: 52
    Rank: Member

    Perfect, exactly what I was looking for.

    Thanks

  • #126810

    Participant
    Points: 290
    Helping Hand
    Rank: Contributor

    You could probably do a nice, concise switch ($PSBoundParameters.Keys) {} and just work from there. 🙂

You must be logged in to reply to this topic.