我们正在尝试在一个
即使添加了
Pester
测试中检查 Invoke-Command
的 ErrorVariable
中的值。但由于某种原因,-ErrorVariable
没有被实例化。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'
}
}
即使添加了
[CmdletBinding()]
选项,它仍然没有填充。我们在这里缺少什么?