Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# with WMI problem

Status
Not open for further replies.

cipro

Technical User
Apr 27, 2002
50
CA
I have written code that reads in a list of computers and retrieves infomation regarding free HD space using WMI. No matter what remote computers are in the list, I always get info for my localhost. The entries in the list are formated like this:

\\\\computer1
\\\\computer2

Here is my Code:
using System;
using System.Management;
using System.IO;

namespace CheckDiskSpace
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string[] computers = getList("C:\\list.txt");
foreach (string s in computers)
{
CheckDisks(s);
}
Console.ReadLine();
}

static string[] getList(string path)
{
StreamReader txtStream = File.OpenText(path);
int counter = 0;
string input = null;
while ((input = txtStream.ReadLine()) != null)
{
counter = counter + 1;
}
txtStream.Close();
Console.WriteLine("There are " + counter + " servers in the list");
string[] list = new string[counter];
input = null;
counter = 0;
txtStream = File.OpenText(path);
while ((input = txtStream.ReadLine()) != null)
{
list[counter] = input;
counter = counter + 1;
}
return list;
}

static void CheckDisks(string remotePC)
{
long mb = 1048576; //bytes in a Mb
ConnectionOptions oConn = new ConnectionOptions();
System.Management.ManagementScope oMs = new System.Management.ManagementScope(remotePC, oConn);
System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("select FreeSpace, Size, Name from Win32_LogicalDisk where DriveType=3");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs,oQuery);

ManagementObjectCollection oReturnCollection = oSearcher.Get();

double freespace = 0;
double usedspace = 0;
double totalspace = 0;
double percentused = 0;
double percentfree = 0;

Console.WriteLine("********************************");
Console.WriteLine("** " + remotePC + " Hard Disks **");
Console.WriteLine("********************************");

foreach (ManagementObject oReturn in oReturnCollection)
{
Console.WriteLine("Name : " + oReturn["Name"].ToString());
totalspace = Convert.ToInt64(oReturn["Size"])/mb;
freespace = Convert.ToInt64(oReturn["FreeSpace"])/mb;
usedspace = (totalspace - freespace)/mb;
percentused = (usedspace/totalspace) * 100;
percentfree = (freespace/totalspace) * 100;

Console.WriteLine("Free Space: " + freespace + " Mb");
}
oConn = null;
oMs = null;
oQuery = null;
oSearcher = null;
}
}
}




Thanks for the help.
 
Changed the following line and got it to work:
System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\\\" + remotePC + "\\root\\cimv2", oConn);

However, I cant run my queries against windows 2003 machines(I am running from a windows 2000 machine). It is telling me access is denied. I am running the script as the domain admin and the 2003 PC is on the domain.

Any ideas?
 
its all good. I figured this whole one out on my own.
 
Now that you've whetted our appetites, you have to tell us how. Run it from a 2003 machine, perhaps?
 
as far as my code goes, everything seems to be working correctly. I can run queries against most windows 2003/2000 machines. There are 2 machines that I cannot query, but the issue seems to be with the machines rather than the code. The only change that I made was the one that I posted earlier.

If anyone thinks that I am incorrect about my assumption regarding the 2 2003 machines that will not work please let me know and maybe suggest a solution.

Thanks for the help.
 
forgot to add one thing. I did add a few lines for impersonation, but it didn't make a difference with the access to 2 of the 2003 servers
 
You probably needed to enable remote access or the dcom configuration to access, the win 2003 servers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top