Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Capturing exceptions

Reply
 
Old 10-29-2008   #1 (permalink)
IT Staff


 
 

Capturing exceptions

I wish to capture the message below "No such host is known"

Exception calling "GetHostAddresses" with "1" argument(s): "No such host is
known"
At D:\dnscheck.ps1:3 char:41


If computer name is xxxx, or not found, it will generate the error above.
How can i capture only the *meaningful* message ?
I don't want to use $erroractionpreference = "SilentlyContinue"

===================================
my sample script

$comp = "xxxx"

$ip = [system.net.dns]::gethostaddresses($comp) | foreach
{$_.ipaddresstostring}
$resolved = [system.net.dns]::gethostentry($ip) | foreach {$_.hostname}

write-host "Original name in AD" $comp
Write-host "Ping hostname" $ip
Write-host "Ping -a $ip, resolved hostname is" $resolved
===================================



My System SpecsSystem Spec
Old 10-29-2008   #2 (permalink)
Tao Ma


 
 

Re: Capturing exceptions

Hi,
"trap" statement can catch the exception:

trap [System.Net.Sockets.SocketException] { continue; }

It says: capture [System.Net.Sockets.SocketException] only and continue
silently.

You can put this statement in your script.

Best wishes,
Tao ma

"IT Staff" <jkklim@xxxxxx> дÈëÏûÏ¢ÐÂÎÅ:eepyaZYOJHA.588@xxxxxx
Quote:

>I wish to capture the message below "No such host is known"
>
> Exception calling "GetHostAddresses" with "1" argument(s): "No such host
> is known"
> At D:\dnscheck.ps1:3 char:41
>
>
> If computer name is xxxx, or not found, it will generate the error above.
> How can i capture only the *meaningful* message ?
> I don't want to use $erroractionpreference = "SilentlyContinue"
>
> ===================================
> my sample script
>
> $comp = "xxxx"
>
> $ip = [system.net.dns]::gethostaddresses($comp) | foreach
> {$_.ipaddresstostring}
> $resolved = [system.net.dns]::gethostentry($ip) | foreach {$_.hostname}
>
> write-host "Original name in AD" $comp
> Write-host "Ping hostname" $ip
> Write-host "Ping -a $ip, resolved hostname is" $resolved
> ===================================
>

My System SpecsSystem Spec
Old 10-29-2008   #3 (permalink)
IT Staff


 
 

Re: Capturing exceptions

How do i capture the exception message ?


"Tao Ma" <feng_eden@xxxxxx> wrote in message
news:OkML3gYOJHA.3876@xxxxxx
Quote:

> Hi,
> "trap" statement can catch the exception:
>
> trap [System.Net.Sockets.SocketException] { continue; }
>
> It says: capture [System.Net.Sockets.SocketException] only and continue
> silently.
>
> You can put this statement in your script.
>
> Best wishes,
> Tao ma
>
> "IT Staff" <jkklim@xxxxxx>
> дÈëÏûÏ¢ÐÂÎÅ:eepyaZYOJHA.588@xxxxxx
Quote:

>>I wish to capture the message below "No such host is known"
>>
>> Exception calling "GetHostAddresses" with "1" argument(s): "No such host
>> is known"
>> At D:\dnscheck.ps1:3 char:41
>>
>>
>> If computer name is xxxx, or not found, it will generate the error above.
>> How can i capture only the *meaningful* message ?
>> I don't want to use $erroractionpreference = "SilentlyContinue"
>>
>> ===================================
>> my sample script
>>
>> $comp = "xxxx"
>>
>> $ip = [system.net.dns]::gethostaddresses($comp) | foreach
>> {$_.ipaddresstostring}
>> $resolved = [system.net.dns]::gethostentry($ip) | foreach {$_.hostname}
>>
>> write-host "Original name in AD" $comp
>> Write-host "Ping hostname" $ip
>> Write-host "Ping -a $ip, resolved hostname is" $resolved
>> ===================================
>>
>
>

My System SpecsSystem Spec
Old 10-29-2008   #4 (permalink)
Tao Ma


 
 

Re: Capturing exceptions

You can access $error virable or record it by yourself:

PS C:\> $err_arr = New-Object System.Collections.ArrayList
PS C:\> &{trap [System.Net.Sockets.SocketException] { [void]
$err_arr.Add($_); continue; }; [system.net.dns]::gethostaddresses('f')}
PS C:\> $err_arr[0]
No such host is known.
At line:1 char:112
+ &{trap [System.Net.Sockets.SocketException] { $err_arr.Add($_);
continue; }; [system.net.dns]::gethostaddresses <<<<
('f')}

Best wishes,
Tao Ma


"IT Staff" <jkklim@xxxxxx> дÈëÏûÏ¢ÐÂÎÅ:%23%23IzEBaOJHA.4504@xxxxxx
Quote:

> How do i capture the exception message ?
>
>
> "Tao Ma" <feng_eden@xxxxxx> wrote in message
> news:OkML3gYOJHA.3876@xxxxxx
Quote:

>> Hi,
>> "trap" statement can catch the exception:
>>
>> trap [System.Net.Sockets.SocketException] { continue; }
>>
>> It says: capture [System.Net.Sockets.SocketException] only and continue
>> silently.
>>
>> You can put this statement in your script.
>>
>> Best wishes,
>> Tao ma
>>
>> "IT Staff" <jkklim@xxxxxx> дÈëÏûÏ¢ÐÂÎÅ:eepyaZYOJHA.588@xxxxxx
Quote:

>>>I wish to capture the message below "No such host is known"
>>>
>>> Exception calling "GetHostAddresses" with "1" argument(s): "No such host
>>> is known"
>>> At D:\dnscheck.ps1:3 char:41
>>>
>>>
>>> If computer name is xxxx, or not found, it will generate the error
>>> above. How can i capture only the *meaningful* message ?
>>> I don't want to use $erroractionpreference = "SilentlyContinue"
>>>
>>> ===================================
>>> my sample script
>>>
>>> $comp = "xxxx"
>>>
>>> $ip = [system.net.dns]::gethostaddresses($comp) | foreach
>>> {$_.ipaddresstostring}
>>> $resolved = [system.net.dns]::gethostentry($ip) | foreach {$_.hostname}
>>>
>>> write-host "Original name in AD" $comp
>>> Write-host "Ping hostname" $ip
>>> Write-host "Ping -a $ip, resolved hostname is" $resolved
>>> ===================================
>>>
>>
>>
>
>

My System SpecsSystem Spec
Old 10-29-2008   #5 (permalink)
Shay Levy [MVP]


 
 

Re: Capturing exceptions

Hello IT,

TRy:

trap {write-warning $_.Exception.Message ;continue}
[system.net.dns]::gethostaddresses('foo')




---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar


IS> I wish to capture the message below "No such host is known"
IS>
IS> Exception calling "GetHostAddresses" with "1" argument(s): "No such
IS> host is
IS> known"
IS> At D:\dnscheck.ps1:3 char:41
IS> If computer name is xxxx, or not found, it will generate the error
IS> above.
IS> How can i capture only the *meaningful* message ?
IS> I don't want to use $erroractionpreference = "SilentlyContinue"
IS> ===================================
IS> my sample script
IS> $comp = "xxxx"
IS>
IS> $ip = [system.net.dns]::gethostaddresses($comp) | foreach
IS> {$_.ipaddresstostring}
IS> $resolved = [system.net.dns]::gethostentry($ip) | foreach
IS> {$_.hostname}
IS> write-host "Original name in AD" $comp
IS> Write-host "Ping hostname" $ip
IS> Write-host "Ping -a $ip, resolved hostname is" $resolved
IS> ===================================


My System SpecsSystem Spec
Old 10-29-2008   #6 (permalink)
IT Staff


 
 

Re: Capturing exceptions

After going thru some trial and errors, i prefer to use this:

================
$error.clear()
$comp = "xxxx"
$erroractionpreference = "SilentlyContinue"


$ip = [system.net.dns]::gethostaddresses($comp) | foreach
{$_.ipaddresstostring}
if($error[0].Exception.Message -ne $null)
{$resolved=$error[0].Exception.Message;} else {$resolved =
[system.net.dns]::gethostentry($ip) | foreach {$_.hostname}}

write-host "Original name in AD" $comp
write-host "ip:" $ip
Write-host "DNS hostname :" $resolved
==================================

If there are better ways, please let me know.





"IT Staff" <jkklim@xxxxxx> wrote in message
news:eepyaZYOJHA.588@xxxxxx
Quote:

>I wish to capture the message below "No such host is known"
>
> Exception calling "GetHostAddresses" with "1" argument(s): "No such host
> is known"
> At D:\dnscheck.ps1:3 char:41
>
>
> If computer name is xxxx, or not found, it will generate the error above.
> How can i capture only the *meaningful* message ?
> I don't want to use $erroractionpreference = "SilentlyContinue"
>
> ===================================
> my sample script
>
> $comp = "xxxx"
>
> $ip = [system.net.dns]::gethostaddresses($comp) | foreach
> {$_.ipaddresstostring}
> $resolved = [system.net.dns]::gethostentry($ip) | foreach {$_.hostname}
>
> write-host "Original name in AD" $comp
> Write-host "Ping hostname" $ip
> Write-host "Ping -a $ip, resolved hostname is" $resolved
> ===================================
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
memory exceptions PowerShell
Re: Unhandled exceptions in ASP.NET .NET General
SMO : Catching Exceptions PowerShell
!!VISTA RC2!! Audio capturing while Video capturing Vista music pictures video
Trapping exceptions PowerShell


Vista Forums 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 Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46