T
Tom
I have the following vbscript that creates a WMI Class and sets the Key. How
would I do this in C#?
option explicit
Const wbemCimtypeString = 8
Dim oLocation, oServices, oInstances, oObject, oDataObject, oNewObject
Set oLocation = CreateObject("WbemScripting.SWbemLocator")
'Remove class'
on error resume next
Set oServices = oLocation.ConnectServer(, "root\cimv2")
set oNewObject = oServices.Get("TestCode")
oNewObject.Delete_
on error goto 0
'Create data class structure '
Set oServices = oLocation.ConnectServer(, "root\cimv2")
Set oDataObject = oServices.Get
oDataObject.Path_.Class = "TestCode"
oDataObject.Properties_.add "Name", wbemCimtypeString
oDataObject.Properties_.add "ScanTime", wbemCimtypeString
oDataObject.Properties_.add "Description", wbemCimtypeString
oDataObject.Properties_("Name").Qualifiers_.add "key", True
oDataObject.Put_
This is what I have so far:
static void Main(string[] args)
{
try
{
ManagementScope scopeDevice1 = new
ManagementScope("root\\CIMV2");
scopeDevice1.Options.EnablePrivileges = true; //sets
required privilege
scopeDevice1.Options.Impersonation =
System.Management.ImpersonationLevel.Impersonate;
ManagementPath managementPath1 = new
ManagementPath("TestCode");
ManagementClass printerClass1 = new
ManagementClass(scopeDevice1, managementPath1, null);
printerClass1.Properties.Add("Name", CimType.String,false);
printerClass1.Properties.Add("Description", CimType.String,
false);
printerClass1.Properties.Add("Scantime", CimType.String,
false);
//printerClass1.SetPropertyQualifierValue("Name","read",
"here i come to save the day");
printerClass1.Put();
}
catch (Exception ex)
{
//throw new Exception( String.Format( "WMI exception: {0}",
ex.Message));
Console.WriteLine(String.Format("Create Class: {0}",
ex.Message));
}
try
{
ManagementScope scopeDevice = new
ManagementScope("root\\CIMV2");
scopeDevice.Options.EnablePrivileges = true; //sets required
privilege
scopeDevice.Options.Impersonation =
System.Management.ImpersonationLevel.Impersonate;
ManagementPath managementPath = new
ManagementPath("TestCode");
ManagementClass printerClass = new
ManagementClass(scopeDevice, managementPath, null);
ManagementObject printerObject =
printerClass.CreateInstance();
printerObject["Name"] = "Test";
printerObject["Description"] = "I want this to work";
printerObject["Scantime"] = "This is the time";
PutOptions options = new PutOptions();
options.Type = PutType.UpdateOrCreate;
printerObject.Put(options);
}
catch ( Exception ex)
{
//throw new Exception( String.Format( "WMI exception: {0}",
ex.Message));
Console.WriteLine ( String.Format( "Add Data: {0}",
ex.Message));
}
Console.ReadLine();
}
If the class exists, then the properties are set, but no data added. If I
manually set the key, then the data is added.
Thanks.
would I do this in C#?
option explicit
Const wbemCimtypeString = 8
Dim oLocation, oServices, oInstances, oObject, oDataObject, oNewObject
Set oLocation = CreateObject("WbemScripting.SWbemLocator")
'Remove class'
on error resume next
Set oServices = oLocation.ConnectServer(, "root\cimv2")
set oNewObject = oServices.Get("TestCode")
oNewObject.Delete_
on error goto 0
'Create data class structure '
Set oServices = oLocation.ConnectServer(, "root\cimv2")
Set oDataObject = oServices.Get
oDataObject.Path_.Class = "TestCode"
oDataObject.Properties_.add "Name", wbemCimtypeString
oDataObject.Properties_.add "ScanTime", wbemCimtypeString
oDataObject.Properties_.add "Description", wbemCimtypeString
oDataObject.Properties_("Name").Qualifiers_.add "key", True
oDataObject.Put_
This is what I have so far:
static void Main(string[] args)
{
try
{
ManagementScope scopeDevice1 = new
ManagementScope("root\\CIMV2");
scopeDevice1.Options.EnablePrivileges = true; //sets
required privilege
scopeDevice1.Options.Impersonation =
System.Management.ImpersonationLevel.Impersonate;
ManagementPath managementPath1 = new
ManagementPath("TestCode");
ManagementClass printerClass1 = new
ManagementClass(scopeDevice1, managementPath1, null);
printerClass1.Properties.Add("Name", CimType.String,false);
printerClass1.Properties.Add("Description", CimType.String,
false);
printerClass1.Properties.Add("Scantime", CimType.String,
false);
//printerClass1.SetPropertyQualifierValue("Name","read",
"here i come to save the day");
printerClass1.Put();
}
catch (Exception ex)
{
//throw new Exception( String.Format( "WMI exception: {0}",
ex.Message));
Console.WriteLine(String.Format("Create Class: {0}",
ex.Message));
}
try
{
ManagementScope scopeDevice = new
ManagementScope("root\\CIMV2");
scopeDevice.Options.EnablePrivileges = true; //sets required
privilege
scopeDevice.Options.Impersonation =
System.Management.ImpersonationLevel.Impersonate;
ManagementPath managementPath = new
ManagementPath("TestCode");
ManagementClass printerClass = new
ManagementClass(scopeDevice, managementPath, null);
ManagementObject printerObject =
printerClass.CreateInstance();
printerObject["Name"] = "Test";
printerObject["Description"] = "I want this to work";
printerObject["Scantime"] = "This is the time";
PutOptions options = new PutOptions();
options.Type = PutType.UpdateOrCreate;
printerObject.Put(options);
}
catch ( Exception ex)
{
//throw new Exception( String.Format( "WMI exception: {0}",
ex.Message));
Console.WriteLine ( String.Format( "Add Data: {0}",
ex.Message));
}
Console.ReadLine();
}
If the class exists, then the properties are set, but no data added. If I
manually set the key, then the data is added.
Thanks.