Pester and Hashtables (Passing in Parameters)

Tagged: ,

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Wei-Yen Tan Wei-Yen Tan 1 week, 2 days ago.

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

    I have an exercpt from a function that I am writing here.

    Basically what i would like is to pass parameters through to Pester. I was able to do it by passing in variables into the hash. Works beautiful.

    Now I would like to build a dynamic key that i can pass into the hash table. In this case its the credential. The idea is that if the user supply the credential paremeter into this function then they can use their credentials to log into this machine. When I supply the credentials as a parameter it gives errors.

    I type this....

    PS C:\Windows\system32> Get-Childitem C:\reports\JSON -Recurse -Include *.json | select-object -ExpandProperty fullname | Export-DCOATestReport -path "C:\reports\JSON\test2" -testpath "C:\Users\weiyentan\Documents\GitHub\Datacom\DatacomBAUPesterTest.ps1" -credential $cred

    (The export-dcoattestreport is my function)

    and I get this error.

    Resolve-Path : Cannot find path 'C:\Windows\system32\System.Collections.Hashtable-PassThru' because it does not exist.
    At C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.3\Pester.psm1:588 char:17Resolve-Path : Cannot find path 'C:\Windows\system32\System.Collections.Hashtable-PassThru' because it does not exist.
    At C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.3\Pester.psm1:588 char:17
    

    Is there any way that I can build in dynamic paramneters and feed it into the hashtable as in this case if the credential parameter is not added it is not put in the hash table?

    An exercept of the function is below.
    Any help or guidance most appreciated.

    
    			$computername = $test.computername
    			$ipaddress = $test.ipaddress
    			$primarydns = $test.primarydns
    			$secondarydns = $test.secondarydns
    			$timeserver = $test.timeserver
    			$VMWareToolsVer = $test.VMWareToolsVer
    			
    			if ($Format -eq 'Word')
    			{
    				$Params = @{ Path = $testpath; Parameters = @{ ipaddress = $ipaddress; computername = $computername; primarydns = $primarydns; secondarydns = $secondarydns; timeserver = $timeserver; VMwareToolsVer = $VMWareToolsVer } }
    				
    				if ($credential)
    				{
    					$Params.add("Credential", $Credential)
    				}
    
    				Invoke-Pester -Script $Params -PassThru -Quiet | Format-Pester -Path $path -Basefilename $computername -Format Word
    			}
    
    
  • #58606
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    Imho You add Credential to outer hashtable but not inner

    	if ($credential)
    				{
    					$Params.Parameters.add("Credential", $Credential)
    				}
    
  • #58708
    Profile photo of Wei-Yen Tan
    Wei-Yen Tan
    Participant

    Thanks Max,

    That was it. It was in the outer hashtable. I am slowly getting to grips with this methods stuff.

You must be logged in to reply to this topic.