powershell >> Handling DotNet Exceptions

by csbodine » Tue, 13 Feb 2007 09:28:11 GMT

I found a short PS script to return a Microsoft.Win32.RegistryKey
object. The main piece that connects to the server to get the
registry is this line:

$rootkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($root,
$computer)

Unfortunately, if i don't have the permissions to access the registry
on $computer, then it throws an exception:

Exception calling "OpenRemoteBaseKey" with "2" argument(s):
"Attempted to perform an unauthorized operation."
At line:25 char:63
+ $rootkey =
[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey( <<<< $root,
$computer)

I tried to write a trap handler so the script could handle it properly
written in the same scope as the $rootkey.... line (literally the line
above it):

trap { write-host ("trapped " + $_.Exception.GetType().Name);
break; }

Unfortunately, its not trapping the exception. Am I not getting it
because the .Net piece is handled differently? How do I get around
this so my script doesn't freak out every time I hit a server I don't
have access to?


powershell >> RE: Handling DotNet Exceptions

by UmljaFM » Tue, 13 Feb 2007 16:07:07 GMT


using break will cause the script to halt rather than continue

try something like

trap {"trapped: " + $_; continue}
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk

Similar Threads

1. differences between ce 5.0's dotnet 2.0 and ce 6.0's dotnet 2.0

I have been trying to port my c# application, used for ce 5.0, to ce 6.0. 
At the previous ce 5.0 os, I added dotnet 2.0 catalog to os design and my c# 
application worked without problem. 
But at the new ce 6.0 os, although I surely added dotnet 2.0 catalog, my 
existing application have trouble with executing. When I execute the 
application, it can run, but there happens some 'native error'. 
The dotnet 2.0 from ce 6.0 have any differences with ce 5.0's? 

2. Non handling exception error occured. - Windows Media SDK

3. Exception handling in Vista Application

I'm amased about the poor handling of errors in vista applications in general.
When loading say an mpeg2 file in "windows movie maker" it shuts down 
without any reasoning why. Trying an mpeg1 file things seem to work ok, so 
thing look to a simple .Net-programmer as I am, like a poor way of handling 
exceptions.
When a program loads an unhandled file format I expect a popup of a simple 
note stating thus! The unexplained program crash has a nagative impact of a 
possibly quite usefull program.
Luckally third party programs, like the programs I write myself, are working 
ok.
But it seems that Microsoft own programmers don't obey their own paradigms!??

Simularly the elsewhere mentioned crashes on Excell (and other programs) 
exit is amasing! Also here when some Excell program does something unexpected 
Excel 2007 shuts down. (which happened recently when loading some game 
written using Excell, which is curious possibility I never would have thought 
up)

Greetings, as said Vista seems ok when using third party / own software. 
Microsoft's own software seems curiously shaky at best!

I hope some future update will provide more stability!

4. "KMODE EXCEPTION NOT HANDLED" appear when reboot

5. Powerpoint STOP error: kmode exception not handled

Powerpoint 2000 causes STOP error: 0x0000001E 
kmode_exception_not_handled in win32k.sys.  Happens during 
every presentation, not in safe mode.  Have replaced 
win32k.sys file with one from working PC, all updates 
installed, antivirus and remote control apps removed.

6. "KMODE EXCEPTION NOT HANDLED" appear when reboot