Reputation: 8319
I'm trying to figure out how to capture an error message for the following line of PowerShell code (for example, if the user running the script doesn't have permission to read the file):
[IO.File]::ReadAllText("C:\[test].txt")
With Get-Content
, I can simply specify the ErrorAction
and ErrorVariable
parameters. This does not seem to be the case with the above line of code.
Thanks!
Upvotes: 1
Views: 1428
Reputation: 301347
Use a try...catch block
try{
[IO.File]::ReadAllText("C:\blah")
}
catch{
#handle here. Catch specific exceptions as well.
}
Upvotes: 1