Forum Discussion
Get-NetIPAddress does not respect $ErrorActionPreference = "Stop"
- Oct 01, 2024
Based on their first paragraph, it seems they have indeed tried that.
It appears to be a bug with the Get-NetIPAddress commandlet. Specifically, it's looking at the globally-scoped ErrorActionPreference variable rather than the locally-scoped version.
Here's a quick proof.
Script #1: Baseline.
Write-Output -InputObject "#1 ErrorActionPreference = $ErrorActionPreference"; Get-NetIPAddress -IPAddress 270.0.0.0; Write-Output -InputObject "#2 ErrorActionPreference = $ErrorActionPreference";Output #1.
Script #2: Setting ErrorActionPreference locally.
$ErrorActionPreference = "Stop"; Write-Output -InputObject "#1 ErrorActionPreference = $ErrorActionPreference"; Get-NetIPAddress -IPAddress 270.0.0.0; Write-Output -InputObject "#2 ErrorActionPreference = $ErrorActionPreference";Output #2.
Script #3: Setting ErrorActionPreference globally.
try { $OldStopAction = $Global:ErrorActionPreference; $Global:ErrorActionPreference = "Stop"; Write-Output -InputObject "#1 ErrorActionPreference = $Global:ErrorActionPreference"; Get-NetIPAddress -IPAddress 270.0.0.0; Write-Output -InputObject "#2 ErrorActionPreference = $Global:ErrorActionPreference"; } catch { throw; } finally { $Global:ErrorActionPreference = $OldStopAction; }Output #3.
Script #4: Using parameter defaults.
You can read more about these here:
try { $ParamSpecification = "Get-NetIPAddress:ErrorAction"; if ($null -eq $PSDefaultParameterValues[$ParamSpecification]) { $PSDefaultParameterValues.Add($ParamSpecification, "Stop"); $RemoveParam = $true; } Write-Output -InputObject "#1 ErrorActionPreference = $ErrorActionPreference"; Get-NetIPAddress -IPAddress 270.0.0.0; Write-Output -InputObject "#2 ErrorActionPreference = $ErrorActionPreference"; } catch { throw; } finally { if ($RemoveParam) { $PSDefaultParameterValues.Remove($ParamSpecification); } }Output #4.
Conclusion
Microsoft has to fix this one. All you can control is working around it through either including -ErrorActionPreference in every single call or using one of the workarounds from scenarios three or four above.
Scenario 4 is the more graceful of the two options presented but both are just as cumbersome.
Cheers,
Lain
- LainRobertsonOct 01, 2024Silver Contributor
Based on their first paragraph, it seems they have indeed tried that.
It appears to be a bug with the Get-NetIPAddress commandlet. Specifically, it's looking at the globally-scoped ErrorActionPreference variable rather than the locally-scoped version.
Here's a quick proof.
Script #1: Baseline.
Write-Output -InputObject "#1 ErrorActionPreference = $ErrorActionPreference"; Get-NetIPAddress -IPAddress 270.0.0.0; Write-Output -InputObject "#2 ErrorActionPreference = $ErrorActionPreference";Output #1.
Script #2: Setting ErrorActionPreference locally.
$ErrorActionPreference = "Stop"; Write-Output -InputObject "#1 ErrorActionPreference = $ErrorActionPreference"; Get-NetIPAddress -IPAddress 270.0.0.0; Write-Output -InputObject "#2 ErrorActionPreference = $ErrorActionPreference";Output #2.
Script #3: Setting ErrorActionPreference globally.
try { $OldStopAction = $Global:ErrorActionPreference; $Global:ErrorActionPreference = "Stop"; Write-Output -InputObject "#1 ErrorActionPreference = $Global:ErrorActionPreference"; Get-NetIPAddress -IPAddress 270.0.0.0; Write-Output -InputObject "#2 ErrorActionPreference = $Global:ErrorActionPreference"; } catch { throw; } finally { $Global:ErrorActionPreference = $OldStopAction; }Output #3.
Script #4: Using parameter defaults.
You can read more about these here:
try { $ParamSpecification = "Get-NetIPAddress:ErrorAction"; if ($null -eq $PSDefaultParameterValues[$ParamSpecification]) { $PSDefaultParameterValues.Add($ParamSpecification, "Stop"); $RemoveParam = $true; } Write-Output -InputObject "#1 ErrorActionPreference = $ErrorActionPreference"; Get-NetIPAddress -IPAddress 270.0.0.0; Write-Output -InputObject "#2 ErrorActionPreference = $ErrorActionPreference"; } catch { throw; } finally { if ($RemoveParam) { $PSDefaultParameterValues.Remove($ParamSpecification); } }Output #4.
Conclusion
Microsoft has to fix this one. All you can control is working around it through either including -ErrorActionPreference in every single call or using one of the workarounds from scenarios three or four above.
Scenario 4 is the more graceful of the two options presented but both are just as cumbersome.
Cheers,
Lain