1

I have this infrastructure pester test. Code for demonstration purpose:

Describe 'WEB-Tests' {
    $servers = 'ServerA','ServerB'
    $sessions = @()
    foreach ($server in $servers) {
        $sessions += New-PSSession -ComputerName $server
    }
    $sessions | foreach-object {
        Context " Service is Running on $($_.ComputerName)" {
            $service = invoke-command -session $_ -scriptblock { get-service 'some service' }
            It "Service $($service.Name) should be Running" {
                $service.Status | Should be "Running"
            }
        }
    }
}

This works fine. If I replace the $sessions|foreach-object with $sessions|foreach-object -Parallel, i get this error -

New-PesterState: C:\Program Files\WindowsPowerShell\Modules\Pester\4.9.0\Functions\Context.ps1:77:128
Line |
  77 |  … '] .) -TestNameFilter $null -TagFilter @() -SessionState SessionState
     |                                                             ~~~~~~~~~~~~
     | Cannot process argument transformation on parameter 'SessionState'. Cannot convert the "SessionState" value of type "System.String" to type
     | "System.Management.Automation.SessionState".

Exception: The Context command may only be used from a Pester test script.

Please suggest on how to achieve parallelism here as there are hundreds of servers.

RESOLVED-I figured out a workaround by using jobs and it serves the purpose.

Describe 'WEB-Tests' {
    $servers = 'ServerA','ServerB'
    $sessions = @()
    foreach ($server in $servers) {
        $sessions += New-PSSession -ComputerName $server
    }
    Get-Job | Remove-Job  
    invoke-command -session $sessions -scriptblock { get-service W3SVC,WAS } -AsJob  
    $j = Get-Job | wait-job
    ($results = $j | Receive-Job) | out-null
    Context "IIS Services are Running" {
        foreach ($result in $results) {      
            It "Service $($result.Name) should be Running on $($result.PSComputerName)" {
                $result.Status | Should be "Running"
            }        
        }
    }
}
medinibster
  • 57
  • 2
  • 11
  • I tried working on this last night but i don't think that Pester really plays nice with Parallelization. Did you ever get anything working? I had an idea this morning that might work for you. I can try and code up an example if you're still looking for help. – Efie Oct 21 '20 at 14:26
  • thanks @Efie . alternate way to do this would be appreciated. – medinibster Oct 21 '20 at 15:32
  • Sorry for the delay I didn't get a chance to look at this again today until now. So it seems that Pester doesn't like when you try to use the -Parallel param inside of a test block. I was going to recommend pulling out logic in your 'It' section that runs the parallel tests and put it in a separate .ps1 file in the same folder. Then you could call that function which would handle the parallelism and you could test the overall results. – Efie Oct 21 '20 at 22:58
  • However, after 2 hours of messing around and searching, I wasn't able to get this to work. Frankly, trying to do anything in Parallel and retrieve any variables is absolutely awful. Half of everything you'll find involves workflows which no longer exist and everything else mentions $using: variables for context but I couldn't get anything to work. PowerShell simply isn't designed to work like this unfortunately :/ If you feel adventurous you could do it in C# and then import your class which does this logic. – Efie Oct 21 '20 at 22:59
  • @Efie thanks for trying. i was able to achieve something similar by using jobs. – medinibster Oct 22 '20 at 16:15
  • i ve updated the question with my solution above. – medinibster Oct 22 '20 at 16:25
  • 1
    @medinibster Out of curiosity, what is it that you're trying to accomplish? Pester doesn't execute the fixture scripts directly, and you're basically "fighting with Pester" over how to orchestrate execution of the test units – Mathias R. Jessen Oct 22 '20 at 16:38
  • @MathiasR.Jessen my goal is just to run pester tests on an array of servers and validate the status of iis services. and using the test results, output in nunit reports. not sure if there is an alternate way to run infrastructure tests on an array of servers. – medinibster Oct 23 '20 at 02:35

0 Answers0