string pc = System.Environment.MachineName;
if (args.Count() == 1)
pc = args[0];
// Ограничение на поле class = 33 символа
string[] List = new string[] {
"Win32_BaseBoard",
"Win32_BIOS",
"Win32_ComputerSystem",
//"Win32_Environment",
"Win32_LogicalDisk",
"Win32_NetworkAdapter",
"Win32_OperatingSystem",
"Win32_NetworkAdapterConfiguration"
};
SqlConnection cmdb = new SqlConnection("server=vm-mssql01p;" +
"Trusted_Connection=yes;" +
"database=CMDB; " +
"connection timeout=10");
cmdb.Open();
foreach (string l in List)
{
Console.WriteLine("Class: " + l);
try
{
//ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BaseBoard");
ManagementScope s = new ManagementScope(@"\\" + pc + @"\root\cimv2");
ManagementPath p = new ManagementPath(l);
ObjectGetOptions o = new ObjectGetOptions(
null, System.TimeSpan.MaxValue, true);
ManagementClass mClass = new ManagementClass(s, p, o);
//ManagementClass = new ManagementClass(l);
mClass.Options.UseAmendedQualifiers = true;
//PropertyDataCollection properties = mClass.Properties;
int instance = 1;
foreach (ManagementObject queryObj in mClass.GetInstances())
{
foreach (PropertyData prop in queryObj.Properties)
{
String str = String.Empty;
//add these to your arraylist or dictionary
//Console.WriteLine("{0}: {1}: {2}", prop.Name, prop.Type, prop.Value);
if (prop.Value != null)
{
if (prop.Value.GetType().BaseType == typeof(Array))
{
Array list = (prop.Value as Array);
foreach (object o1 in list)
str += o1.ToString() + " ";
if (list.Length > 0)
str = str.Substring(0, str.Length - 1);
}
else // value is already a string
str = prop.Value.ToString();
}
//Console.WriteLine("{0}: {1}", prop.Name, str);
String query = @"
begin tran
Update Win32 with(serializable)
set value = @value
where
class = @class and
computer = @computer and
name = @name and
instance = @instance;
if @@ROWCOUNT = 0
begin
INSERT INTO Win32 (class, computer, name, instance, value) VALUES(@class, @computer, @name, @instance, @value);
end
commit tran
";
SqlCommand command = new SqlCommand(query, cmdb);
command.Parameters.AddWithValue("@class", l);
command.Parameters.AddWithValue("@computer", pc);
command.Parameters.AddWithValue("@name", prop.Name.ToString());
command.Parameters.AddWithValue("@instance", instance);
if (prop.Value != null)
{
command.Parameters.AddWithValue("@value", str);
command.ExecuteNonQuery();
}
//else
//command.Parameters.AddWithValue("@value", "");
}
instance++;
}
//Console.ReadLine();
/*
foreach (PropertyData property in mClass.Properties)
{
//Console.WriteLine("Name: " + property.Name);
int instance = 1;
foreach (ManagementObject c in mClass.GetInstances())
{
//Console.WriteLine("Value: " + c.Properties[property.Name.ToString()].Value);
//Console.WriteLine();
if (c.Properties[property.Name.ToString()].IsArray)
{
//string val = c.Properties[property.Name.ToString()].Value.ToString().Length.ToString();
//string val = String.Join("", c.Properties[property.Name.ToString()]);
//MessageBox.Show(c.Properties[property.Name.ToString()].Value.ToString());
}
String query = @"
begin tran
Update Win32 with(serializable)
set value = @value
where
class = @class and
computer = @computer and
name = @name and
instance = @instance;
if @@ROWCOUNT = 0
begin
INSERT INTO Win32 (class, computer, name, instance, value) VALUES(@class, @computer, @name, @instance, @value);
end
commit tran
";
SqlCommand command = new SqlCommand(query, cmdb);
command.Parameters.Add("@class", l);
command.Parameters.Add("@computer", pc);
command.Parameters.Add("@name", property.Name.ToString());
command.Parameters.Add("@instance", instance);
if (c.Properties[property.Name.ToString()].IsArray || c.Properties[property.Name.ToString()].Value == null || c.Properties[property.Name.ToString()].Value == "")
command.Parameters.Add("@value", "");
else
command.Parameters.Add("@value", c.Properties[property.Name.ToString()].Value.ToString());
command.ExecuteNonQuery();
instance++;
}
}
*/
}
catch (ManagementException e)
{
Console.WriteLine("An error occurred while querying for WMI data: " + e.Message);
}
catch (System.UnauthorizedAccessException unauthorizedErr)
{
Console.WriteLine("Connection error (user name or password might be incorrect): " + unauthorizedErr.Message);
}
catch (Exception err)
{
Console.WriteLine(err);
Application.Exit();
}
}
try
{
Console.WriteLine("Class: MSFC_FCAdapterHBAAttributes");
ManagementClass mClass = new ManagementClass(
new ManagementScope (@"\\" + pc + @"\root\WMI"),
new ManagementPath("MSFC_FCAdapterHBAAttributes"),
new ObjectGetOptions(null, System.TimeSpan.MaxValue, true)
);
//ManagementClass = new ManagementClass(l);
mClass.Options.UseAmendedQualifiers = true;
//PropertyDataCollection properties = mClass.Properties;
int instance = 1;
foreach (ManagementObject queryObj in mClass.GetInstances())
{
foreach (PropertyData prop in queryObj.Properties)
{
String str = String.Empty;
//add these to your arraylist or dictionary
//Console.WriteLine("{0}: {1}: {2}", prop.Name, prop.Type, prop.Value);
if (prop.Value != null)
{
if (prop.Value.GetType().BaseType == typeof(Array))
{
Array list = (prop.Value as Array);
foreach (object o1 in list)
str += o1.ToString() + " ";
if (list.Length > 0)
str = str.Substring(0, str.Length - 1);
}
else // value is already a string
str = prop.Value.ToString();
}
//Console.WriteLine("{0}: {1}", prop.Name, str);
String query = @"
begin tran
Update Win32 with(serializable)
set value = @value
where
class = @class and
computer = @computer and
name = @name and
instance = @instance;
if @@ROWCOUNT = 0
begin
INSERT INTO Win32 (class, computer, name, instance, value) VALUES(@class, @computer, @name, @instance, @value);
end
commit tran
";
SqlCommand command = new SqlCommand(query, cmdb);
command.Parameters.AddWithValue("@class", "MSFC_FCAdapterHBAAttributes");
command.Parameters.AddWithValue("@computer", pc);
command.Parameters.AddWithValue("@name", prop.Name.ToString());
command.Parameters.AddWithValue("@instance", instance);
if (prop.Value != null)
{
command.Parameters.AddWithValue("@value", str);
command.ExecuteNonQuery();
}
}
instance++;
}
}
catch (Exception err)
{
Console.WriteLine(err);
}
cmdb.Close();
//Console.ReadLine();