Interrogating System with WMI - Part 2


After my last article on WMI it was time to get serious. WMI or Windows Management Instrumentation is useful on a local machine to produce a WINMSD type program but using it across a network makes it even more useful.

How many companies have thousands of machines and would love to inventory them without buying a product like Microsoft SMS? Using this technology you could get a list of IP addresses and then tell your server to start scanning machines on the network through the night. Perhaps you could write all the data to a database. Note WMI also allows you to collect performance data etc.

WMI is standard on Win2000 Professional and Server. You also need to have admin rights to access the data. WMI downloads for NT4 etc are available. 

In this code written with Beta 2 you will need to change the username, password and ip address to access your own machines.

Enjoy

using
System;
using System.Management;
namespace WMI2
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
Console.WriteLine("Computer details retrieved using Windows Management Instrumentation (WMI)");
Console.WriteLine("Written 01/23/02 By John O'Donnell - [email protected]");
Console.WriteLine("=========================================================================");
//Connect to the remote computer
ConnectionOptions co = new ConnectionOptions();
co.Username = "john";
co.Password = "john";
System.Management.ManagementScope ms =
new System.Management.ManagementScope("\\\\192.168.1.4\\root\\cimv2", co);
//Query remote computer across the connection
System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher query1 =
new ManagementObjectSearcher(ms,oq);
ManagementObjectCollection queryCollection1 = query1.Get();
foreach( ManagementObject mo in queryCollection1 )
{
Console.WriteLine("Name : " + mo["name"].ToString());
Console.WriteLine("Version : " + mo["version"].ToString());
Console.WriteLine("Manufacturer : " + mo["Manufacturer"].ToString());
Console.WriteLine("Computer Name : " +mo["csname"].ToString());
Console.WriteLine("Windows Directory : " +mo["WindowsDirectory"].ToString());
}
oq =
new System.Management.ObjectQuery("SELECT * FROM Win32_ComputerSystem");
query1 =
new ManagementObjectSearcher(ms,oq) ;
queryCollection1 = query1.Get();
foreach( ManagementObject mo in queryCollection1 )
{
//Console.WriteLine("Manufacturer : " + mo["manufacturer"].ToString());
//Console.WriteLine("Model : " + mo["model"].ToString());
Console.WriteLine(mo["systemtype"].ToString());
Console.WriteLine("Total Physical Memory : " + mo["totalphysicalmemory"].ToString());
}
oq =
new System.Management.ObjectQuery("SELECT * FROM Win32_processor") ;
query1 =
new ManagementObjectSearcher(ms,oq) ;
queryCollection1 = query1.Get();
foreach( ManagementObject mo in queryCollection1 )
{
Console.WriteLine(mo["caption"].ToString());
}
oq =
new System.Management.ObjectQuery("SELECT * FROM Win32_bios");
query1 =
new ManagementObjectSearcher(ms,oq) ;
queryCollection1 = query1.Get();
foreach( ManagementObject mo in queryCollection1 )
{
Console.WriteLine(mo["version"].ToString());
}
}
}
}


Similar Articles