Terminating error in PowerShell not stopping function execution

40 Views Asked by At

I had the following snippet in a function I'm writing to check whether a directory is writable by the current user or not:

  # Write temp file to see if we can
  $checkPermArgs = @{
    Path = $tempFilePath
    ErrorAction = 'SilentlyContinue'
    ErrorVariable = 'wError'
  }
  Write-Verbose "Writing file ${tempFilePath}"
  "$($MyInvocation.MyCommand) check $([DateTime]::Now)" | Out-File @checkPermArgs
  
  # Check for any errors
  if( $wError ) {
    return $false
  } else {
    $true
  }

My understanding is that IOException should be a terminating error, but for some reason I'm getting continued execution of my function. If I try to write to $tempFilePath, and get an IOException, instead of terminating my function it continues and evaluates the if( $wError ) statement, returning and displaying $false in the console, resulting in the following output:

Out-File: 
Line |
  34 |  … cation.MyCommand) check $([DateTime]::Now)" | Out-File @checkPermArgs
     |                                                  ~~~~~~~~~~~~~~~~~~~~~~~
     | Access to the path '/path/to/tempFile.txt' is denied.
False

Since the goal is to still process the error without displaying it to the end user, I realized I goofed and wrapped Out-File in a try/catch block instead, but I'm curious why a terminating error here isn't actually terminating execution of the function.

0

There are 0 best solutions below