1  Introduction

This is the VBScript which will help preventing manually added the machines into the collection. We can make the WQL Query Based Collections to avoid the manual addition of systems. However, sometimes customer provides the list of systems/machine names in a way that we can’t run SQL queries to build the list.

For such scenarios, it takes a huge amount of time to manually add each systems into the collection. And sometimes, we are needed to create huge no of collections, all with the direct membership rule. Hence to save the time and efforts, I have created this VBScript which will take the input i.e. list of machines from the text file and add them without having to do anything just one-click.

2  Applicability

This script should be copied to the Configuration Manager Site Server on which we need to perform the actions. This script has been tested and already been used in both SCCM 2007 and SCCM 2012 environment.

2.1  Prerequisites

As this script is generic and applicable to any environment, it prompts for the user input when we execute this script. It asks for below 4 parameters which we should have before execution of this script.

  1.  It asks for the server name i.e. Configuration Manager Site Server name
  2.  It asks for the Configuration Manager Site code
  3. It asks for the Collection ID in which you need to add the systems into
  4. It asks for the path of input file which contains the list of systems

3  Step-by-step approach on how to use this script

  1. Copy the attached script to any drive on the Configuration Manager Site Server. Make sure you have the write permissions on the drive on which you have copied this script to because this script will create a log file in the same directory in which you have copied this script file to.
  2. Double click the Vbs file
  3. It first asks for the Site Server name. Please enter the FQDN of the local Site Server and click OK.

    [strSiteServer = Inputbox("Please enter the Site Server Name")]

  4. Then you will have to enter the Configuration Manager Site Code in the below window and click OK.

    [strSiteCode = InputBox("Please enter the Site Code")]

  5. Then retrieve the Collection ID, which you would need to add the systems into, from SCCM 2007/2012 and put in the below window. Click OK.

    [strcollID = InputBox("Please provide the collection ID in which you need to add the systems")]

  6. Finally, it asks for the path of the file containing the list of devices which you need to add into the collection. Please provide the path in the below window and click OK.

    [fileLocation = InputBox("Please provide the full path of the file which contains the list of systems")]

  7. Then the execution engine will run in the background for a while depending on the no of systems in the list. It will generate two message boxes in the end.
    1. In the first message box, it will give you the count of systems it processed, systems it has successfully added and systems it has failed to add. Here we can validate if the no of entries processed are same as given in the list.
    2.  In the second message box, it will just give the information to refer to the log file to see any failures.
  8. This script will create log file with the name “SystemAdded” in the same directory in which we have run the script from. Here is the screenshot of log file with the status messages:
  9. Please pick the system names which the script failed to add. We will have to add them manually into the collection. For the reason of failure, please refer to error handling section.

4  Error Handling

  1.  We provide the list of systems in the text file. And each system entry is verified against the database for the unique entry. If configuration manager database contains more than 1 entry of the same systems, script will not add the system into the collection and report it in the log file as mentioned above. The reason for more than 1 entry is generally because one entry is obsolete and is yet to be deleted from the database. Using this script, we will be able to clean up the database as well if needed.
  2.  Secondly, if you mistyped the collection ID by any chance, the script will error out and report in the database to cross check and type the correct ID. It will end up with the below message.
  3. All of the other error messages will itself be handled by Host Engine and the messages will be self-explanatory to work on.

Script

Dim strCollID ‘ CollectionID of the selected Collection
Dim ResID ‘ ResourceID of the System
dim compName ‘ ComputerName of the System
dim lLocator ‘ SWbemLocator object
dim gService ‘ SWbemServices object
dim oSystem ‘ SMS_R_System object
dim oSystemSet ‘ Collection of SMS_R_System objects
dim oCollRule ‘ SMS_CollectionRuleDirect object
dim oCollection ‘ SMS_Collection object
dim oCollSet ‘ Collection of SMS_Collection objects
Dim strSiteServer ‘ SCCM Site Server name
Dim strSiteCode ‘ SCCM Site Code
Dim fileLocation ‘ Text file with the list of systems
Int s = 0
Int t = 0
Int f = 0
strSiteServer = Inputbox(“Please enter the Site Server Name”)
strSiteCode = InputBox(“Please enter the Site Code”)
strcollID = InputBox(“Please provide the collection ID in which you need to add the systems”)
fileLocation = InputBox(“Please provide the full path of the file which contains the list of systems”)
set lLocator = CreateObject(“WbemScripting.SWbemLocator”)
Set gService = lLocator.ConnectServer(strSiteServer, “root/sms/site_” & strSiteCode)

‘ Get the SMS record for the selected computer
Set objFSO=CreateObject(“Scripting.FileSystemObject”)
Set objFSO1=CreateObject(“Scripting.FileSystemObject”)
Set objReadFile=objFSO.OpenTextFile(fileLocation)

outFile=”.\SystemAdded.txt”
Set logFile = objFSO1.CreateTextFile(outfile,True)

Do while objReadfile.AtEndOfStream <> TRUE

CompName=””
CompName=trim(objReadFile.ReadLine)
set oSystemSet = gService.ExecQuery(“Select * From SMS_R_System Where Name = ” & chr(34) & CompName & chr(34))
if oSystemSet.count = 1 Then
for each oSystem in oSystemSet
ResID = oSystem.ResourceID
next

‘ Create the Rule to add the System to the Collection
Set oCollRule = gService.Get(“SMS_CollectionRuleDirect”).SpawnInstance_()
oCollRule.ResourceClassName = “SMS_R_System”
oCollRule.RuleName = CompName
oCollRule.ResourceID = ResID

‘ Query to retrieve the desired collection
set oCollSet = gService.ExecQuery(“Select * From SMS_Collection Where CollectionID = ” & Chr(34) & strCollID & Chr(34))

if oCollSet.Count = 0 then
msgbox “Error: The specified collection ID does not exist”
logFile.Write “Please check the collection ID and try again”
return
else
for each oCollection In oCollSet
oCollection.AddMembershipRule oCollRule
logFile.Write “Success: System name ” & compName & ” added successfully into the collection ID – ” & strcollID & vbcrlf
s=s+1
Next
end If

Else
logFile.Write “Failure: There is more than 1 entry in the database for ” & compName & ” Please add the system manually” & vbCrLf
f=f+1
end If
t=t+1
Loop
logFile.Close
objReadFile.Close
MsgBox “The total no of records processed are ” & t & “. Successfully Added: ” & s & “. Failure: ” & f
msgbox “Please refer to the failure in the log file and add them manually”