Point Pester test file at different servers

This topic contains 4 replies, has 2 voices, and was last updated by  staggerlee011 1 year, 9 months ago.

  • Author
  • #51426



    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, 9 months ago by  staggerlee011.
  • #51449

    Dave Wyatt

    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' } }
  • #51459


    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'}}


    Param (
    $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!

  • #51461

    Dave Wyatt

    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)

  • #51464


    ha, awesome! thank you sir!

You must be logged in to reply to this topic.