Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Question on Exception Trapping

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 09-07-2007   #1 (permalink)
Brandon Shell
Guest


 

Re: Question on Exception Trapping

Take a look here:
http://blogs.msdn.com/powershell/arc...and-throw.aspx

"Kuma" <kumasan76@xxxxxx> wrote in message
news:1189172983.689730.144700@xxxxxx
<sarcasm>
I have to admit, I am completely overwhelmed at the extensive
documentation available for PowerShell's exception handling.
</sarcasm>

Got that out of my system now, I have a problem with Trap. I have a
small script which basically takes a list of all computer names on my
part of the network (500 of them) and tries to see which ones have WMI
blocked by the Windows Firewall, which have the wrong admin password
(we change it frequently and miss a computer sometimes). I used
WIN32_LocalTime because its less information to pass across the
network. The problem is that I need two Trap statements. I have one
working, but am having a rough time with the other. Any ideas? Here's
the script. I cannot figure out the exception I need to Trap.

<snip>
$WMI_Comps = List of all computers from AD
$credential = Admin Credentials
foreach ($j in $WMI_Comps)
{
Get-WmiObject Win32_LocalTime -computername $j -ea silentlycontinue
-credential $credential | Foreach {$_.Server}
trap [System.UnauthorizedAccessException]
{$j+' Wrong Admin Password' >> $file;continue}
trap [****************************************************]
{$j+' Windows Firewall Blocking' >> $file;continue}
}

I know the Password one works but I can't get the other to work.
Here's the $error output I want to Trap.

PS » $error[0]
Get-WmiObject : The RPC server is unavailable. (Exception from
HRESULT: 0x800706BA)
At line:1 char:14
+ Get-WmiObject <<<< Win32_LocalTime -computername BLAHBLAH | ForEach-
Object {$_.__Server}

PS » $error[0].exception
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

PS » $error[0].fullyqualifiederrorid
GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

I've tried to trap just about every variation of
$error[0].fullyqualifiederrorid that i can think of or find. I'll
admit I'm new not only to powershell but to WMI so any ideas are
welcome. Thanks.


My System SpecsSystem Spec
Old 09-07-2007   #2 (permalink)
Kuma
Guest


 

Question on Exception Trapping

<sarcasm>
I have to admit, I am completely overwhelmed at the extensive
documentation available for PowerShell's exception handling.
</sarcasm>

Got that out of my system now, I have a problem with Trap. I have a
small script which basically takes a list of all computer names on my
part of the network (500 of them) and tries to see which ones have WMI
blocked by the Windows Firewall, which have the wrong admin password
(we change it frequently and miss a computer sometimes). I used
WIN32_LocalTime because its less information to pass across the
network. The problem is that I need two Trap statements. I have one
working, but am having a rough time with the other. Any ideas? Here's
the script. I cannot figure out the exception I need to Trap.

<snip>
$WMI_Comps = List of all computers from AD
$credential = Admin Credentials
foreach ($j in $WMI_Comps)
{
Get-WmiObject Win32_LocalTime -computername $j -ea silentlycontinue
-credential $credential | Foreach {$_.Server}
trap [System.UnauthorizedAccessException]
{$j+' Wrong Admin Password' >> $file;continue}
trap [****************************************************]
{$j+' Windows Firewall Blocking' >> $file;continue}
}

I know the Password one works but I can't get the other to work.
Here's the $error output I want to Trap.

PS » $error[0]
Get-WmiObject : The RPC server is unavailable. (Exception from
HRESULT: 0x800706BA)
At line:1 char:14
+ Get-WmiObject <<<< Win32_LocalTime -computername BLAHBLAH | ForEach-
Object {$_.__Server}

PS » $error[0].exception
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

PS » $error[0].fullyqualifiederrorid
GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

I've tried to trap just about every variation of
$error[0].fullyqualifiederrorid that i can think of or find. I'll
admit I'm new not only to powershell but to WMI so any ideas are
welcome. Thanks.

My System SpecsSystem Spec
Old 09-07-2007   #3 (permalink)
Kuma
Guest


 

Re: Question on Exception Trapping

On Sep 7, 10:49 pm, "Brandon Shell" <tshell.m...@xxxxxx> wrote: Been there. The link to Chapter 11 no longer works. And the sample
chapters available are not Trap and Throw.
I have the basic syntax down. I just cant get the exception to use.

My System SpecsSystem Spec
Old 09-07-2007   #4 (permalink)
RichS
Guest


 

Re: Question on Exception Trapping

You could just use a genreic trap statement that will catch all exceptions.
--
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


"Kuma" wrote:
Quote:

> On Sep 7, 10:49 pm, "Brandon Shell" <tshell.m...@xxxxxx> wrote:
Quote:
> Been there. The link to Chapter 11 no longer works. And the sample
> chapters available are not Trap and Throw.
> I have the basic syntax down. I just cant get the exception to use.
>
>
My System SpecsSystem Spec
Old 09-07-2007   #5 (permalink)
Edengundam
Guest


 

Re: Question on Exception Trapping

I hope this code would work for you.

$WMI_Comps = List of all computers from AD
$credential = Admin Credentials
foreach ($j in $WMI_Comps)
{
Get-WmiObject Win32_LocalTime -computername $j -ErrorVariable
myerror -credential $credential | Foreach {$_.Server}
if ($myerror.count -gt 0)
{
if ($myerror[0].Exception -is
[System.Runtime.InteropServices.COMException])
{$j+' Wrong Admin Password' >> $file;}
elseif ($myerror[0].Exception -is
[System.UnauthorizedAccessException])
{$j+' Windows Firewall Blocking' >> $file;}
}
}

Tao Ma
Best wishes.


My System SpecsSystem Spec
Old 09-07-2007   #6 (permalink)
Hal Rottenberg
Guest


 

Re: Question on Exception Trapping

Kuma wrote:
Quote:

> <sarcasm>
> I have to admit, I am completely overwhelmed at the extensive
> documentation available for PowerShell's exception handling.
> </sarcasm>
https://connect.microsoft.com/feedba...7271&SiteID=99


Description
get-help about_execution_environment references another topic called
error_handling, but there is no help for error_handling.
Comments
Thank you for reporting this bug. We will try to incorporate this in the next
release.
Posted by Microsoft on 10/4/2006 at 10:50 AM



--

Hal Rottenberg
blog: http://halr9000.com
powershell category:
http://halr9000.com/article/category...ng/powershell/
My System SpecsSystem Spec
Old 09-07-2007   #7 (permalink)
Kuma
Guest


 

Re: Question on Exception Trapping

On Sep 8, 12:27 am, "Edengundam" <feng_e...@xxxxxx> wrote:
Quote:

> I hope this code would work for you.
>
> $WMI_Comps = List of all computers from AD
> $credential = Admin Credentials
> foreach ($j in $WMI_Comps)
> {
> Get-WmiObject Win32_LocalTime -computername $j -ErrorVariable
> myerror -credential $credential | Foreach {$_.Server}
> if ($myerror.count -gt 0)
> {
> if ($myerror[0].Exception -is
> [System.Runtime.InteropServices.COMException])
> {$j+' Wrong Admin Password' >> $file;}
> elseif ($myerror[0].Exception -is
> [System.UnauthorizedAccessException])
> {$j+' Windows Firewall Blocking' >> $file;}
> }
>
> }
>
> Tao Ma
> Best wishes.
That worked great. I never thought of using -errorvariable. I ended up
using a Trap [system.exception] {$z=$null;continue} as well just to
get rid of the bright red error messages. Thanks for the help.

My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception stack is corrupt when catching and storing the exception Morten Herman Langkjaer .NET General 1 03-05-2008 02:11 AM
Vista Install Error: Exception Unknown Software Exception burntham77 Vista installation & setup 2 01-07-2008 01:39 PM
Help w/ error trapping Blip PowerShell 3 02-12-2007 07:34 PM
Confusing exception trapping behaviour david.jade PowerShell 2 10-25-2006 05:21 PM
Trapping exceptions =?Utf-8?B?UmF5IEhheWVz?= PowerShell 3 09-11-2006 12:31 PM


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008