在SCCM中添加计算机并将其导入特定的集合

时间:2017-11-10 17:43:05

标签: c# visual-studio wmi configurationmanager sccm

我想将计算机导入特定的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);

但这种方法正在困扰着我。 (这只是一种爱好,恳求放纵)

提前感谢提示...

克里斯

1 个答案:

答案 0 :(得分:0)

Google是你的朋友,朋友:

https://msdn.microsoft.com/en-us/library/cc146404.aspx

以下示例:

public WqlConnectionManager Connect(string serverName, string userName, string userPassword)
{
    try
    {
        SmsNamedValuesDictionary namedValues = new SmsNamedValuesDictionary();
        WqlConnectionManager connection = new WqlConnectionManager(namedValues);

        if (System.Net.Dns.GetHostName().ToUpper() == serverName.ToUpper())
        {
            // Connect to local computer.
            connection.Connect(serverName);
        }
        else
        {
            // Connect to remote computer.
            connection.Connect(serverName, userName, userPassword);
        }

        return connection;
    }
    catch (SmsException e)
    {
        Console.WriteLine("Failed to Connect. Error: " + e.Message);
        return null;
    }
    catch (UnauthorizedAccessException e)
    {
        Console.WriteLine("Failed to authenticate. Error:" + e.Message);
        return null;
    }
}