Here’s a class I wrote awhile back for doing WMI lookup’s against remote hosts and writing the results back to a database, it’s pretty straightforward and could easily be modified to write the results anywhere.
class WMIsql
{
//threading stuff copied from WMIreader.cs
editItem parentForm;
ManualResetEvent m_EventStop;
ManualResetEvent m_EventStopped;
public string computer;
public WMIsql(ManualResetEvent eventStop, ManualResetEvent eventStopped, Form form)
{
m_EventStop = eventStop;
m_EventStopped = eventStopped;
form = parentForm;
}
//build the queries for WMI
public void wmiQueryBuilder()
{
//check to see if an entry already exists for the computer and delete it if it does
wmiCheck(computer);
//array of wmi queries
string[] wmiClasses = new string[] {
“Win32_Processor”, //0
“Win32_ComputerSystemProduct”, //1
“Win32_ComputerSystem”, //2
“Win32_OperatingSystem”, //3
“Win32_LogicalDisk”, //4
“Win32_MemoryDevice”, //5
“Win32_VideoController” }; //6
ArrayList queryFor = new ArrayList();
foreach (string wmiClass in wmiClasses)
{
queryFor.Clear();
string type = wmiClass;
string query = “”;
switch (wmiClass)
{
case “Win32_Processor”:
query = “SELECT * FROM Win32_Processor”;
//CPU Model
queryFor.Add("Name");
//Clock speed
queryFor.Add("MaxClockSpeed");
break;
case “Win32_ComputerSystemProduct”:
query = “SELECT * FROM Win32_ComputerSystemProduct”;
//Motherboard or system name
queryFor.Add("Name");
//Computer Manufacturer
queryFor.Add("Vendor");
//Serial Number
queryFor.Add("IdentifyingNumber");
break;
case “Win32_ComputerSystem”:
query = “SELECT * FROM Win32_ComputerSystem”;
//Computer Name
queryFor.Add("Name");
//grab remote username
queryFor.Add("UserName");
break;
case “Win32_OperatingSystem”:
query = “SELECT * FROM Win32_OperatingSystem”;
//OS Name
queryFor.Add("Caption");
//Build Number
queryFor.Add("Version");
//Service Pack Number
queryFor.Add("CSDVersion");
//Additional information (Server R2 for example)
queryFor.Add("OtherTypeDescription");
//Windows Folder Location
queryFor.Add("WindowsDirectory");
break;
case “Win32_LogicalDisk”:
query = “SELECT * FROM Win32_LogicalDisk”;
//Drive Title
queryFor.Add("DeviceID");
//Drive Type ie. removable media, cd-rom etc.
queryFor.Add("Description");
//Filesystem
queryFor.Add("FileSystem");
//Free space on drive
queryFor.Add("FreeSpace");
//Total space
queryFor.Add("Size");
break;
case “Win32_MemoryDevice”:
query = “SELECT * FROM Win32_MemoryDevice”;
//Total Memory
queryFor.Add("EndingAddress");
break;
case “Win32_VideoController”:
query = “SELECT * FROM Win32_VideoController”;
//Card Model Name
queryFor.Add("Name");
break;
}
wmiQuery(computer, query, queryFor, type);
}
}
//check if theirs already entries for the pc in the database, and if their are, delete them
public void wmiCheck(string computer)
{
//need to import form1 so we can use the get machine id class in it, really should move that into globals at some point
Form1 console = new Form1();
//SQL Stuff
globals Global = new globals();
SqlConnection addWMIinfo = new SqlConnection(Global.connect);
//Grab MachineID for inserting into database
string machineid = console.getID(computer);
//Check if theirs already a listing for the pc in the databace
SqlCommand checkExists = new SqlCommand(
“SELECT * FROM WMIData WHERE MachineID = ‘“ + machineid + ”’ “, addWMIinfo);
//delete existing entries for the machine if it exists
SqlCommand delMachine = new SqlCommand(
“DELETE FROM WMIData WHERE MachineID = ‘“ + machineid + ”’ “, addWMIinfo);
//boolean value to set whether the machine is already in wmi database
bool exists = false;
try
{
//Open SQL connection
addWMIinfo.Open();
SqlDataReader check = checkExists.ExecuteReader();
check.Read();
if (check.FieldCount != -1)
{
exists = true;
}
check.Close();
}
catch (Exception)
{
}
finally
{
addWMIinfo.Close();
}
//delete entries if they exist already
try
{
addWMIinfo.Open();
if (exists == true)
{
delMachine.ExecuteNonQuery();
}
}
catch (Exception ex)
{
MessageBox.Show("Error removing pre-existing WMI information:\n” + ex.Message);
}
finally
{
addWMIinfo.Close();
}
}
//run the query against the remote pc and write the values to the database
public void wmiQuery(string computer, string query, ArrayList items, string type)
{
//need to import form1 so we can use the get machine id class in it, really should move that into globals at some point
Form1 console = new Form1();
//SQL Stuff
globals Global = new globals();
SqlConnection addWMIinfo = new SqlConnection(Global.connect);
SqlCommand addInfo = new SqlCommand(
“INSERT INTO WMIData ( “ +
“MachineID, Data, WMIName, Type ) VALUES “ +
“( @MachineID, @Data, @WMIName, @Type )”, addWMIinfo);
ConnectionOptions wmiConnect = new ConnectionOptions();
System.Management.ManagementScope oMs = new System.Management.ManagementScope(
“\\\\” + computer + “\\root\\CIMV2”, wmiConnect);
System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery(query);
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher(
oMs, oQuery);
//Grab MachineID for inserting into database
string machineid = console.getID(computer);
//Open SQL connection
addWMIinfo.Open();
foreach (ManagementObject queryObj in searcher.Get())
{
foreach (string item in items)
{
addInfo.Parameters.Clear();
if (queryObj[item] == null)
{
}
else
{
try
{
addInfo.Parameters.Add(
“@MachineID”, SqlDbType.Int).Value = machineid;
addInfo.Parameters.Add(
“@Data”, SqlDbType.Text).Value = queryObj[item].ToString();
addInfo.Parameters.Add(
“@WMIName”, SqlDbType.NVarChar).Value = item;
addInfo.Parameters.Add(
“@Type”, SqlDbType.NVarChar).Value = type;
//add data for current object into database
addInfo.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("Error updating WMI information in database:\n” + ex.Message);
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error grabbing WMI Information, please make sure “+computer+” is turned on:\n” + ex.Message + “\n” + query);
}
finally
{
//close SQL
addWMIinfo.Close();
}
}
}
Comments: (0)


















Post Tags: microsoft code sql wmi c#
Next entry: Automated Out of Band Hotfix Downloads Previous entry: Hmmm, sharepoint 2003 'master'?