PowerShell – Moving Collections

It’s been a while since I’ve posted anything and I figured I would share a bit of what I’ve done over the past few days.  I work in an environment where there are over 100K clients.  When you’re trying to deal with moving collections around in the console and each time you click on a new collection with 10s of thousands of clients in each, it takes a long time to enumerate the collection list just so you can right-click, then “link” to collection.  Once you get there, you still have to wait for the entire collection tree to enumerate again so that you can select the parent collection.  You have to then go back and delete the collection link you had before to actually complete the “move”.

I made a script that gathers all of the parent collections a particular collectionID may be a child of, creates a new parent/child collection relationship in the SMS_CollectToSubcollect class and then, if the bit is set, remove all previous parent/child relationships so that you’ve left the collection a child of only one collection.

The script accepts the following parameters:

ParentColl = This is where you’re moving the collection to.

Collections = You can specify multiple collections in an array, like so:  -Collections (‘abc12345’,’abc12346’,’abc12347’)

CreateLinkOnly = specify as $True if you’d like to only create the collection link and not remove any other collection links.

 

Here’s the code:

 

param($ParentColl,$Collections,$CreateLinkOnly)

Function movecollection
{
param($subCollID, $parentCollID)
$newSubCollectToSubCollect = ([WmiClass](“\\$script:siteserver\root\sms\site_”+$script:sitecode+”:SMS_CollectToSubCollect”)).CreateInstance()
$newSubCollectToSubCollect.parentCollectionID = $parentCollID
$newSubCollectToSubCollect.subCollectionID = $subCollID
$newSubCollectToSubCollect.Put()
$newSubCollectToSubCollect.Get()
write-host “CollectionID $subcollID moved to parent collection $parentcollid”
}
Function GetParentCollections
{
param($subcollectionid)
$ParentCollectionList = Get-WmiObject -ComputerName $script:siteserver -Namespace “root\sms\site_$($script:sitecode)” -query “Select * from sms_collecttosubcollect where subcollectionid = ‘$($subcollectionid)’”
return $ParentCollectionList
}

foreach($col in $collections)
{
$ParentCollections = GetParentCollections $Col
MoveCollection $col $script:parentcoll
if ($CreateLinkOnly -eq $True)
{
#Do Nothing – We moved the collections – yay!
}
else
{
foreach ($Parent in $ParentCollections)
{
$parent.delete()
}
}
}

If you have any questions, leave me a comment.

email

Written by , Posted .