PowerShell for Windows Admins

May 16 2011   2:56PM GMT

Scripting Games Commentary: XIII ping



Posted by: Richard Siddaway
Tags:
PowerShell v2

How many times have you done this when testing network connectivity?

ping 127.0.0.1

 

If all we want is a test of connectivity ping works fine. We can ping using WMI or .NET classes from within PowerShell. This has the advantage of returning an object rather than text which means we can work with it much easier.

Using .NET

$ping = New-Object -TypeName System.Net.NetworkInformation.Ping
$ping.Send("127.0.0.1")

 

This could be condensed to

$ping = (New-Object -TypeName System.Net.NetworkInformation.Ping).Send("127.0.10.1")

 

and we get the results in the ping variable.

 

The WMI solution is like this

Get-WmiObject -Class Win32_PingStatus -Filter "Address=’127.0.0.1′"

 

We can make this even easier because PowerShell 2 wraps this WMI class in a cmdlet

Test-Connection -ComputerName 127.0.0.1

 

if you don’t want to type all that then create an alias

Set-Alias -Name p -Value Test-Connection
p 127.0.0.1

 

just don’t use the alias in scripts

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: