O R G A N I C / F E R T I L I Z E R: sms: moving collections through a command line...

Aug 27, 2007

sms: moving collections through a command line...

well, during my travel, i find myself at a site whose collection management was a little sparse. to help move them along, i wrote up a script to move a collection to a new parent collection, through a command line. it was a little bit of a challenge because there weren't very many samples floating around.

many thanks to dave lippa and the rest of the group at myitforum for their help! i shamelessly stole stuff out of greg ramsey's script. the script is located here.

there's not that much to it, actually. you simply tell it the old parent, the new parent, and the id of the collection you're moving. i suppose it's more concept than anything else. it'd be easy to make this thing much more useful... but you know... necessity. less words. :| just keep in mind when doing something like this that in order to remove the current collection link, you have to add a new one. otherwise, it will go through as if it works but never remove it.

order it correctly and things are great. 1. add. 2. delete.

[usage] sms_collmove.vbs
[example] sms_collmove.vbs collroot abc0019a abc0014a

 

strSMSServer = "<SMS Server Name>"

Set objArguments = WScript.Arguments
If objArguments.Count <> 3 Then
    ListHelp
    WScript.Quit
End If

strOldParentColl = UCase(objArguments(0))
strNewParentColl = UCase(objArguments(1)) 'parent collection ID
strSubColl =       UCase(objArguments(2)) 'collection ID to link

Set objLoc =  CreateObject("WbemScripting.SWbemLocator")
Set objSMS = objLoc.ConnectServer(strSMSServer, "root\sms")
Set Results = objSMS.ExecQuery("SELECT * From SMS_ProviderLocation WHERE ProviderForLocalSite = true")
For Each Loc in Results
    If Loc.ProviderForLocalSite = True Then
        Set objSMS = objLoc.ConnectServer(Loc.Machine, "root\sms\site_" & Loc.SiteCode)
    End If
Next

' Add a collection link
Set ColSvcs = objSMS.Get("SMS_Collection")

' Verify no looping in the collection
ColSvcs.VerifyNoLoops "SMS_Collection.CollectionID=""" & _
    strNewParentColl & """", "SMS_Collection.CollectionID=""" & _
    strSubColl & """", Result

if Result = 0 then
    wscript.echo "Link would cause looping condition, exiting"
else
    Set objCol = objSMS.Get("SMS_CollectToSubCollect").SpawnInstance_()
    objCol.parentCollectionID = strNewParentColl
    objCol.subCollectionID = strSubColl
    objCol.Put_
    wscript.echo "Added collection link: " & strSubColl & " to " & strNewParentColl & " ..."
end If

' Delete a collection link
Set Coll = objSMS.ExecQuery("select * from sms_collecttosubcollect where parentcollectionid= '" & strOldParentColl & "' and subcollectionid = '" & strSubColl & "'")

For Each colitem In Coll
    WScript.Echo "Deleting subcollection link: " & colitem.subcollectionid & " from " & strOldParentColl & " ..."
    colitem.delete_
Next

Function ListHelp
    WScript.Echo VbCrLf & "SMS Change Parent Collection" & VbCrLf & VbCrLf & _
        "[Usage  ]   sms_collmove.vbs <old parent> <new parent> <subcollection>" & VbCrLf & VbCrLf &_
        "[Example]   sms_collmove.vbs collroot abc0019a abc0014a" & VbCrLf & _
        "              - Transfers subcollection ABC0014A to new parent" & VbCrLf &_
        "              - ABC0019A from old parent COLLROOT" & VbCrLf & VbCrLf &_
        "[Misc   ]   Use COLLROOT as the old parent if the" & VbCrLf &_
           "        subcollection does not have a parent."
End Function