![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
| | #6 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||