How To Add Multiple Machines To Collections In SCCM

Did you know you can add multiple machines to a collection in SCCM without an AD query? To add multiple machines to collections in SCCM:

(Note: If you want to add multiple machine to a collection from an AD group, you want to Google how to populate an SCCM colleciton with a query)

  1. Right click the collection you want to add multiple machines to, in SCCM
  2. Click Properties
  3. Make note of the Collection ID (In the box at the bottom of the General tab)
  4. Create a file called AddCompResID.vbs and paste the following code into it (ammending the ServerName and any other attributes necessary to yuour environment. NOTE: You may have to amend the quote marks as my CMS system will have altered them to become curly quotes, rather than straight quotes):

    strCompName = Wscript.Arguments.Item(0)
    strCollID = Wscript.Arguments.Item(1)

    Set objSWbemLocator = CreateObject(“WbemScripting.SWbemLocator”)
    Set objSWbemServices= objSWbemLocator.ConnectServer(“ServerName”, “root\sms”)

    Set ProviderLoc = objSWbemServices.InstancesOf(“SMS_ProviderLocation”)
     
    For Each Location In ProviderLoc
        If Location.ProviderForLocalSite = True Then
            Set objSWbemServices = objSWbemLocator.ConnectServer _
                (Location.Machine, “root\sms\site_” + Location.SiteCode)
        End If
    Next

    Set colCompResourceID = objSWbemServices.ExecQuery(“SELECT ResourceID FROM SMS_R_System WHERE ” & _
                                                  “NetbiosName='” & strCompName & “‘”)

    For each insCompResource in colCompResourceID
     strNewResourceID = insCompResource.ResourceID
    Next

    Set instColl = objSWbemServices.Get(“SMS_Collection.CollectionID=””” & strCollID & “”””)
    Set instDirectRule = objSWbemServices.Get(“SMS_CollectionRuleDirect”).SpawnInstance_()

    instDirectRule.ResourceClassName = “SMS_R_System”
    instDirectRule.ResourceID = strNewResourceID
    instDirectRule.RuleName = strComputerName
    instColl.AddMembershipRule instDirectRule

  5. Create a file called Comp.txt in the same directory and enter a list of multiple machines to add to the collection. The list should contain a computer name on each line, with no seperating characters (such as comma’s or tabs)
  6. Create a batch file with the following code (ammending the Collection to the collection found in step 1):
    @echo off

    Set _COLID=Collection
    Set _log=AddToCollection.log

    echo Starting log file > %_log%

    for /f %%a in (comp.txt) do call :AddComp %%a

    goto :eof 

    :AddComp

    cscript //nologo AddCompResID.vbs %1 %_COLID%
    Echo. Adding Computer %1 to SCCM CollectionID %_COLID%
    Echo. Adding Computer %1 to SCCM CollectionID %_COLID% >> %_log%

    goto :eof

  7. Run the batch file to add the list of multiple machines to the SCCM collection
VN:F [1.9.22_1171]
Rating: 3.0/10 (1 vote cast)
How To Add Multiple Machines To Collections In SCCM, 3.0 out of 10 based on 1 rating

Leave a comment

Your email address will not be published. Required fields are marked *