4/13/2011

vbScript - bulk import collections

This little vbscript loops throught the file CollectionStructure.txt and creates all the defined collections

# are and blank lines are ignored
# are available for comments

The CollectionStructure.txt file should look like this:

############################################
################ EXAMPLES ##################
############################################
### Parent Collection;Child Collection
### COLLROOT;MyCollection1
### Collection1;MyCollection2
############################################
############################################
############################################

#Create Collections for Department XYZ
COLLROOT;Department XYZ
Department XYZ;Department XYZ-123
Department XYZ;Department XYZ-ABC
Department XYZ;Department XYZ-LOL

#Create Collections for Adobe Reader
SWDIST;Adobe Reader
Adobe Reader;Install_Adobe Reader
Adobe Reader;Uninstall_Adobe Reader


The vbScript is the below code - Have fun ;)
Note: There's not build in a lot of error handling, so please first test it

  1. '************************************************************************************************************  
  2. ' Jonas Hettich  
  3. '************************************************************************************************************  
  4. ' Ver 1.00 - 13.04.2011 - initial version  
  5. ' What this script does:  
  6. ' This Scripts creates the Collection Structure defined in the file CollectionStructure.txt  
  7. '************************************************************************************************************  
  8. result = MsgBox("Do you really want to create the Collections?", vbYesNo)  
  9. If result = vbNo Then WScript.Quit  
  10.   
  11. Dim strSiteServer : strSiteServer = ""  
  12. Dim strSitecode : strSitecode = ""  
  13. Dim objSWbemLocator : Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")  
  14. Dim objSWbemServices : Set objSWbemServices = objSWbemLocator.ConnectServer(strSiteServer,"root/sms/site_" & strSitecode)  
  15.   
  16. Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")  
  17. Dim objFile : Set objFile = objFSO.OpenTextFile("CollectionStructure.txt")  
  18. Dim strCurrentLine  
  19. Dim strRootCollection, strChildCollection  
  20. Dim strRootCollectionID, strChildCollectionID  
  21.   
  22. 'Loop the Source File  
  23. Do While not objFile.AtEndOfStream  
  24.  strCurrentLine = objFile.ReadLine  
  25.    
  26.  'Skip Comment and Blank lines  
  27.  If strCurrentLine <> "" Then  
  28.   If Not Left(strCurrentLine,1) = "#" Then  
  29.    'Parse the Root- and Child Collection  
  30.    strRootCollection = Split(strCurrentLine,";")(0)  
  31.    strChildCollection = Split(strCurrentLine,";")(1)  
  32.      
  33.    'Create the collection.  
  34.    Dim strpath  
  35.       Dim objChildCollection : Set objChildCollection = objSWbemServices.Get("SMS_Collection").SpawnInstance_()  
  36.       objChildCollection.Name = strChildCollection  
  37.       objChildCollection.OwnedByThisSite = True  
  38.       strpath=objChildCollection.Put_  
  39.         
  40.       If Err.Number = 0 Then  
  41.        WScript.Echo "Created Collection: " &strChildCollection  
  42.       Else  
  43.     WScript.Echo "Error creating Collection: " &strChildCollection &" - ErrNumber:" &Err.Number  
  44.       End If  
  45.       Err.Clear  
  46.         
  47.       'Get Collection ID  
  48.       Dim objCollection : Set objCollection=objSWbemServices.Get(strpath)  
  49.       strChildCollectionID = objCollection.CollectionID  
  50.      
  51.      
  52.    'Configure Root Collection  
  53.       Dim objCollectionRelation  
  54.       Set objCollectionRelation = objSWbemServices.Get"SMS_CollectToSubCollect" ).SpawnInstance_()  
  55.       If strRootCollection = "COLLROOT" Then  
  56.        objCollectionRelation.parentCollectionID = strRootCollection  
  57.       Else  
  58.        objCollectionRelation.parentCollectionID = CollectionNameToID(strRootCollection)  
  59.       End If  
  60.       objCollectionRelation.subCollectionID = strChildCollectionID  
  61.       objCollectionRelation.Put_  
  62.         
  63.       If Err.Number = 0 Then  
  64.        WScript.Echo "Linked to Root Collection: " &strRootCollection  
  65.       Else  
  66.     WScript.Echo "Error when linking to Root Collectionn: " &strRootCollection &" - ErrNumber:" &Err.Number  
  67.       End If  
  68.   End If  
  69.  End If  
  70. Loop  
  71.   
  72. Function CollectionNameToID(strName)  
  73.  Dim strCollection  
  74.  Dim intCollectionID : intCollectionID = ""  
  75.  Dim allCollections : Set allCollections = objSWbemServices.ExecQuery("SELECT * FROM SMS_Collection WHERE Name = '" & strName & "'")  
  76.  For Each strCollection In allCollections  
  77.   intCollectionID = strCollection.CollectionID  
  78.  Next  
  79.   
  80.  CollectionNameToID = intCollectionID  
  81.    
  82. End Function  

No comments: