Search code examples
powershellwmisccm

PowerShell Move collection to another folder in SCCM?


I have a script that creates collections in SCCM but I need to have it created in one of the sub folders under "Device Collections". I cant see to figure out how to move the collection using the WMI class. Since this script does not run on the SCCM server itself I cant use configmanager module for Move-CMobject. Is there a way to implement a collection move using the PowerShell code style below?

$CMCollection = ([WMIClass]”\root\sms\site_:SMS_Collection”).CreateInstance()
$CMCollection.name = $CollectionName
$CMCollection.LimitToCollectionID = “12345678”
$CMCollection.RefreshType = 2
$CMCollection.Put()

Solution

  • You need the method MoveMembers of the class SMS_ObjectContainerItem. After you created the collection (It is not possible to create in the correct path afaik).

    The powershell code would be something like this:

    [Array]$DeviceCollectionID = <CollID>
    $TargetFolderID = <ContainerNodeID>
    $CurrentFolderID = 0
    $ObjectTypeID = 5000
    
    Invoke-WmiMethod -Namespace 'Root\SMS\Site_<SiteCode>' -Class 'SMS_objectContainerItem' -Name 'MoveMembers' -ArgumentList $CurrentFolderID,$DeviceCollectionID,$ObjectTypeID,$TargetFolderID
    

    To get the ContainerNodeID of your Target folder you can use a query like this:

    select * from sms_objectcontainernode where objecttypeName = 'sms_collection_device' and Name = <your target folder name>
    

    I found no valid ms source for the ObjectIDTypes but from my own programs and several samples I know 5000 is for collection folders. The CurrentFolderID is always 0 if you just created something programatically because it will be in root. For an existing folder you can figure it out the same way as your target.

    There is also this list from some code sample I found which has no source but is probably correct.

    Object type 2 - Package Folder
    Object type 7 - Query Folder
    Object type 9 - Software Metering Folder
    Object type 14 - Operating System Installers Folder
    Object type 17 - State Migration GFolder
    Object type 18 - Image Package Folder
    Object type 19 - Boot Image Folder
    Object type 20 - Task Sequence Folder
    Object type 23 - Driver Package Folder
    Object type 25 - Driver Folder
    Object type 2011 - Configuration Baseline Folder
    Object type 5000 - Device Collection Folder
    Object type 5001 - User Collection Folder
    Object type 6000 - Application Folder
    Object type 6001 - Configuration Item Folder
    

    If you need some type that is missing you can also look at the sms_objectcontainernode instances and just check what your given folders have as type