Mukesh
2007-02-24 01:41:03 UTC
I am trying to connect to servers in my domain and gathering the members of
Administrator group. I have multiple threads calling WMI functions to gather
the administrators. It works fine for some time but starts failing with "RPC
server is unavailable" messages. Soon all the threads report the same
exceptions . I dont get any more results. Anybody seen somthing like this
before?
I am using vs 2003 and .Net 1.1
below is my function which runs with all worker threads.
static void GetAdministratorsInfo()
{
Thread.CurrentThread.Name = Convert.ToString(ThreadNum++);
Thread.CurrentThread.ApartmentState=ApartmentState.MTA;
Console.WriteLine("Starting New Thread with identity: {0}",
Thread.CurrentThread.Name);
ConnectionOptions co = new ConnectionOptions();
co.Impersonation = ImpersonationLevel.Impersonate;
co.Authentication = AuthenticationLevel.PacketPrivacy;
co.Authority = "NTLMDOMAIN:MYDOMAIN";
co.Username = LogonAccount;
co.Password = GetAssetOwner.Password;
ManagementScope ms = new ManagementScope();
ManagementPath mp = new ManagementPath();
ObjectQuery oq = new ObjectQuery();
ManagementObjectCollection queryCollection;
oq.QueryLanguage = "WQL";
ManagementObjectSearcher query = new ManagementObjectSearcher();
while (RunNormal)
{
cInHost objOS = null;
inMut.WaitOne();
//Console.WriteLine("Input Mutex held by {0}", Thread.CurrentThread.Name);
if (aIn.Count > 0)
{
objOS = (cInHost)aIn[0];
aIn.RemoveAt(0);
}
inMut.ReleaseMutex();
//Console.WriteLine("Input Mutex Released by {0}",
Thread.CurrentThread.Name);
if (objOS != null)
{
try
{
Console.WriteLine("Thread<{0}>:Trying Hostid <{1}> HostName <{2}>",
Thread.CurrentThread.Name, objOS.HOSTID, objOS.NAME);
string resolvedName = "", osDate = "";
mp.Path = "\\\\" + objOS.NAME + "\\root\\cimv2";
ms.Path = mp ;
ms.Options = co;
//1. Get the Computer Name
oq.QueryString = "Select Name from Win32_ComputerSystem";
query.Scope= ms;
query.Query= oq;
queryCollection = query.Get();
query.Dispose();
foreach ( ManagementObject mo in queryCollection)
{
resolvedName = mo["Name"].ToString();
if (resolvedName.Length > 0)
{
break;
}
}
queryCollection.Dispose();
// 2. OS Date
oq.QueryString = "Select * from Win32_OperatingSystem";
query.Scope= ms;
query.Query= oq;
queryCollection = query.Get();
query.Dispose();
foreach ( ManagementObject mo in queryCollection)
{
osDate =
ManagementDateTimeConverter.ToDateTime((mo["InstallDate"].ToString())).ToString("yyyy:MM:dd hh:mm:ss.fff");
if (osDate.Length > 0)
{
break;
}
}
queryCollection.Dispose();
// 3. Administrators
oq.QueryString = "Select PartComponent from Win32_GroupUser where
GroupComponent=\"Win32_Group.Domain='" + resolvedName +
"',Name='Administrators'\"";
query.Scope= ms;
query.Query= oq;
queryCollection = query.Get();
query.Dispose();
foreach ( ManagementObject mo in queryCollection)
{
string [] strParts = mo["PartComponent"].ToString().Split('=');
string name = strParts[2].Trim('\"');
string domain = strParts[1].Replace(",Name","").Trim('\"');
if (name.Length > 0 && domain.Length > 0)
{
string aOwner = domain + "\\" + name;
outMut.WaitOne();
aOut.Add(new cOutHost(objOS.HOSTID,
objOS.NAME.ToUpper(),resolvedName.ToUpper(), osDate, aOwner));
outMut.ReleaseMutex();
Console.WriteLine("Thread<{0}>: <{1}> identified as owner for HostName
<{2}>", Thread.CurrentThread.Name, aOwner, resolvedName);
}
}
queryCollection.Dispose();
}
catch (Exception e)
{
query.Dispose();
Console.WriteLine("Thread<{0}>: Exception Occured: {1}
",Thread.CurrentThread.Name, e.Message);
Console.WriteLine("Thread<{0}>: Source {1} ",Thread.CurrentThread.Name,
e.Source);
Console.WriteLine("Thread<{0}>: Stack Trace\n {1}
",Thread.CurrentThread.Name, e.StackTrace);
Thread.Sleep(3000);
}
//clear all WMI components
}
else
{
Console.WriteLine("Thread<{0}>: Sleeping", Thread.CurrentThread.Name);
Thread.Sleep(10000);
}
}
}
I get the exception at queryCollection = query.Get();
Administrator group. I have multiple threads calling WMI functions to gather
the administrators. It works fine for some time but starts failing with "RPC
server is unavailable" messages. Soon all the threads report the same
exceptions . I dont get any more results. Anybody seen somthing like this
before?
I am using vs 2003 and .Net 1.1
below is my function which runs with all worker threads.
static void GetAdministratorsInfo()
{
Thread.CurrentThread.Name = Convert.ToString(ThreadNum++);
Thread.CurrentThread.ApartmentState=ApartmentState.MTA;
Console.WriteLine("Starting New Thread with identity: {0}",
Thread.CurrentThread.Name);
ConnectionOptions co = new ConnectionOptions();
co.Impersonation = ImpersonationLevel.Impersonate;
co.Authentication = AuthenticationLevel.PacketPrivacy;
co.Authority = "NTLMDOMAIN:MYDOMAIN";
co.Username = LogonAccount;
co.Password = GetAssetOwner.Password;
ManagementScope ms = new ManagementScope();
ManagementPath mp = new ManagementPath();
ObjectQuery oq = new ObjectQuery();
ManagementObjectCollection queryCollection;
oq.QueryLanguage = "WQL";
ManagementObjectSearcher query = new ManagementObjectSearcher();
while (RunNormal)
{
cInHost objOS = null;
inMut.WaitOne();
//Console.WriteLine("Input Mutex held by {0}", Thread.CurrentThread.Name);
if (aIn.Count > 0)
{
objOS = (cInHost)aIn[0];
aIn.RemoveAt(0);
}
inMut.ReleaseMutex();
//Console.WriteLine("Input Mutex Released by {0}",
Thread.CurrentThread.Name);
if (objOS != null)
{
try
{
Console.WriteLine("Thread<{0}>:Trying Hostid <{1}> HostName <{2}>",
Thread.CurrentThread.Name, objOS.HOSTID, objOS.NAME);
string resolvedName = "", osDate = "";
mp.Path = "\\\\" + objOS.NAME + "\\root\\cimv2";
ms.Path = mp ;
ms.Options = co;
//1. Get the Computer Name
oq.QueryString = "Select Name from Win32_ComputerSystem";
query.Scope= ms;
query.Query= oq;
queryCollection = query.Get();
query.Dispose();
foreach ( ManagementObject mo in queryCollection)
{
resolvedName = mo["Name"].ToString();
if (resolvedName.Length > 0)
{
break;
}
}
queryCollection.Dispose();
// 2. OS Date
oq.QueryString = "Select * from Win32_OperatingSystem";
query.Scope= ms;
query.Query= oq;
queryCollection = query.Get();
query.Dispose();
foreach ( ManagementObject mo in queryCollection)
{
osDate =
ManagementDateTimeConverter.ToDateTime((mo["InstallDate"].ToString())).ToString("yyyy:MM:dd hh:mm:ss.fff");
if (osDate.Length > 0)
{
break;
}
}
queryCollection.Dispose();
// 3. Administrators
oq.QueryString = "Select PartComponent from Win32_GroupUser where
GroupComponent=\"Win32_Group.Domain='" + resolvedName +
"',Name='Administrators'\"";
query.Scope= ms;
query.Query= oq;
queryCollection = query.Get();
query.Dispose();
foreach ( ManagementObject mo in queryCollection)
{
string [] strParts = mo["PartComponent"].ToString().Split('=');
string name = strParts[2].Trim('\"');
string domain = strParts[1].Replace(",Name","").Trim('\"');
if (name.Length > 0 && domain.Length > 0)
{
string aOwner = domain + "\\" + name;
outMut.WaitOne();
aOut.Add(new cOutHost(objOS.HOSTID,
objOS.NAME.ToUpper(),resolvedName.ToUpper(), osDate, aOwner));
outMut.ReleaseMutex();
Console.WriteLine("Thread<{0}>: <{1}> identified as owner for HostName
<{2}>", Thread.CurrentThread.Name, aOwner, resolvedName);
}
}
queryCollection.Dispose();
}
catch (Exception e)
{
query.Dispose();
Console.WriteLine("Thread<{0}>: Exception Occured: {1}
",Thread.CurrentThread.Name, e.Message);
Console.WriteLine("Thread<{0}>: Source {1} ",Thread.CurrentThread.Name,
e.Source);
Console.WriteLine("Thread<{0}>: Stack Trace\n {1}
",Thread.CurrentThread.Name, e.StackTrace);
Thread.Sleep(3000);
}
//clear all WMI components
}
else
{
Console.WriteLine("Thread<{0}>: Sleeping", Thread.CurrentThread.Name);
Thread.Sleep(10000);
}
}
}
I get the exception at queryCollection = query.Get();