Author Posts

August 22, 2016 at 10:26 am

Hi,

so i have created a file full of pester tests, but at the start i had to hard code the server to connect to.

So when i run the script it always points at that server. Is there a way to pass the server to the pester test file dynamically?

ie can you do something like:

Invoke-Pester "$testsFolder\*.ps1" -ServerName "myServer"

or even

Invoke-Pester "$testsFolder\*.ps1" -ServerName "myServer", "myServer2"

  • This topic was modified 1 year, 12 months ago by  staggerlee011.

August 22, 2016 at 1:37 pm

Yep. You can put a param block in your tests.ps1 file, same as any other PowerShell script. When you run Invoke-Pester, you can tell it what parameters to pass to the test file like this (assuming you have a parameter named "ExampleParam" that's expecting a string):

Invoke-Pester -Script @{ Path = '.\SomeFile.Tests.ps1'; Parameters = @{ ExampleParam = 'Value to pass' } }

August 22, 2016 at 2:00 pm

Hi Dave,

Thanks for the reply!

Any idea what im doing wrong with the below (guessing i've over simplified?), tried to make the simplest test to check im connecting to a passed through server.

Calling script:

Invoke-Pester -Script @{Path = "$testsFolder\Parameter.test.ps1"; Parameters = @{$ServerName = 'MyServerName'}}

Paramter.test.ps1

Param (
[string]$ServerName
)
$Session = New-PSSession -ComputerName $ServerName

Import-Module sqlserver

Describe "test parameter in pester" {

It "Test Server name" {
Invoke-Command -Session $Session {$env:COMPUTERNAME} | Should be 'MyServerName'
}
}

Im getting angry replies from powershell not liking where im taking this!

August 22, 2016 at 2:01 pm

You've got one extra $ symbol in there. Should look like this:

@{Path = "$testsFolder\Parameter.test.ps1"; Parameters = @{ServerName = 'MyServerName'}}

(I removed the $ before ServerName)

August 22, 2016 at 2:03 pm

ha, awesome! thank you sir!