Try, Catch - Custom Errors


hey, using powershell v2 , have question try catch.  able use try catch fine catch error "divsion 0".  when want catch custom error, example error outputted enter-pssession, won't catch.  have code (see below), never catch's, when type bad password, red error text enter-pssession stating bad username/password displayed on screen, instead of custom message , actions.  thoughts?  thanks!

try
{
    enter-pssession -computername server1.domain.local -credential domain\admin
}
catch
{
    write-host "failed connect remote server."
    return
}
write-host "successfully connected remote server."

another way more predictable try/catch behavior set $erroractionpreference = 'stop' @ top of scripts, or in profile.  setting causes errors treated terminating errors, try/catches triggered in cases.  if accustomed c# style of coding, think simplest way go.

the whole terminating/non-terminating error thing pretty frustrating if ask me.  guess upside "minor" errors won't cause whole script crash, makes error handling more complicated , confusing.  it's nice have control on how different errors affect script flow, don't why default setting isn't 'stop.'  oh :)

-lincoln



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Motherboard replacement

Cannot create Full Text Search catalog after upgrading to V12 - Database is not fully started up or it is not in an ONLINE state

Remote Desktop App - Error 0x207 or 0x607