【发布时间】:2018-04-24 00:19:36
【问题描述】:
我想将计算机导入特定的 SCCM 集合。
我在msdn上找到了这个方法:
public int AddNewComputer(
WqlConnectionManager connection,
string netBiosName,
string smBiosGuid,
string macAddress)
{
try
{
if (smBiosGuid == null && macAddress == null)
{
throw new ArgumentNullException("smBiosGuid or macAddress must be defined");
}
// Reformat macAddress to : separator.
if (string.IsNullOrEmpty(macAddress) == false)
{
macAddress = macAddress.Replace("-", ":");
}
// Create the computer.
Dictionary<string, object> inParams = new Dictionary<string, object>();
inParams.Add("NetbiosName", netBiosName);
inParams.Add("SMBIOSGUID", smBiosGuid);
inParams.Add("MACAddress", macAddress);
inParams.Add("OverwriteExistingRecord", false);
IResultObject outParams = connection.ExecuteMethod(
"SMS_Site",
"ImportMachineEntry",
inParams);
// Add to All System collection.
IResultObject collection = connection.GetInstance("SMS_Collection.collectionId='ABC0000A'");
IResultObject collectionRule = connection.CreateEmbeddedObjectInstance("SMS_CollectionRuleDirect");
collectionRule["ResourceClassName"].StringValue = "SMS_R_System";
collectionRule["ResourceID"].IntegerValue = outParams["ResourceID"].IntegerValue;
Dictionary<string, object> inParams2 = new Dictionary<string, object>();
inParams2.Add("collectionRule", collectionRule);
collection.ExecuteMethod("AddMembershipRule", inParams2);
return outParams["ResourceID"].IntegerValue;
}
catch (SmsException e)
{
Console.WriteLine("failed to add the computer" + e.Message);
throw;
}
}
现在,我尝试使用按钮事件来调用它:
private void button2_Click(object sender, EventArgs e)
{
AddNewComputer(PcNameBox.Text, MacAdrBox, PcNameBox.Text, CollectionDropDown.Text);
}
但是对不起,我不知道如何在这一点上调用 WQLConnectionManager?!我知道对象必须在“PcNameBox.Text”之前预设。就是这样:(
在另一个事件中,我这样称呼它:
SmsNamedValuesDictionary namedValues = new SmsNamedValuesDictionary();
WqlConnectionManager connection = new WqlConnectionManager(namedValues);
connection.Connect(PrimarySiteServer);
但是这种方法让我很苦恼。 (纯属爱好,请放纵)
提前感谢您的提示...
克里斯
【问题讨论】:
标签: c# visual-studio wmi configurationmanager sccm