SCCM: VBScript to add multiple systems into a collection
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.
- It asks for the server name i.e. Configuration Manager Site Server name
- It asks for the Configuration Manager Site code
- It asks for the Collection ID in which you need to add the systems into
- 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
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.
Double click the Vbs file
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")]
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")]
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")]
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")]
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.
- 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.
- In the second message box, it will just give the information to refer to the log file to see any failures.
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:
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
- 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.
- 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.
- 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 ‘ Get the SMS record for the selected computer outFile=”.\SystemAdded.txt” Do while objReadfile.AtEndOfStream <> TRUE CompName=”” ‘ Create the Rule to add the System to the Collection ‘ Query to retrieve the desired collection if oCollSet.Count = 0 then Else |