Forum Discussion

noamlerner's avatar
noamlerner
Copper Contributor
Sep 30, 2024
Solved

Get-NetIPAddress does not respect $ErrorActionPreference = "Stop"

I have a script that is set with `$ErrorActionPreference = "Stop"` and `Get-NetIPAddress` fails, but the script continues to run for some reason. Passing `-Error Stop` to `Get-NetIPAddress` will fix that, but I would expect to stop without it as well.

 

I read both about ErrorActionPreference and Get-NetIPAdress and didn't see any mention of this behavior.

 

Here is a short repro - link

 

I was wondering if someone can help me understand why this happens, and whether there are any more magical cmdlets that are behaving the same.

 

Thanks. 

For context, I'm using powershell version 5.1.

  • LainRobertson's avatar
    LainRobertson
    Oct 01, 2024

    AndresGorzelany 

     

    Based on their first paragraph, it seems they have indeed tried that.

     

    noamlerner 

     

    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

    • LainRobertson's avatar
      LainRobertson
      Silver Contributor

      AndresGorzelany 

       

      Based on their first paragraph, it seems they have indeed tried that.

       

      noamlerner 

       

      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

Resources