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 - What's in a Ping? or a Ping by another method.

Reply
 
Old 02-18-2009   #1 (permalink)
OldDog


 
 

What's in a Ping? or a Ping by another method.

Hi,

I have been wondering about this for a while.

Many of the scripts here use this method to Ping a server:

$ping = new-object System.Net.NetworkInformation.Ping
$reply = $ping.Send($srv)
if ($reply.status -eq "Success")
{ do something } Else { Write-Host $srv " Does Not Ping" }


However, I have been using this method with good results:

$response = Get-WmiObject -query "Select * From Win32_PingStatus Where
Address = '$srv'"

if( ($response -eq $null) -or ($response.StatusCode -ne 0))
{ Write-Host $srv " Does Not Ping" } Else { do something }


So, What's the difference? or what is the advantage of one over the
other?

My System SpecsSystem Spec
Old 02-18-2009   #2 (permalink)
Brandon Shell [MVP]


 
 

Re: What's in a Ping? or a Ping by another method.

no major different... one is .NET the other is WMI.

One thing the WMI can offer is a remote ping.

Brandon Shell [MVP]
------------------------
Blog: http://www.bsonposh.com/
Author: http://www.TurboChargeAD.org
Profile: https://mvp.support.microsoft.com/profile/Brandon

O> Hi,
O>
O> I have been wondering about this for a while.
O>
O> Many of the scripts here use this method to Ping a server:
O>
O> $ping = new-object System.Net.NetworkInformation.Ping
O> $reply = $ping.Send($srv)
O> if ($reply.status -eq "Success")
O> { do something } Else { Write-Host $srv " Does Not Ping" }
O> However, I have been using this method with good results:
O>
O> $response = Get-WmiObject -query "Select * From Win32_PingStatus
O> Where Address = '$srv'"
O>
O> if( ($response -eq $null) -or ($response.StatusCode -ne 0)) {
O> Write-Host $srv " Does Not Ping" } Else { do something }
O>
O> So, What's the difference? or what is the advantage of one over the
O> other?
O>


My System SpecsSystem Spec
Old 02-18-2009   #3 (permalink)
PaulChavez


 
 

RE: What's in a Ping? or a Ping by another method.

A ping is an ICMP echo request.

Both of these methods send one, so there is very little difference from a
networking perspective.



"OldDog" wrote:
Quote:

> Hi,
>
> I have been wondering about this for a while.
>
> Many of the scripts here use this method to Ping a server:
>
> $ping = new-object System.Net.NetworkInformation.Ping
> $reply = $ping.Send($srv)
> if ($reply.status -eq "Success")
> { do something } Else { Write-Host $srv " Does Not Ping" }
>
>
> However, I have been using this method with good results:
>
> $response = Get-WmiObject -query "Select * From Win32_PingStatus Where
> Address = '$srv'"
>
> if( ($response -eq $null) -or ($response.StatusCode -ne 0))
> { Write-Host $srv " Does Not Ping" } Else { do something }
>
>
> So, What's the difference? or what is the advantage of one over the
> other?
>
My System SpecsSystem Spec
Old 02-18-2009   #4 (permalink)
OldDog


 
 

Re: What's in a Ping? or a Ping by another method.

On Feb 18, 3:10*pm, Brandon Shell [MVP] <a_bshell.m...@xxxxxx>
wrote:
Quote:

> no major different... one is .NET the other is WMI.
>
> One thing the WMI can offer is a remote ping.
>
> Brandon Shell [MVP]
> ------------------------
> Blog:http://www.bsonposh.com/
> Author:http://www.TurboChargeAD.org
> Profile:https://mvp.support.microsoft.com/profile/Brandon
>
> O> Hi,
> O>
> O> I have been wondering about this for a while.
> O>
> O> Many of the scripts here use this method to Ping a server:
> O>
> O> $ping = new-object System.Net.NetworkInformation.Ping
> O> $reply = $ping.Send($srv)
> O> if ($reply.status -eq "Success")
> O> { do something } Else { Write-Host $srv " Does Not Ping" }
> O> However, I have been using this method with good results:
> O>
> O> $response = Get-WmiObject -query "Select * From Win32_PingStatus
> O> Where Address = '$srv'"
> O>
> O> if( ($response -eq $null) -or ($response.StatusCode -ne 0)) {
> O> Write-Host $srv " Does Not Ping" } Else { do something }
> O>
> O> So, What's the difference? or what is the advantage of one over the
> O> other?
> O>
OK, that's pretty basic. Why does one method, .NET, seemed to be
prefered over the WMI on this forum?

BTW I get odd errors when I use the .NET method if the machine I am
trying to ping does not exist.
My System SpecsSystem Spec
Old 02-18-2009   #5 (permalink)
Brandon Shell [MVP]


 
 

Re: What's in a Ping? or a Ping by another method.

shrug... I prefer the WMI one

Brandon Shell [MVP]
------------------------
Blog: http://www.bsonposh.com/
Author: http://www.TurboChargeAD.org
Profile: https://mvp.support.microsoft.com/profile/Brandon

O> On Feb 18, 3:10 pm, Brandon Shell [MVP] <a_bshell.m...@xxxxxx>
O> wrote:
O>
Quote:
Quote:

>> no major different... one is .NET the other is WMI.
>>
>> One thing the WMI can offer is a remote ping.
>>
>> Brandon Shell [MVP]
>> ------------------------
>> Blog:http://www.bsonposh.com/
>> Author:http://www.TurboChargeAD.org
>> Profile:https://mvp.support.microsoft.com/profile/Brandon
>> O> Hi,
>> O>
>> O> I have been wondering about this for a while.
>> O>
>> O> Many of the scripts here use this method to Ping a server:
>> O>
>> O> $ping = new-object System.Net.NetworkInformation.Ping
>> O> $reply = $ping.Send($srv)
>> O> if ($reply.status -eq "Success")
>> O> { do something } Else { Write-Host $srv " Does Not Ping" }
>> O> However, I have been using this method with good results:
>> O>
>> O> $response = Get-WmiObject -query "Select * From Win32_PingStatus
>> O> Where Address = '$srv'"
>> O>
>> O> if( ($response -eq $null) -or ($response.StatusCode -ne 0)) {
>> O> Write-Host $srv " Does Not Ping" } Else { do something }
>> O>
>> O> So, What's the difference? or what is the advantage of one over
>> the
>> O> other?
>> O>
O> OK, that's pretty basic. Why does one method, .NET, seemed to be
O> prefered over the WMI on this forum?
O>
O> BTW I get odd errors when I use the .NET method if the machine I am
O> trying to ping does not exist.
O>


My System SpecsSystem Spec
Old 02-18-2009   #6 (permalink)
Rob Campbell


 
 

RE: What's in a Ping? or a Ping by another method.

One big difference is execution time. Using the .net method I can ping-sweep
an entire class c network on our internal network and do a dns resolution on
anything that answers in about 2 minutes.

I started off trying this with WMI, and got tired of waiting for it to finish.

"OldDog" wrote:
Quote:

> Hi,
>
> I have been wondering about this for a while.
>
> Many of the scripts here use this method to Ping a server:
>
> $ping = new-object System.Net.NetworkInformation.Ping
> $reply = $ping.Send($srv)
> if ($reply.status -eq "Success")
> { do something } Else { Write-Host $srv " Does Not Ping" }
>
>
> However, I have been using this method with good results:
>
> $response = Get-WmiObject -query "Select * From Win32_PingStatus Where
> Address = '$srv'"
>
> if( ($response -eq $null) -or ($response.StatusCode -ne 0))
> { Write-Host $srv " Does Not Ping" } Else { do something }
>
>
> So, What's the difference? or what is the advantage of one over the
> other?
>
My System SpecsSystem Spec
Old 02-18-2009   #7 (permalink)
Brandon Shell [MVP]


 
 

RE: What's in a Ping? or a Ping by another method.

My experience has been quite different and actually along those lines you
can specify the time out with WMI. I do not believe you can do that with
the .NET provider.


Brandon Shell [MVP]
------------------------
Blog: http://www.bsonposh.com/
Author: http://www.TurboChargeAD.org
Profile: https://mvp.support.microsoft.com/profile/Brandon

RC> One big difference is execution time. Using the .net method I can
RC> ping-sweep an entire class c network on our internal network and do
RC> a dns resolution on anything that answers in about 2 minutes.
RC>
RC> I started off trying this with WMI, and got tired of waiting for it
RC> to finish.
RC>
RC> "OldDog" wrote:
RC>
Quote:
Quote:

>> Hi,
>>
>> I have been wondering about this for a while.
>>
>> Many of the scripts here use this method to Ping a server:
>>
>> $ping = new-object System.Net.NetworkInformation.Ping
>> $reply = $ping.Send($srv)
>> if ($reply.status -eq "Success")
>> { do something } Else { Write-Host $srv " Does Not Ping" }
>> However, I have been using this method with good results:
>>
>> $response = Get-WmiObject -query "Select * From Win32_PingStatus
>> Where Address = '$srv'"
>>
>> if( ($response -eq $null) -or ($response.StatusCode -ne 0)) {
>> Write-Host $srv " Does Not Ping" } Else { do something }
>>
>> So, What's the difference? or what is the advantage of one over the
>> other?
>>

My System SpecsSystem Spec
Old 02-18-2009   #8 (permalink)
Rob Campbell


 
 

RE: What's in a Ping? or a Ping by another method.

Try this.


get-date

$network = "10.1.3.0"


[int]$timeout = 20
[switch]$resolve = $true
[int]$TTL = 128
[switch]$DontFragment = $false
[int]$buffersize = 32

$options = new-object system.net.networkinformation.pingoptions
$options.TTL = $TTL
$options.DontFragment = $DontFragment
$buffer=([system.text.encoding]::ASCII).getbytes("a"*$buffersize)

foreach ($a in 1..254){

$remotehost = $($network.trim("0")) + $a
$ping = new-object system.net.networkinformation.ping
$reply = $ping.Send($remotehost,$timeout,$buffer,$options)
if ($resolve) {
$hostname = ([System.Net.Dns]::GetHostEntry($remotehost)).hostname
if ($hostname -eq $remotehost){$hostname = "No RDNS"}
}

if ($reply.status -eq "Success"){$remotehost + "`t" + $reply.status + "`t`t"
+ $hostname}
else {$remotehost + "`t" + $reply.status + "`t" + $hostname}

}

get-date



"Brandon Shell [MVP]" wrote:
Quote:

> My experience has been quite different and actually along those lines you
> can specify the time out with WMI. I do not believe you can do that with
> the .NET provider.
>
>
> Brandon Shell [MVP]
> ------------------------
> Blog: http://www.bsonposh.com/
> Author: http://www.TurboChargeAD.org
> Profile: https://mvp.support.microsoft.com/profile/Brandon
>
> RC> One big difference is execution time. Using the .net method I can
> RC> ping-sweep an entire class c network on our internal network and do
> RC> a dns resolution on anything that answers in about 2 minutes.
> RC>
> RC> I started off trying this with WMI, and got tired of waiting for it
> RC> to finish.
> RC>
> RC> "OldDog" wrote:
> RC>
Quote:
Quote:

> >> Hi,
> >>
> >> I have been wondering about this for a while.
> >>
> >> Many of the scripts here use this method to Ping a server:
> >>
> >> $ping = new-object System.Net.NetworkInformation.Ping
> >> $reply = $ping.Send($srv)
> >> if ($reply.status -eq "Success")
> >> { do something } Else { Write-Host $srv " Does Not Ping" }
> >> However, I have been using this method with good results:
> >>
> >> $response = Get-WmiObject -query "Select * From Win32_PingStatus
> >> Where Address = '$srv'"
> >>
> >> if( ($response -eq $null) -or ($response.StatusCode -ne 0)) {
> >> Write-Host $srv " Does Not Ping" } Else { do something }
> >>
> >> So, What's the difference? or what is the advantage of one over the
> >> other?
> >>
>
>
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Can't ping XP (even IP) but XP can ping Vista x64 Vista networking & sharing
I can ping with IPv6. I can not ping with IPv4. Vista General
cannot ping through vpn Vista networking & sharing
cant ping my self Vista General
Can Only Ping One way Vista networking & sharing


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