【问题标题】:Add Computer in SCCM and import it to a specific Collection在 SCCM 中添加计算机并将其导入特定集合
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 请不要给出仅链接的答案。如果链接死了,答案就没用了。
    • 应要求复制并粘贴示例代码,以防 Microsoft 突然消失或随机决定删除过去 8 年未触及的文章。 (只是开玩笑,我明白你的意思)
    • 当我第一次接触这些 Microsoft 示例时,我个人感到困惑的是,它们都需要引用 AdminUI.WqlQueryEngine.dll 和/或 Microsoft.ConfigurationManagement.ManagementProvider.dll,可以在您的 SCCM 控制台安装目录\bin。我不知道为什么有人会在不提及引用的情况下发布示例代码,但 MS 一直在这样做。它也总是让我感到困惑,为什么他们甚至会在他们的示例中使用这些 dll,因为它们基本上只是 WMI 调用,可以使用默认方法完成,但我只是希望它有一些隐藏点
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多