>Simple PING test: VBScript vs PowerShell

>As a preparation for my TechEd Asia 2008 session on Windows PowerShell, I’ve been trying to convert a few of my administrative scripts to demonstrate how easy tasks can be done using PowerShell. Here’s one I just did: simple PING test. Here’s a sample VBScript code that checks whether a PING test is successful or not. I call the VBScript and pass it a parameter which is the hostname or IP address of the computer I want to ping

VBScript:
strComputer=Wscript.Arguments.Item(0) 'parameter passed = hostname

wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strComputer & "'"

Set objWMIService = GetObject("winmgmts:\.rootcimv2")
Set objPing = objWMIService.ExecQuery(wmiQuery
)
For Each objStatus In
objPing
If IsNull(objStatus.StatusCode) Or objStatus.Statuscode0
Then
Reachable = False
'if computer is unreacable, return false
Else
Reachable = True
'if computer is reachable, return true
End If
Next


Wscript.Echo Reachable

Here’s the equivalent script using PowerShell

Param($hostname)
$ping = new-object System.Net.NetworkInformation.Ping
$Reply = $ping.Send($hostname)

$Reply.status

You can see how much easier it is to accomplish a simple task in PowerShell. It makes the life of an administrator much better, especially if you have to write scripts that automate repetitive tasks. I’ll post more samples of the VBScript codes you’ve probably seen in this blog converted to PowerShell

Please note: I reserve the right to delete comments that are offensive or off-topic.

Leave a Reply

Your email address will not be published. Required fields are marked *