Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Using -ErrorVariable

$
0
0

I have a script to scan a folder for certain strings in the files there.  Ran into a situation where the file is too big for the script to process, so it throws an out-of-memory exception in red to the console.  Modified the script to use -ErrorVariable and -ErrorAction as follows:

  [string] $FileContents = Get-Content -readcount 0 -Path $File.FullName -ErrorVariable $ErrVar -ErrorAction SilentlyContinue
  if ($ErrVar -ne $null)
  {
   Write-Host $File.FullName -foregroundcolor red
  }

Now when it hits one of those files, I get nothing.  That makes sense since I told it to silently continue, but why am I not getting a hit on $ErrVar?  Should't it not be null?


Viewing all articles
Browse latest Browse all 6937

Trending Articles