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


'************************************************************************************************************
' Jonas Hettich
'************************************************************************************************************
' Ver 1.00 - 13.04.2011 - initial version
' What this script does:
' This Scripts creates the Collection Structure defined in the file CollectionStructure.txt
'************************************************************************************************************
result = MsgBox("Do you really want to create the Collections?", vbYesNo)
If result = vbNo Then WScript.Quit

Dim strSiteServer : strSiteServer = ""
Dim strSitecode : strSitecode = ""
Dim objSWbemLocator : Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Dim objSWbemServices : Set objSWbemServices = objSWbemLocator.ConnectServer(strSiteServer,"root/sms/site_" & strSitecode)

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile : Set objFile = objFSO.OpenTextFile("CollectionStructure.txt")
Dim strCurrentLine
Dim strRootCollection, strChildCollection
Dim strRootCollectionID, strChildCollectionID

'Loop the Source File
Do While not objFile.AtEndOfStream
strCurrentLine = objFile.ReadLine

'Skip Comment and Blank lines
If strCurrentLine <> "" Then
If Not Left(strCurrentLine,1) = "#" Then
'Parse the Root- and Child Collection
strRootCollection = Split(strCurrentLine,";")(0)
strChildCollection = Split(strCurrentLine,";")(1)

'Create the collection.
Dim strpath
Dim objChildCollection : Set objChildCollection = objSWbemServices.Get("SMS_Collection").SpawnInstance_()
objChildCollection.Name = strChildCollection
objChildCollection.OwnedByThisSite = True
strpath=objChildCollection.Put_

If Err.Number = 0 Then
WScript.Echo "Created Collection: " &strChildCollection
Else
WScript.Echo "Error creating Collection: " &strChildCollection &" - ErrNumber:" &Err.Number
End If
Err.Clear

'Get Collection ID
Dim objCollection : Set objCollection=objSWbemServices.Get(strpath)
strChildCollectionID = objCollection.CollectionID


'Configure Root Collection
Dim objCollectionRelation
Set objCollectionRelation = objSWbemServices.Get( "SMS_CollectToSubCollect" ).SpawnInstance_()
If strRootCollection = "COLLROOT" Then
objCollectionRelation.parentCollectionID = strRootCollection
Else
objCollectionRelation.parentCollectionID = CollectionNameToID(strRootCollection)
End If
objCollectionRelation.subCollectionID = strChildCollectionID
objCollectionRelation.Put_

If Err.Number = 0 Then
WScript.Echo "Linked to Root Collection: " &strRootCollection
Else
WScript.Echo "Error when linking to Root Collectionn: " &strRootCollection &" - ErrNumber:" &Err.Number
End If
End If
End If
Loop

Function CollectionNameToID(strName)
Dim strCollection
Dim intCollectionID : intCollectionID = ""
Dim allCollections : Set allCollections = objSWbemServices.ExecQuery("SELECT * FROM SMS_Collection WHERE Name = '" & strName & "'")
For Each strCollection In allCollections
intCollectionID = strCollection.CollectionID
Next

CollectionNameToID = intCollectionID

End Function

No comments: