1

I have a script where I mount with the command "New-PSDrive" a network drive. Now, since the script is running as a "cronjob" on a server I want to have some error detection. If for any reason the command New-PSDrive fails the script should stop executing and notify that something went wrong. I have the following code:

Try {
    New-PSDrive -Name A -PSProvider FileSystem -Root \\server\share
} Catch {
    ... handle error case ...
}
... other code ...

For testing reasons I specified a wrong server name and I get the following error "New-PSDrive : Drive root "\wrongserver\share" does not exist or it's not a folder". Which is OK since the server does not exists. But the script does not go into the Catch clause and stop. It happily continues to run and ends up in a mess since no drive is mounted :-)

So my question, why? Is there any difference in Exception handling in PowerShell? I should also note that I'm a noob in PowerShell scripting.

Bye, Martin

2

There are two types of errors in Powershell, terminating errors and non-terminating errors. Check out the help for about_try_catch_finally for more info.

If I try this

try
{
jimjim-cmdlet
}
catch
{
"It's a jimjim error!"
}

the nonsense cmdlet will generate a terminating error which will be caught by the catch block.

The code you are running is not throwing a terminating error, so execution throws the non-terminating error and continues after the catch block.

Also see this page, http://powershell.com/cs/forums/p/521/703.aspx, for more information.

I am still a little fuzzy on when exactly a terminating error is thrown as opposed to a non-terminating error (perhaps more knowledgable folks can help out).

  • 1
    Yes, this is exactly right -- and it is sometimes difficult to know if/when a cmdlet is going to throw a terminating error or not. Which is why powershell gives you some options -- you can set the automatic variable $errorActionPreference to 'Stop' in your script and then ANY errors will be treated as terminating errors (and thus trigger the catch block). Alternately, and perhaps better in this specific scenario, specific cmdlets have the -ErrorAction paramter; just add "-ErrorAction Stop" to your New-PSDrive line and all errors from that specific cmdlet will be caught. – jbsmith Jul 26 '11 at 5:51
  • 1
    $ErrorActionPreference ="Stop" will not stop on all non-terminating errors. Adding -ErrorAction Stop explicitly will. I've hit two instances of this recently, and am currently in the midst of a discussion with the PowerShell team on that topic... – Matthew Wetmore Mar 8 '17 at 21:00
2

I know I'm late to the party, but here is my take on this.

For non-terminating errors you could read the error variable at index 0 like so

try {    
   New-PSDrive -Name "H" -PSProvider filesystem -Root \\SERVER\Folder -Scope 1
if ($Error[0].Exception.Message -like "*does not exist*")
{
   throw [System.IO.FileNotFoundException]::new("Could not find folder")
}
}
Catch [System.IO.FileNotFoundException]
{
    break
}
Catch 
{
    # Other error handling
}

This should then break your script, you can add additional logic and logging in the catch block.

  • Shouldn't the $error be in catch ? This seems like a really bad pseudo code? – MadBoy Oct 6 '17 at 20:12
  • If you read the original post, this is for non-terminating errors. That's why I use throw.. – Nigel Tatschner Oct 8 '17 at 11:14
  • Throw is fine, i mean using if inside try? If there is an error on new-psdrive it should skip to catch no? – MadBoy Oct 8 '17 at 11:22
  • In this specific case, it wasn't being handled, hence catching other errors in the outer try catch and evaluating the error elsewhere – Nigel Tatschner Oct 18 '17 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.