在 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);

但是这种方法让我很挣扎。(这只是一种爱好,放纵一下(

提前感谢您的提示...

克里斯

谷歌是你的朋友,朋友:

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;
    }
}

最新更新