Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reduce the timeout of WMI query?

1,534 views
Skip to first unread message

msnews.microsoft.com

unread,
May 15, 2002, 7:42:54 PM5/15/02
to
How can I reduce the timeout of this query?

---snip
set objInstance = objService.ExecQuery ("select * from Win32_OperatingSystem
where Primary=true")
for each OS in objinstance
response.write os.name
Next
---snip

If the remote server I'm running this against is down, it takes forever to
reply back an error. I wan the error immediate if the reponse is not given
from the server.

I'm using Win2K, vbscript, and asp

Please help!
Thanks
Brian


Alex K. Angelopoulos (MVP)

unread,
May 16, 2002, 3:53:52 AM5/16/02
to
You can't.

Here's a much easier trick; you can ping the remote system using WMI to see if
it will respond before you even try a query; this should speed you up
dramatically. Here's a routine to use WMI's built-in ping provider. You can
use either an IP or a DNS name.


sTarget = "192.168.1.11"
set oPing = _
GetObject("winmgmts:{impersonationLevel=impersonate}"). _
ExecQuery ("select * from Win32_PingStatus where address = '" _
& sTarget & "'")

For Each o In oPing
sResp = o.StatusCode : exit for
Next
wscript.echo sResp

if sResp = 0 Then
' do stuff here since it is up
End If


"msnews.microsoft.com" <btu...@hotmail.com> wrote in message
news:epX66oG$BHA.2524@tkmsftngp05...

Torgeir Bakken

unread,
May 16, 2002, 4:07:33 AM5/16/02
to
"Alex K. Angelopoulos (MVP)" wrote:

> Here's a much easier trick; you can ping the remote system using WMI to see if
> it will respond before you even try a query; this should speed you up
> dramatically. Here's a routine to use WMI's built-in ping provider. You can
> use either an IP or a DNS name.
>
> sTarget = "192.168.1.11"
> set oPing = _
> GetObject("winmgmts:{impersonationLevel=impersonate}"). _
> ExecQuery ("select * from Win32_PingStatus where address = '" _
> & sTarget & "'")

Hi

Note that Win32_PingStatus is only available for Windows XP.

--
torgeir


Alex K. Angelopoulos (MVP)

unread,
May 16, 2002, 4:24:43 AM5/16/02
to
Really!
I saw when I re-checked the SDK that it is listed in the "New WMI Classes"
category, which is supposedly only implemented on XP...

However on the Win32_PingStatus page they had this under Requirements:
Windows NT/2000/XP: Requires Windows NT 4.0 SP4 or later
Namespace: Included in \root\cimv2
MOF: Declared in Ping_desc.mof

Looks like another documentation "bug"...?

"Torgeir Bakken" <Torgeir.B...@hydro.com> wrote in message
news:3CE368C5...@hydro.com...

Torgeir Bakken

unread,
May 16, 2002, 4:39:16 AM5/16/02
to
"Alex K. Angelopoulos (MVP)" wrote:

> I saw when I re-checked the SDK that it is listed in the "New WMI Classes"
> category, which is supposedly only implemented on XP...
>
> However on the Win32_PingStatus page they had this under Requirements:
> Windows NT/2000/XP: Requires Windows NT 4.0 SP4 or later
> Namespace: Included in \root\cimv2
> MOF: Declared in Ping_desc.mof
>
> Looks like another documentation "bug"...?

Hi

The Requirements part of the WMI documentation in MSDN can't be trusted at all.

At the top of the MSDN documentation of Win32_PingStatus
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/r_32os4_1xdf.asp

<qoute>
The Win32_PingStatus WMI class represents the values returned by the standard ping
command. More information on ping can be found in RFC 791. This class was added for
Windows XP.
</qoute>

--
torgeir


Gregory Phillips

unread,
May 16, 2002, 4:51:00 AM5/16/02
to
Ping it first to see if it will respond

Sample code in wsf (both vbscript and JScript)

<job>
<script language="vbscript">
Option Explicit
On Error Resume Next
' All Visual Basic names are not case-sensitive (yuk)
'-------------------------
Function JsInputBox(sPrompt,sTitle,sDefault)
JSinputbox = InputBox(sPrompt,sTitle,sDefault)
End Function
'-------------------------
</script>

<script language="jscript">
//---------------------- JScript --- Main Routine -----------------------------
var sMachine;

sMachine = "192.168.200.005" // Default value for dialog box

sMachine = JsInputBox("Enter machine to ping", "Input Computer Address", sMachine);

if( ping(sMachine) ) {
WScript.echo("Machine " + sMachine + " responded to pinging.");
}
else {
WScript.echo("Machine " + sMachine + " did not respond to pinging.");
}
//-----------------------------------------------------------------------------
function ping(sMachine) {
var oPing
var sResp;
var oP;
var e;
var bResult;

try {
oPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery(
"select * from Win32_PingStatus where address = '" + sMachine + "'");

for (e = new Enumerator(oPing); !e.atEnd(); e.moveNext()) {
oP = e.item();
sResp = oP.StatusCode;
break;
}
if (sResp == 0) {
//WScript.echo("Ping Status = 0, machine " + sMachine + " is responding.");
bResult = true;
}
else {
//WScript.echo("Ping Status not 0, machine " + sMachine + " is NOT responding.");
bResult = false;
}
}
catch(err) { ShowError(err); }
return(bResult);
}
//-----------------------------------------------------------------------------
function ShowError(error) {
var sMessage
var sFacilityCode;
var sErrorCode;

sFacilityCode = error.number >> 16 & 0x1FFF;
sErrorCode = error.number & 0xFFFF;
sMessage = "ERROR: 0x"+hex(error.number) + " " + error + "\n\n"+
"Name: " + error.name + "\n" +
"Description " + error.description + "\n" +
"Facility Code: " + sFacilityCode + "\n" +
"Error Code: " + sErrorCode + "\n" ;
WScript.echo(sMessage);
return(error);
}
//-----------------------------------------------------------------------------
function hex(nmb)
{
if (nmb > 0)
return nmb.toString(16);
else
return (nmb + 0x100000000).toString(16);
}
//-----------------------------------------------------------------------------
</script>
</job>

---
Gregory Phillips greg...@home.com Seattle, Washington, USA

mhicks

unread,
May 16, 2002, 8:06:25 AM5/16/02
to
"Alex K. Angelopoulos \(MVP\)" <alexangelop...@hotmail.com>
wrote:

> However on the Win32_PingStatus page they had this under Requirements:
> Windows NT/2000/XP: Requires Windows NT 4.0 SP4 or later
> Namespace: Included in \root\cimv2
> MOF: Declared in Ping_desc.mof

Is it possible that the .mof just needs to be compiled on NT?
If someone would post the Ping_desc.mof, I can try this out. I don't
have XP myself.

--Matt

msnews.microsoft.com

unread,
May 16, 2002, 10:26:35 AM5/16/02
to
Thanks for you help on this.

However, I'm looking for something like that that works with Windows 2000.
:)

Not all my systems run XP.

"Torgeir Bakken" <Torgeir.B...@hydro.com> wrote in message
news:3CE368C5...@hydro.com...

msnews.microsoft.com

unread,
May 16, 2002, 10:32:56 AM5/16/02
to
I'd like to tell you I was able to pull out the Vbscript part of your
example, but I wasn't able to. Do you have this in pure vbscript?

I don't know, or use JS. What is the advantage of JS using case-sensitive?

Totally appreciate your help
Brian


"Gregory Phillips" <greg...@attbi.com> wrote in message
news:pls6euol1q4fppc4o...@4ax.com...

msnews.microsoft.com

unread,
May 16, 2002, 10:46:44 AM5/16/02
to
You guys are awesome by the way. Thanks for all your help on this stuff. I
have several scripts that I use daily that have your names commented at the
top. I believe in giving credit where it's due. Again, you rock!

Brian

news:enExtWO$BHA.2252@tkmsftngp02...

Alex K. Angelopoulos (MVP)

unread,
May 16, 2002, 10:54:14 AM5/16/02
to
Ok, we have to fall back to the tried-and-true "IsConnectible" function.


Function IsConnectible(sHost,iPings,iTO)
' sHost is a hostname or IP
' iPings is number of ping attempts
' iTO is timeout in milliseconds
' if values are set to "", then defaults below used
If iPings = "" Then iPings = 2
If iTO = "" Then iTO = 750
Set oShell = CreateObject("WScript.Shell")
Set oExCmd = oShell.Exec("ping -n " & iPings _
& " -w " & iTO & " " & sHost)
Select Case InStr(oExCmd.StdOut.Readall,"100% loss")
Case 0 IsConnectible = True
Case Else IsConnectible = False
End Select
End Function

news:enExtWO$BHA.2252@tkmsftngp02...

msnews.microsoft.com

unread,
May 16, 2002, 2:10:26 PM5/16/02
to
I think this is what I'm looking for. It's really fast and returns TRUE when
the server is up. But when the server is down, it times out. No FALSE
response. I dont know why. :\

Brian

"Alex K. Angelopoulos (MVP)" <alexangelop...@hotmail.com> wrote in
message news:Ov7QHmO$BHA.2192@tkmsftngp02...

Torgeir Bakken

unread,
May 16, 2002, 2:39:36 PM5/16/02
to
"Alex K. Angelopoulos (MVP)" wrote:

> Ok, we have to fall back to the tried-and-true "IsConnectible" function.
>
> Function IsConnectible(sHost,iPings,iTO)
> ' sHost is a hostname or IP
> ' iPings is number of ping attempts
> ' iTO is timeout in milliseconds
> ' if values are set to "", then defaults below used
> If iPings = "" Then iPings = 2
> If iTO = "" Then iTO = 750
> Set oShell = CreateObject("WScript.Shell")
> Set oExCmd = oShell.Exec("ping -n " & iPings _
> & " -w " & iTO & " " & sHost)
> Select Case InStr(oExCmd.StdOut.Readall,"100% loss")
> Case 0 IsConnectible = True
> Case Else IsConnectible = False
> End Select
> End Function

Alex, you should consider doing this change:

- from -

Select Case InStr(oExCmd.StdOut.Readall,"100% loss")

- to -

sStdOut = oExCmd.StdOut.Readall
Select Case InStr(sStdOut,"100% loss") Or InStr(sStdOut,"Unknown host")


--
torgeir


Torgeir Bakken

unread,
May 16, 2002, 2:44:22 PM5/16/02
to
"msnews.microsoft.com" wrote:

> I think this is what I'm looking for. It's really fast and returns TRUE when
> the server is up. But when the server is down, it times out. No FALSE
> response. I dont know why. :\

Works perfectly for me.

Could you post the output from the ping run from a command prompt here (when the
server is down).

Note: You should obscure the real IP address/web address in the post

--
torgeir


Alex K. Angelopoulos (MVP)

unread,
May 16, 2002, 3:59:20 PM5/16/02
to
No considering about it, it's done. I didn't run a test against all cases.. :(

XP's ping does one other thing with unknown hosts, an error message that says:

"Ping request could not find host xxx.xxx.xxx. Please check the name and try
again."

Looks like it should be:

Select Case InStr(sStdOut,"100% loss") OR InStr(sStdOut,"Unknown host") OR
InStr(sStdOut,"could not find host")

"Torgeir Bakken" <Torgeir.B...@hydro.com> wrote in message

news:3CE3FCE8...@hydro.com...

Gurgen

unread,
May 16, 2002, 4:42:19 PM5/16/02
to
Hi Alex,

Just a suggestion.....
.............................
Select Case InStr(sStdOut,"TTL=")
Case 0 IsConnectible = False
Case Else IsConnectible = True
End Select
.............................

--
Gurgen Alaverdian
http://www.gurgensvbstuff.com
"Alex K. Angelopoulos (MVP)" <a...@mvps.org> wrote in message
news:uJk6lQR$BHA.2540@tkmsftngp05...

Alex K. Angelopoulos (MVP)

unread,
May 16, 2002, 5:20:09 PM5/16/02
to
Ching!

MUCH better.

"Gurgen" <gur...@bellatlantic.net> wrote in message
news:eV34TpR$BHA.2216@tkmsftngp02...

[MS] Scott McNairy

unread,
May 16, 2002, 8:09:12 PM5/16/02
to
Only your client system has to be XP for this to work.

--
[MS] Scott McNairy
WMI Test Engineer
This posting is provided "As Is" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

news:enExtWO$BHA.2252@tkmsftngp02...

Brian

unread,
May 17, 2002, 2:29:13 PM5/17/02
to
The results of ping when the server is down is:

Request Timed Out
Request Timed Out
Request Timed Out
Request Timed Out
Request Timed Out


"Torgeir Bakken" <Torgeir.B...@hydro.com> wrote in message

news:3CE3FE06...@hydro.com...

Brian

unread,
May 17, 2002, 2:29:27 PM5/17/02
to
Request Timed Out
Request Timed Out
Request Timed Out
Request Timed Out
Request Timed Out
Request Timed Out
Request Timed Out
Request Timed Out
Request Timed Out
"Torgeir Bakken" <Torgeir.B...@hydro.com> wrote in message
news:3CE3FE06...@hydro.com...

Brian

unread,
May 17, 2002, 2:40:41 PM5/17/02
to
Ping running on Windows XP returns serveral varied messages depending on
your network, client, etc.

The usual message is:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Brian>ping 123.123.123.123

Pinging 123.123.123.123 with 32 bytes of data:

Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 123.123.123.123:
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

However, sometimes the message is:

C:\Documents and Settings\Brian>ping nonameserver
Ping request could not find host nonameserver. Please check the name and try
aga
in.


Sometimes I've even seen it say something like:

Response from 192.168.1.1: Host not reachable.

"Alex K. Angelopoulos (MVP)" <a...@mvps.org> wrote in message
news:uJk6lQR$BHA.2540@tkmsftngp05...

Alex K. Angelopoulos (MVP)

unread,
May 17, 2002, 6:40:28 PM5/17/02
to
Looks like Gurgen's change is the best way then:

Select Case InStr(sStdOut,"TTL=")
Case 0 IsConnectible = False
Case Else IsConnectible = True
End Select


"Brian" <su...@hotmail.com> wrote in message news:eUROZJd$BHA.2172@tkmsftngp04...

msnews.microsoft.com

unread,
May 21, 2002, 3:33:11 PM5/21/02
to
Even with this change it doesnt work for me. How are you testing this??
Everytime I run it from within asp it times out.

Brian

"Alex K. Angelopoulos (MVP)" <a...@mvps.org> wrote in message

news:eknuH#f$BHA.1108@tkmsftngp04...

Alex K. Angelopoulos (MVP)

unread,
May 22, 2002, 12:25:18 AM5/22/02
to
That's odd.

Two things you can try:

(1) explicitly set iPings to 4 and iTO to about 3000 or so. See if it still
does it.

(2) If that doesn't fix the problem, try returning all of the results and
writing them to see the results:

Function PingResults(sHost,iPings,iTO)


' sHost is a hostname or IP
' iPings is number of ping attempts
' iTO is timeout in milliseconds
' if values are set to "", then defaults below used
If iPings = "" Then iPings = 2
If iTO = "" Then iTO = 750
Set oShell = CreateObject("WScript.Shell")
Set oExCmd = oShell.Exec("ping -n " & iPings _
& " -w " & iTO & " " & sHost)

PingResults = oExCmd.StdOut.Readall
End Function

news:O$1sV5PACHA.1916@tkmsftngp04...

msnews.microsoft.com

unread,
May 22, 2002, 1:05:45 PM5/22/02
to
With the following script.. I now get output of:

The Server is Offline.
IP address must be specified.

here is the script as it has evolved from this thread:
----------------start
<% Response.Buffer = True %>

<%

shost=tibccswba01

iPings=1

iTO=3000

' sHost is a hostname or IP

' iPings is number of ping attempts

' iTO is timeout in milliseconds

' if values are set to "", then defaults below used

If iPings = "" Then iPings = 2

If iTO = "" Then iTO = 750

Set oShell = CreateObject("WScript.Shell")

Set oExCmd = oShell.Exec("ping -n " & iPings _

& " -w " & iTO & " " & sHost)


PingResults = oExCmd.StdOut.Readall


'Select Case InStr(sStdOut,"TTL=")

Select Case InStr(sStdOut,"100% loss") OR InStr(sStdOut,"Unknown host") OR

InStr(sStdOut,"could not find host") OR InStr(sStdOut,"Request timed out")

Case 0 IsConnectible = False

response.write "The Server is Offline." & "<br>"

response.write pingresults

Case Else IsConnectible = True

response.write "The Server is Online." & "<br>"

response.write pingresults

End Select

%>

----end

Running this from the command line out of asp however, the results are a
normal ping.

ping -n 2 -w 1000 shuttlecraft1

Your guess is as good as mine

"Alex K. Angelopoulos (MVP)" <a...@mvps.org> wrote in message

news:#DaemiUACHA.1680@tkmsftngp05...

Michael Harris (MVP)

unread,
May 22, 2002, 2:31:57 PM5/22/02
to
> <% Response.Buffer = True %>
>
> <%
>
> shost=tibccswba01

The line above should be:

shost="tibccswba01"


>
> iPings=1
>
> iTO=3000
>
> ' sHost is a hostname or IP
>
> ' iPings is number of ping attempts
>
> ' iTO is timeout in milliseconds
>
> ' if values are set to "", then defaults below used
>
> If iPings = "" Then iPings = 2
>
> If iTO = "" Then iTO = 750
>
> Set oShell = CreateObject("WScript.Shell")
>
> Set oExCmd = oShell.Exec("ping -n " & iPings _
>
> & " -w " & iTO & " " & sHost)
>
>
> PingResults = oExCmd.StdOut.Readall
>
>
> 'Select Case InStr(sStdOut,"TTL=")
>
> Select Case InStr(sStdOut,"100% loss") OR InStr(sStdOut,"Unknown host") OR
> InStr(sStdOut,"could not find host") OR InStr(sStdOut,"Request timed out")
>
> Case 0 IsConnectible = False
>
> response.write "The Server is Offline." & "<br>"
>
> response.write pingresults
>
> Case Else IsConnectible = True
>
> response.write "The Server is Online." & "<br>"
>
> response.write pingresults
>
> End Select
>
> %>
>

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US
--


Torgeir Bakken

unread,
May 22, 2002, 2:44:05 PM5/22/02
to
"Michael Harris (MVP)" wrote:

> > shost=tibccswba01
>
> The line above should be:
>
> shost="tibccswba01"

Not only that, his Case test is based on the variable sStdOut that he has not
defined.

--
torgeir


Torgeir Bakken

unread,
May 22, 2002, 2:59:35 PM5/22/02
to
Torgeir Bakken wrote:

Ok Brian, here is a updated version of your script that should work:


<% Response.Buffer = True %>

<%

shost="tibccswba01"

iPings=1

iTO=3000

' sHost is a hostname or IP

' iPings is number of ping attempts

' iTO is timeout in milliseconds

' if values are set to "", then defaults below used

If iPings = "" Then iPings = 2

If iTO = "" Then iTO = 750

Set oShell = CreateObject("WScript.Shell")

Set oExCmd = oShell.Exec("ping -n " & iPings _
& " -w " & iTO & " " & sHost)


PingResults = oExCmd.StdOut.Readall

Select Case InStr(PingResults,"TTL=")

Case 0 IsConnectible = False
response.write "The Server is Offline." & "<br>"
'response.write pingresults

Case Else IsConnectible = True
response.write "The Server is Online." & "<br>"
'response.write pingresults
End Select

%>


--
torgeir


Alex K. Angelopoulos (MVP)

unread,
May 22, 2002, 9:14:53 PM5/22/02
to
Thanks for following up, gentlemen; this is one of those weeks when I feel like
there should be 3 of me to follow up on everything!

"Torgeir Bakken" <Torgeir.B...@hydro.com> wrote in message

news:3CEBEA97...@hydro.com...

0 new messages