3

We're trying to check the value in the ErrorVariable of Invoke-Command within a Pester test. But for one reason or another the -ErrorVariable is not instantiated.

Describe 'test ErrorVariable' {
    Mock Invoke-Command {
        #[CmdletBinding()]
        #param (
        #    [String[]]$ComputerName,
        #    $ScriptBlock
        #)

        $ErrorId = 'NetworkPathNotFound,PSSessionStateBroken'
        $TargetObject = 'UnknownHost'
        $ErrorCategory = [System.Management.Automation.ErrorCategory]::OpenError
        $ErrorMessage = "Connecting to remote server $TargetObject failed with the following error message : WinRM cannot process the request. The following error occurred while using Kerberos authentication: Cannot find the computer $TargetObject. Verify that the computer exists on the network and that the name provided is spelled correctly. For more information, see the about_Remote_Troubleshooting Help topic."
        $Exception = New-Object -TypeName System.InvalidOperationException -ArgumentList $ErrorMessage
        $ErrorRecord = New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList $Exception, $ErrorId, $ErrorCategory, $TargetObject

        $ErrorRecord
    }

    it 'should be green because it should contain the TargetObject' {
        Invoke-Command -ComputerName TestComputer -ScriptBlock {1} -ErrorVariable ConnectionError
        $ConnectionError.TargetObject | Should -Be 'UnknownHost'
    }
}

Even when adding the [CmdletBinding()] option it's still not populated. What are we missing here?

DarkLite1
  • 13,637
  • 40
  • 117
  • 214
  • I have seen somewhere (can't remember when or where..) that setting the "ErrorVariable" parameter won’t do anything unless you also set the "ErrorAction" parameter. Maybe that is the case here as well?? – Theo Jul 06 '18 at 09:55
  • Thx for the tip @Theo, tried that but it didn't make a difference. – DarkLite1 Jul 09 '18 at 07:14

1 Answers1

3

You should use Write-Error:

Describe 'test ErrorVariable' {
    Mock Invoke-Command {
        $ErrorId = 'NetworkPathNotFound,PSSessionStateBroken'
        $TargetObject = 'UnknownHost'
        $ErrorCategory = [System.Management.Automation.ErrorCategory]::OpenError
        $ErrorMessage = "Connecting to remote server $TargetObject failed with the following error message : WinRM cannot process the request. The following error occurred while using Kerberos authentication: Cannot find the computer $TargetObject. Verify that the computer exists on the network and that the name provided is spelled correctly. For more information, see the about_Remote_Troubleshooting Help topic."
        $Exception = New-Object -TypeName System.InvalidOperationException -ArgumentList $ErrorMessage

        Write-Error -ErrorId $ErrorId -TargetObject $TargetObject -Category $ErrorCategory -Message $ErrorMessage -Exception $Exception
    }

    it 'should be green because it should contain the TargetObject' {
        Invoke-Command -ComputerName TestComputer -ScriptBlock {1} -ErrorVariable ConnectionError -ErrorAction SilentlyContinue
        $ConnectionError.TargetObject | Should -Be 'UnknownHost'
    }
}
  • Thanks, this was indeed what I was looking for. – DarkLite1 Jan 28 '19 at 07:20
  • This example helped me a lot, but it is a little dated. With modern versions of Pester, the Mock needs to be inside the "It", or in a "BeforeEach" or "BeforeAll" clause. I was testing with Pester v5.3. – Jean Libera May 20 '22 at 17:06