SCCM Add Computer Associations With Powershell

I can’t wait to get ConfigMgr 2012 up and running. Beta 2 is going into my lab as i type…

Until then…. Yes, ConfigMgr 2007 is fantastic, i love it. But i deploy it at customer sites and as a rule the technicals who are to run and operate it once i have gone have not taken any form of SCCM training. With 2007’s MMC Console it can be a bit daunting for them, so much information, and the access security is not what it should be. So until the launch of 2012 with it’s lovely interfaces and user-friendly-centric operations how can i enable my customers to carry out day-to-day tasks in SCCM?

First of all identify the key stakeholders: Who wants SCCM? Who needs SCCM? Why do they want it? What role in the business do they expect it perform?

FD: Paying for this new technology, wants return on investment.

IT Manager or similar: Probably wants deployment processes streamlined for their staff, to enable them to work more efficiently. Wants to know what IT equipment and licensing they have. Doesn’t care about the technical side.

IT Technician: More interested in the technical aspects. May want to get under the hood, but without training not advisable. Has (hopefully) documented the deployment and operational processes they want to streamline. Knows the applications, the estate, the environment.

For FDs and IT Management, the built-in Reports may well be enough to get information on the estate and inventory, with R2’s Reporting Services Point enhancing this functionality.

For Technicians, those who will be running and maintaining the SCCM environment and carrying out the deployment and administrative tasks with SCCM the MMC console can be a maze of nodes and components.

I identified a need for a customer to carry out basic tasks, such as Import Computer Information. They wanted to quickly add in new hardware to receive OSD Task Sequence Advertisements via PXE. I created a simple tool in Powershell which they could run from their desktop and easily add a new laptop or computer for OS deployment.

First of all, and i won’t go into too much detail here, i set up an OSD Task Sequence, a Collection and advertised the TS. 

In normal circumstances, the technician would open the SCCM console, find the collection with the TS advert and drill-down through the OSD Node, to import the computer using the wizard.

All these steps are possible with WMI.

 The basic powershell to add a computer account to SCCM:

#create Computer Account in SCCM





$inParamsAddEntry.MACAddress =$mACAddress.Text

$inParamsAddEntry.OverwriteExistingRecord =$false

$inParamsAddEntry.NetbiosName =$serverName.Text

$objComputer=$siteClass.psbase.InvokeMethod($methodAddEntry, $inParamsAddEntry, $null)

Next add the direct collection membership rule, this always applies to the collection and not the computer:



$objCollectionRuleDirect.psbase.Properties[“ResourceClassName”].Value = “SMS_R_System”

$objCollectionRuleDirect.psbase.Properties[“ResourceID”].value = $objComputer.ResourceID


And add the computer to the collection using this rule:

$targetCollection=Get-WmiObject -ComputerName “$siteProviderName” -Namespace “root\sms\site_$siteCode” -Class “SMS_Collection” | where{$_.Name -eq $determinedCollection}




$addToCollectionResult=$targetCollection.psbase.InvokeMethod($methodAddToCollection, $inParamsAddToCollection, $null)

I also used Computer Variables to designate if the computer is to be installed with german, french or italian regional settings and keyboard layouts, this is useful when working in Switzerland. First you create an empty array of machine variables:

#create machine task sequence variables



$computerInstance = $machineClass.CreateInstance()









Then you can add variables, i use a Windows Form object to take all this information in from input:



I know i have a left a lot out here, and i will skip quickly to the end, but the important thing i have found is that once you harness WMI and powershell, with windows form objects, you can create all manner of dazzling(?) tools to help make SCCM life a bit easier.

Here is a couple of screengrabs of my tool:

For client management, Roger Zander’s Client Center is still freely available from sourceforge:

And for another method to set regional settings in a task sequence, i found this post very interesting:

15 thoughts on “SCCM Add Computer Associations With Powershell

  1. James Avery April 25, 2012 / 1:58 am

    Can you share the code with the Powershell to associate two computers?

    • andrewdcraig April 25, 2012 / 11:08 am

      Hi James, to associate more than one computer you should use a text file with the comma-separated computer details, I.e. name and Mac address. Then pass this into the script in a foreach loop. You need a get-content statement to read in the file contents to an array:
      Function add-assoc($name,$mac){
      $content=get-content ‘c:\file.txt’
      Foreach($line in $content){
      Add-assoc $computername $macaddress

      Is this what you mean?

      • lewolfeur May 23, 2014 / 10:32 am

        hi andrewdcraig,
        i have to import many computer, this code and file.txt with name, mac and a variablen what i have to add ?
        For my case the variable is usmt with yes or no.

  2. James Avery April 25, 2012 / 8:25 pm

    Kind of, I need to have computer A and Computer B association in the OSD Computer Association in SCCM. I have vbscript working, but I would like to have this work in powershell. My issue is, I don’t have the MAC addresses. In my VBScript, I have it searching for the resource ID in SCCM then taking that resourceID and associate the two computers by resourceID.

    My plan is to utilize Primalforms to create a GUI interface to allow contractor desktop tech’s to submit via the GUI and not utilize the SCCM Console or require someone else to do this.

    From what you’re showing it looks like the code would be something like:

    Function add-assoc($srcComputerName,$destComputerName){
    Add-assoc $srcComputerName $destComputerName

    Would this be correct?

    • andrewdcraig April 25, 2012 / 9:38 pm

      Hi, do you want to create a source and destination association? As if you are migrating settings from one computer to another? If you want to send me the vbs function I can convert to powershell. The importmachineentry method doesn’t have a parameter for source computer. And are you working with existing machines? With existing guids?

  3. James Avery April 25, 2012 / 10:16 pm

    Hello Andrew, yes, I want input a Source and Computer computer in SCCM Computer Association through Powershell. Yes, I’m working with existing machines having existing GUIDS.

    Here’s the entire HTA with VB Script. I didn’t want to fill up your blog post with it.

    Here’s the parameter your looking for.
    Set inParams = _
    inParams.SourceClientResourceID = referenceComputerResourceId
    inParams.RestoreClientResourceID = destinationComputerResourceId
    ‘ Call the method
    Set outParams = objSMS.ExecMethod( “SMS_StateMigration”,”AddAssociation”,inParams)

    Thank you for all of your help!

    • andrewdcraig April 26, 2012 / 7:31 am

      Hi, hope this helps:

      function AddAssoc($computerA_resID, $computerB_resID){
      $stateMigrationClass = [wmiclass]’\\SITE_SERVER\root\sms\site_SITECODE:sms_statemigration’
      $inParams = $stateMigrationClass.psbase.GetMethodParameters(“AddAssociation”)
      $inParams.SourceClientResourceID = $computerA_resID
      $inParams.RestoreClientResourceID = $computerB_resID
      $outParams = $stateMigrationClass.psbase.InvokeMethod(“AddAssociation”, $inParams, $null)
      return $outParams
      $computerA = #Get Resource ID of desired computer
      $computerB = #Get Resource ID of desired computer
      $returnParams = AddAssoc $computerA $computerB

  4. windwaker23 May 23, 2012 / 6:22 pm

    I have configuration manager already running on my local computer. I use it to add machines to a collection that in turn installs software on the client’s machines. I want to use powershell to be able to set up this so that the machine automatically joins the collections that I have specified. Is this possible? I am running Config Manager 2007 by the way.

    • andrewdcraig May 23, 2012 / 8:13 pm

      Hi, yes you can use wmiprovider in Powershell to add computers to collections. One thing to consider is that with 2012 now on the market you really should be using query based collections, for future migrations. Having said that you may not migrate this collection so sticking with 2007 you can programmatically add the machine with direct membership and remove again after the software is installed. Do you have a trigger? When should the machine be added? And how is the software advertised? I would define an event as a trigger, add the computer, then install the software using a task sequence so you can add a step to remove it after. I will post an example tomorrow as I’m shutting down now for the night. More food for thought: DCM is good for querying and automatically install software, so is extending the sms_def.mof
      Hope this helps for now,

      • andrewdcraig May 24, 2012 / 8:40 am

        The powershell for adding to a collection is:

        $computername = ‘mycomputer’
        $servername = ‘sccm_server’
        $sitecode = ‘mysitecode’
        $collectionName = ‘myCollection’

        $objComputer = gwmi -computerName $serverName -nameSpace “root\sms\site_$sitecode” -class ‘SMS_R_System’ -filter “Name=’$computerName'”

        $objCollRuleDirect = [wmiclass]”\\sccm_server\root\sms\site_$sitecode:SMS_CollectionRuleDirect”
        $objCollRuleDirect.psbase.Properties[“ResourceClassName”].Value = “SMS_R_System”
        $objCollRuleDirect.psbase.Properties[“ResouceID”].Value = $objComputer.ResourceID

        $collection = gwmi -computerName $serverName -nameSpace “root\sms\site_$sitecode” -class ‘SMS_Collection’ -filter “Name=’$collectionName'”

        $inParams = $collection.psbase.GetMethodParameters(“AddMembershipRule”)
        $inParams.collectionRule = $objCollRuleDirect

        So you can use a trigger to invoke this code, if you write as a powershell script you can edit script parameters to accept input, and you can put it in a task sequence installation to remove the membership afterwards (using DeleteMemberShipRule)


  5. windwaker23 June 4, 2012 / 5:58 pm

    Sorry to just be getting back to you now. I have a collection called APPS and under that collection is a bunch of subcollections that are essentially the apps i want to install. When I install I use the sccm right click tools and choose the “Add machines to the collection” option which provides a form for a list of computer names. Once the machine is added to the collection my job is done as other administrators remove the machine later.

    Is it possible to add a machine to a folder under apps? I have not tried your script because I currently do not have a test environment and want to get as much information about the process before I try it on a live system.

    • andrewdcraig June 4, 2012 / 8:08 pm

      Subcollections in wmi and SQL are treated the same as collections, essentially in a flat hierarchy. So the wmi queriesand methods will work with subcollections just the same. Try exploring the wmi namespace with wbemtest or wmiexplorer from thepowershellguy. It helps to actually see the contents of the namespaces and classes… Also, use the -whatif parameter in powershell, or just run the query in a shell window if you don’t have a test environment.

  6. Rhicm June 19, 2012 / 10:45 am

    Where can I find your awesome SCCM Tool?😉

    • andrewdcraig June 21, 2012 / 8:43 pm

      I’ll post the code tomorrow. And I’ll post up my software updates tool, as a wee bonus.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s