Setter function

Welcome Forums General PowerShell Q&A Setter function

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

 
Participant
3 months, 3 weeks ago.

  • Author
    Posts
  • #126707

    Participant
    Points: 96
    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: 1,628
    Helping Hand
    Rank: Community Hero

    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: 96
    Rank: Member

    Perfect, exactly what I was looking for.

    Thanks

  • #126810

    Participant
    Points: 457
    Helping Hand
    Rank: Contributor

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

The topic ‘Setter function’ is closed to new replies.

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