Pester and Hashtables (Passing in Parameters)

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Wei-Yen Tan Wei-Yen Tan 6 months, 3 weeks ago.

  • Author
    Posts
  • #60934
    Profile photo of Wei-Yen Tan
    Wei-Yen Tan
    Participant

    Hi guys,

    I am writing a function that builds a hash table to pasbut what ds parameters into invoke-pester. Had the inspiration from this post How to Pass Parameters to a Pester Test Script

    I have managed to build the hash table and the parameters pass through.

    In the function code The hashtable looks like this:

    $finalhash = @{ 'Path' = $testpath; 'Parameters' = $hash }

    I have constructed the $hash hashtable to mimic the layout invoke-pester wants.
    Up to this point if I do Invoke-Pester $finalhash it works great.

    However what I would like is to include other parameters into the function as well programatically.

    For example my function would read something like

    Invoke-POVTest $a -testpath c:\test\test.test.ps1 -quiet -passthru

    I would like to include both switch parameter into the underlying invoke-pester $finalhash so when i enter those switches it will add in the parameters appropriately with having to use a long condition? Is it possible to do it with Dynamic parameters? I cant picture what the end result will be like.

    Many thanks in advance for the help.

    WY

  • #60948
    Profile photo of Don Jones
    Don Jones
    Keymaster

    I'm sorry, but I'm not following what you're trying to do. I don't understand "builds a hash table to pasbut what ds parameters" unfortunately, sorry.

    Are you asking how to construct $hash?

  • #60958
    Profile photo of Wei-Yen Tan
    Wei-Yen Tan
    Participant

    Hi Don,

    Sorry about that,my tablet key board is playing up but that should have read "pass parameters into invoke-pester"

    Anyway I have constructed the $hash to feed into $finalhash.

    It works great.

    What I would like is to have the ability to add in parameters like -quiet and passthru to feed into the underlying invoke-pester command

    I know I can do this easily if it was just one parameter, but when it comes to two or more it will turn into a bit of a hell...when i do if statements ie for example

    if ($quiet){
        Invoke-Pester $finalhash -quiet
      } 
    elseif($quiet -and $passthru){
        Invoke-Pester $finalhash -passthru -quiet
    }else{
        invoke-pester $finalhash
    }
    
    

    I know thats one way of doing it and its great if its just two but as i add more parameters i can see that it will cause more complications...I was wondering if there was another way that I can add -quiet or -passthru.

    Many thanks Don,

    regards,

    Wei-Yen Tan

    PS. I am sorry about the code formatting

  • #60964
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Yeah, so you'd just pass "Quiet"=$True for a switch parameter. The $hash from your original example would be a hash table, of course, with whatever parameters you needed. Does that help?

  • #60973
    Profile photo of Wei-Yen Tan
    Wei-Yen Tan
    Participant

    Thanks Don,

    I reread my function and it read as:
    Invoke-Pester -Script $finalhash

    So looking at it again adding to the finalhash won't help.

    So I started playing and created a hashtable like so:

    $param = @{
    'Script' =$finalhash
    'passthru' = $true
    }

    Then splatted it as invoke-pester @param and this worked. 🙂

    Which means that i can have a base $param = @{ 'script' = $finalhash } and then use the add method to add additional parameters.

    I just tested using $param.add('passthru', $true) and splatting that to invoke-pester it works nicely.

    Thanks Don for putting me on the right path.

You must be logged in to reply to this topic.