When writing a script to perform a task on a remote computer, it is beneficial to know whether a network connection to this computer can be established.
When writing a script to perform a task on a remote computer, it is beneficial to know whether a network connection to this computer can be established. Marcin Policht’s latest article explains how to use the properties of Win32_PingStatus class to test network connectivity. The article also features a sample script that retrieves various connection-related statistics.
In such cases, the most commonly performed test is running the PING program, which generates one or more ICMP packets. The packets’ return to originating computer enables the user to determine not only existence of the network path to the remote computer, but also a number of its characteristics, such as speed and reliability. This determination comes in very handy if, for example, a specific task must be performed repetitively on a large number of computers.
Before the task is executed (which typically happens as the result of a communication session on a higher level in terms of the OSI model), you can test the basic (network layer) connectivity. If this test fails, there is no reason to continue the connection attempt. You can simply log the attempt as unsuccessful and repeat the same process with the next computer. This type of approach saves time and computing power.
Unfortunately, until recently, testing results of the PING command executed as part of a script required fairly cumbersome workarounds. Typically, this was done by parsing the outcome of the command for lines indicating a failure (e.g., “Request timed out”) or success (e.g., “Reply from”). This wasn’t very efficient since it required redirecting the results into a temporary text file and, subsequently, reading it line by line.
With the release of Windows XP, Microsoft introduced a new Windows Management Instrumentation (WMI) provider (i.e., a software component that provides a specific type of functionality). The WMI provider gives access to status information generated by the PING command. Called (not surprisingly) Ping provider, it supports single class (Win32_PingStatus) and two methods (ExecQueryAsync and GetObjectAsync). The focus of this article will be using the properties of Win32_PingStatus class to test network connectivity.
Note that the provider is available only on the Windows XP platform (as well as on the upcoming Windows .NET). This does not mean, however, that the script must be run from a computer running Windows XP. As long as a Windows XP computer is somewhere on a network and you have administrative rights to it, you can initiate PING remotely and retrieve its results.
This opens a range of other possibilities. For example, you can sit at your desk and test characteristics of IP connectivity (e.g., reliability and speed) between two remote locations — as long as one of them has at least one computer running Windows XP.
The following script illustrates how to use the StatusCode property of the Win32_PingStatus class to determine whether a remote computer is responding to the PING command.
A value of the StatusCode equal to 0 indicates the PING command was successful; a non-zero value indicates a failure.
The reason for the failure can be determined by analyzing returned value, which can be one of the following:
Besides testing the status, it is possible to retrieve other connection statistics, such as packet size, response time, or remaining time to live (all returned by default in the Windows implementation of the PING). These statistics are included in the sample script below.
The sTarget variable contains the name or IP address of the target computer. If you use the name, be sure that it can be properly resolved via DNS, WINS, or local hosts (or lmhosts) file. If the name is used, in addition to returning status and parameters of the connection, the script returns the corresponding IP address.
Dim sHost 'name of Windows XP computer from which the PING command will be initiated
Dim sTarget 'name or IP address of remote computer to which connectivity will be tested
Dim cPingResults 'collection of instances of Win32_PingStatus class
Dim oPingResult 'single instance of Win32_PingStatus class
sHost = "SWYNKPC-XP001"
sTarget = "192.168.12.14"
Set cPingResults = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
sHost & "/root/cimv2"). ExecQuery("SELECT * FROM Win32_PingStatus " & _
"WHERE Address = '" + sTarget + "'")
For Each oPingResult In cPingResults
If oPingResult.StatusCode = 0 Then
If LCase(sTarget) = oPingResult.ProtocolAddress Then
WScript.Echo sTarget & " is responding"
Else
WScript.Echo sTarget & "(" & oPingResult.ProtocolAddress & ") is responding"
End If
Wscript.Echo "Bytes = " & vbTab & oPingResult.BufferSize
Wscript.Echo "Time (ms) = " & vbTab & oPingResult.ResponseTime
Wscript.Echo "TTL (s) = " & vbTab & oPingResult.ResponseTimeToLive
Else
WScript.Echo sTarget & " is not responding"
WScript.Echo "Status code is " & oPingResult.StatusCode
End If
Next
|
Marcin Policht obtained his Master of Computer Science degree about 20 years ago and has been since then working in the Information Technology field, handling variety of responsibilities, but focusing primarily on the areas of identity and access management, virtualization, system management, and, more recently private, hybrid, and public cloud services. He has authored the first book dedicated to Windows Management Instrumentation and co-written several others dealing with subjects ranging from core operating system features to high-availability solutions. His articles have been published on such Web sites as ServerWatch.com and DatabaseJournal.com. For his contributions to the Microsoft technical community, he has been awarded the title of Microsoft MVP over the last ten years.
Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved
Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.