TimeOut for ManagementScope.connect()

1322 views
Skip to first unread message

Arkady N.

unread,
Jul 8, 2002, 2:21:06 PM7/8/02
to
hi all,

i am trying to connect to a machine via WMI and get some info from it.
when i connect to an existing machine, everything goes right. but
when i try to connect to a machine that is not on our network (machine
with such IP does not exist) it takes 30 to 60 seconds to come back
with the error "The RPC server is unavailable." i tried to set
ConnectionOptions.Timeout to something low, but this property does not
seem to have any impact on ManagementScope.connect().

so, i would like to know if it is possible to cut down on the time it
takes for me to figure out that machine with given IP does not exists.

thank you for all your responses.

below is a piece of code that i am executing (in C#) :

ManagementScope ms;
ConnectionOptions co;

co = new ConnectionOptions();
co.Username = USERNAME;
co.Password = PASSWD;
co.Timeout = TimeSpan.FromMilliseconds(300);

try
{
ms = new ManagementScope("\\\\10.1.10.110\\root\\cimv2",co);
ms.Connect();
}
catch (COMException COMe)
{
if (COMe.ErrorCode == -2147023174)
// this is where "The RPC server is unavailable." error gets caught
...
...
}

Andy Cheung [MS]

unread,
Jul 8, 2002, 5:00:27 PM7/8/02
to
The ManagementOptions.Timeout property is intended for WMI semi-sync
operations. In particular, the ConnectionOption.Timeout property is not used
to control the timeout for a WMI connection. This is because the underlying
WMI COM API doesn't support specifying a timeout value. The timeout value is
default to the value DCOM specifies (usually 60secs). You can adjust this
value via dcomcnfg.exe.

A nicer workaround for the issue is to first ping the server before
attempting to make a WMI connection to it. Here is a possible code:

...
using System.Net;

string hostName = "ServerNameHere";
bool serverExists = false;
try
{
System.Net.IPHostEntry hostEntry = Dns.GetHostByName(hostName);
serverExists = true;
}
catch(System.Net.Sockets.SocketException)
{
serverExists = false;
}
if (serverExists)
{
Console.WriteLine(@"\\{0} machine exists", hostName);
}
else
{
Console.WriteLine(@"\\{0} machine does not exist", hostName);
}

--
Andy Cheung
Microsoft 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


"Arkady N." <ar...@iname.com> wrote in message
news:8650b20e.02070...@posting.google.com...

Phil Wilson

unread,
Jul 10, 2002, 3:42:33 PM7/10/02
to
This doesn't actually do a ping, does it? As far as I can tell, this test will tell you the server
exists even when it's powered off, because it's only validating the name at a DNS server, not
whether the actual server is up and running.

"Andy Cheung [MS]" <ha...@online.microsoft.com> wrote in message
news:3d29fd69$1...@news.microsoft.com...

Andy Cheung [MS]

unread,
Jul 10, 2002, 6:57:00 PM7/10/02
to
Phil, you're correct. Thanks for pointing it out. Dns class only provides
domain name resolution capability, so my previous snippet is not bullet
proof if a machine is offline. On XP or above, you may find
Wiin32_PingStatus useful. Here is the snippet I use to ping my machines.

string hostName = "HostNameOrIPAddressHere";
SelectQuery query = new SelectQuery("Win32_PingStatus", "Address='" +
hostName + "'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach(ManagementObject result in searcher.Get())
{
if (null != result["StatusCode"] && (0 ==
(UInt32)result["StatusCode"]))
{
Console.WriteLine(@"\\{0} machine exists.", hostName);
}
else
{
Console.WriteLine(@"\\{0} machine does not exists.",
hostName);
}
}

--
Andy Cheung
Microsoft 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


"Phil Wilson" <phil....@unisys.spam.com> wrote in message
news:#x2uSnEKCHA.2228@tkmsftngp08...

Ned Flanders

unread,
Jul 11, 2002, 11:47:24 AM7/11/02
to
Nice, but useless if you don't run XP :-)
You can shell out and run ping with redirect to a temp file, then parse the
file looking for "Request timed out" or "Reply from" to determine whether
the machine is reachable or not. Not terribly elegant but it does work
well.

"Andy Cheung [MS]" <ha...@online.microsoft.com> wrote in message

news:3d2cbbbc$1...@news.microsoft.com...

[MS] Scott McNairy

unread,
Jul 11, 2002, 1:17:42 PM7/11/02
to
You can always connect remotely to an XP Server to do the pinging for you.
Just an idea.

--
[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

"Ned Flanders" <ned...@hotmail.com> wrote in message
news:OAb$2IPKCHA.2556@tkmsftngp11...

Torgeir Bakken

unread,
Jul 11, 2002, 1:37:19 PM7/11/02
to
Ned Flanders wrote:

> Nice, but useless if you don't run XP :-)
> You can shell out and run ping with redirect to a temp file, then parse the
> file looking for "Request timed out" or "Reply from" to determine whether
> the machine is reachable or not. Not terribly elegant but it does work
> well.

Free 3.party pingers with COM interface:

AspPing from http://www.serverobjects.com

System Scripting Runtime
http://www.netal.com/Software/SSR14.ZIP


For ping.exe, the best thing is to test for the text "TTL=", VBScript function
attached:


Function IsConnectible(sHost, iPings, iTO)
' Author: Alex Angelopoulos/Torgeir Bakken
' Works an "all" WSH versions
' 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

Const OpenAsDefault = -2
Const FailIfNotExist = 0
Const ForReading = 1

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
sTempFile = sTemp & "\runresult.tmp"

oShell.Run "%comspec% /c ping -n " & iPings & " -w " & iTO _
& " " & sHost & ">" & sTempFile, 0 , True

Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
FailIfNotExist, OpenAsDefault)

sResults = fFile.ReadAll
fFile.Close
oFSO.DeleteFile(sTempFile)

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


--
torgeir


Ned Flanders

unread,
Jul 11, 2002, 1:56:56 PM7/11/02
to
Whose? Yours? Like a lot of people, we don't have XP in any form here.

"[MS] Scott McNairy" <sco...@online.microsoft.com> wrote in message
news:3d2dbdb7$1...@news.microsoft.com...

black_sp...@hotmail.com

unread,
Feb 2, 2016, 9:16:35 PM2/2/16
to
i must say, THANK YOU FOR THIS WONDERFUL SOLUTION

i tried hard to abort the process in parallel looping... things got dirty

thank you again for this simple solution

pkhal...@gmail.com

unread,
Jan 16, 2019, 10:57:45 AM1/16/19
to
در سه‌شنبه 9 ژوئیهٔ 2002، ساعت 1:30:27 (UTC+4:30)، Andy Cheung [MS] نوشته:
Hi,
I have the same issue,I find that my Server Provider has closed ICMP port so I can not ping the server.
is this the problem of "RPC server is not Available"?
Reply all
Reply to author
Forward
0 new messages