1

i would like to import a computer to a specific SCCM-collection.

I've found this method on the 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;
    }
}

Now, I try to call it with a button event:

private void button2_Click(object sender, EventArgs e)
    {
        AddNewComputer(PcNameBox.Text, MacAdrBox, PcNameBox.Text, CollectionDropDown.Text);
    }

But I'm sorry, I don't no how to call the WQLConnectionManager on this point?! I know the object must be preset before "PcNameBox.Text". That's all :(

On another event, I call it like this:

SmsNamedValuesDictionary namedValues = new SmsNamedValuesDictionary();
WqlConnectionManager connection = new WqlConnectionManager(namedValues);
connection.Connect(PrimarySiteServer);

But this Method is struggling me. (It's only a hobby, pleave be indulgent)

Thanks in advance for a hint...

Chris

iByteU
  • 55
  • 6

1 Answers1

0

Google is your friend, friend:

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

Example below:

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;
    }
}
Matt
  • 151
  • 1
  • 7
  • Please don't give link-only answers. If the link dies, the answer is useless. – Millie Smith Nov 11 '17 at 19:55
  • As requested, copied and pasted example code in case Microsoft suddenly disappears or randomly decides to remove an article that's sat there happily untouched for the last 8 years. (Only joking, I get your point) – Matt Nov 12 '17 at 08:59
  • What I personally found confusing about these Microsoft examples when I first got into this is that they all require references to AdminUI.WqlQueryEngine.dll and/or Microsoft.ConfigurationManagement.ManagementProvider.dll that can be found in your SCCM Consoles install directory\bin. I don't know why anyone would post sample code without mentioning the references but MS does it all the time. It also always confused me why they would even use these dlls for their examples because they are basically just WMI calls and can be done with the default methods but I just hoped it has some hidden point – Syberdoor Nov 16 '17 at 09:12