SIGN UP MEMBER LOGIN:    
ARTICLE

Interrogating System with WMI - Part 2

Posted by John O Donnell Articles | Networking January 24, 2002
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.
Reader Level:
Download Files:
 

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 - csharpconsulting@hotmail.com");
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());
}
}
}
}

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor