PowerShell Functions: Test-Ping

This is my first PowerShell function, Test-Ping. This simply returns $true if a given host is pingable, which is pretty useful. This function also makes use of PowerShell’s built in Comment Based Help.  Here is the code:

 ##  FUNCTION.......:  Test-Ping
 ##  PURPOSE........:  Returns "True" of the host is pingable, or "False" if
 ##                    not.
 ##                    This uses the .NET NetworkInformation.Ping method.
 ##  ARGUMENTS......:  A computer name or IP address (just one at a time).
 ##  EXAMPLE........:  Test-Ping
 ##                    Test-Ping google.com
 ##  REQUIREMENTS...:  PowerShell 2.0
 ##  NOTES..........: 
 function Test-Ping
 Determines if a given device is pingable from the computer where the
 function is used. This can only be used with one device at a time. For
 examples type:
 Get-Help Test-Ping -examples
 This function uses the .NET NetworkInformation.Ping method to determine
 if a device is reachable using an ICMP echo message. The Function
 returns $true if the device is pingable, and $false if it is not (or if
 ICMP echo message replies are not returned).
 Test-Ping google.com
 Will return "True" if DNS is working in the environment that the script
 is run in, and the site is pingable.
 Will return "True" if the IPAddress is pingable.
 ## Contents of exampleScript.ps1
 If (!(Test-Ping deadHost)) {
 Write-Host "Host 'deadHost' not Live!" -Fo Red
 Else {
 Write-Host "Host 'deadHost' is Live!" -Fo Cyan

 Assuming that this script also contains the Test-Ping function, this
 script will return the string "Host 'deadHost' not Live!" (unless you
 have a device named "deadHost" in your environment for some reason).
 NAME......:  Test-Ping
 AUTHOR....:  Joe Glessner
 CREATED...:  17AUG10
 trap {$false; continue}
 $timeout = 1000
 $object = New-Object system.Net.NetworkInformation.Ping
 (($object.Send($ip, $timeout)).Status -eq 'Success')

Save the code into a file called C:\test-ping.ps1

Open up powershell, and run this command:

. c:\test-ping.ps1

And hit ENTER (this is called “dot sourcing” by the way). Now run this command:

get-help Test-Ping

And hit ENTER again. Pretty cool eh? It also makes it much easier to remember how your functions are used from the command line, if you keep them all in one file, and then dot source that file.


New category: PowerShell Functions

Like many of my fellow IT Professionals out in the world, I’m digging me some PowerShell. I find it really funny that For all these years the *NIX admins have been laughing their asses off because the Windows admins have not really had a decent shell scripting language. And then came PowerShell. PowerShell is like the command shell and a programming language had a baby. A baby that has superpowers. This is exactly what Windows admins the world over have wanted all this time.

And it’s so easy to use. In fact, even if you’ve never opened a PowerShell prompt before, you already know some stuff that you can use in it (assuming that you’ve opened a command prompt in the last twenty years). Go ahead and open up a PowerShell prompt (Win+R, type: powershell, and then hit ENTER, or just type powershell into the search box on the start menu).

Type: dir, and then hit ENTER.

Looks familiar right?

But try this, type:

get-alias dir, and hit ENTER.

You just ran a PowerShell cmdlet (pronounced “command let”).

And there are a ton more. And unlike some people I could name, Microsoft didn’t forget the other guys when they built PowerShell, try typing the following in the PowerShell Window: ls, and then hit ENTER. Yep they make it pretty easy to harness what you know, whether that be *NIX, or Win32.

One of the most awesome things about PowerShell is that you can extend it. Easily. And make the extension seamless. And Functions are pretty awesome. So expect to see more on this here.

%d bloggers like this: