I have a custom module wrapping an external command (csrun.exe
), and parses the output so I can use it in PowerShell.
Everything just about works except if the external command writes to stderror
, and clearing the error in my cmdlet doesn't seem to fully work. It will clear (i.e. $error.count
is 0
and $lasterrorcode
is 0
, but once I return to the script that is calling my cmdlet, $error
and $lasterrorcode
are no longer clear and the error in $error
references the underlying exception for the external command
System.Management.Automation.RemoteException: The compute emulator is not running.
I've attempted, try-catches, clearing the mentioned variables. Regardless, the calling script retains a reference to the error.
CustomModule.psm1
$__azureEmulatorPath = "C:\Program Files\Microsoft SDKs\Azure\Emulator\"SDKs\Azure\Emulator\"
$__azureEmulator = __azureEmulatorPath + "csrun.exe"
function Get-EmulatorStatus() {
[OutputType([ComputeEmulatorStatus])]
[cmdletbinding()]
param()
$output = (& $__azureEmulator /status | Out-String)
if ($error.Count -gt 0 -or $LASTEXITCODE -ne 0) {
Write-Host ($Error | Format-List -Force | Out-String)
Write-Host Clearing Error and Continuing
$error.Clear()
$LASTEXITCODE = 0
}
#error from command cleared here
return $output
}
export-modulemember -function *
Test.ps1
import-module "CustomModule.psm1" # definew cmdlet Get-EmulatorStatus
$status = Get-EmulatorStatus
# even though error cleared in cmdlet, still here
Write-Host Write-Host Error $LASTEXITCODE, $Error.Count
Write-Host ($Error | Format-List -Force | Out-String)
[CmdletBinding]
? If so, it should support-ErrorAction Ignore
, which will not fill the$Error
variable.ErrorAction
isn't ideal because I only want to ignore the underlying call to the command because I know its an error I want to ignore. If anything else comes up I want the error to bubble up.