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
...
...
}
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...
"Andy Cheung [MS]" <ha...@online.microsoft.com> wrote in message
news:3d29fd69$1...@news.microsoft.com...
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...
"Andy Cheung [MS]" <ha...@online.microsoft.com> wrote in message
news:3d2cbbbc$1...@news.microsoft.com...
--
[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...
> 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
"[MS] Scott McNairy" <sco...@online.microsoft.com> wrote in message
news:3d2dbdb7$1...@news.microsoft.com...