O R G A N I C / F E R T I L I Z E R: 08.07

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

Aug 17, 2007

sms: selecting objects not in a collection

if you were interested in a way of retrieving objects into a collection that don't exist in another collection, it's actually not very difficult. for example, you have a collection of clients with antivirus.  now you want to create a collection of clients that do not have antivirus.  instead of creating a new one, you run subselect to bring back all the clients that are not in the original antivirus collection.

the only thing you have to know is the collection id of the collection that you want to check. for the samples below, note that [collid] is a generic tag for your collection ids. if you examine the root\sms\site_ of your sms server, you'll see a list of sms collections labeled with:

sms_cm_res_coll_[collid]


this is what you need to build your subselect query. if you query this in wbemtest, with something like select * from sms_cm_res_coll_[collid] you should get back a list of resource ids which look something like this:

... 
sms_cm_res_coll_[collid].resourceid=1 
sms_cm_res_coll_[collid].resourceid=2 
sms_cm_res_coll_[collid].resourceid=3 
sms_cm_res_coll_[collid].resourceid=4 
sms_cm_res_coll_[collid].resourceid=5 
... 


or something similar... i was showing that part just to demonstrate that querying the collection id from wmi in this manner, does indeed produce the members of the collection. now to put it all together, this is how you'd build your subselect query.

select sms_r_system.resourceid, sms_r_system.name 
from sms_r_system 
where resourceid not in 
    (
    select sys.resourceid 
    from sms_cm_res_coll_[collid] AS coll, sms_r_system as sys 
    where sys.resourceid = coll.resourceid
    )


in effect, this would be the same as building a collection that's limited to another collection. the only difference is that when you're using limiting collections, you can't specify to retrieve where a machine doesn't exist in a certain collection.

(btw, you can execute that wql query in wbemtest. works quite nicely. output has a lot to be desired though...)

as a follow on note, kim oppalfens wrote this little post recently on figuring out the collectionid for linked collections.

Aug 3, 2007

mom: icmp pings for servers...

most of you are probably familiar with the icmpping script available from huntland services. one of the admins i work with wanted something that would do the same type of functionality but work for a list of servers. i took it a tiny step further and had it read from an ou instead of a text file. you'll want to modify this part to reflect your environment:
oCommand.CommandText = ";" & _ "(&(objectClass=Computer)(objectCategory=Computer));name"
sSiteName is the variable that you can specify in your parameters location. you could even put the path directly into the script and bypass all that. anyway, here's the script! i tried to put it in this post... but you know how those translations go. :) have fun!