create a new sccm folder, collection

Preliminary posting of some cool code

Function Get-FolderCollectionNodeID {
# Given a folder name, return the folder NodeID
Param( [String]$FolderName,
       [string]$SCCMSiteCode,
       [string]$Servername)
    (Get-WmiObject -Namespace "Root\SMS\Site_$GlobalSCCMSiteCode" -Class SMS_ObjectContainerNode -ComputerName $Servername -Filter "Name='$ParentFolderName'" -ErrorAction SilentlyContinue).ContainerNodeID

}


Function New-CMDeviceCollectionFolder { #----------------------------------------------------------
# Given a new folder name and parent folder name, create a new folder within the parent
Param( [String]$NewFolderName,
       [String]$ParentFolderName,
       [string]$SCCMSiteCode,
       [string]$Servername)

    if (Get-FolderContainerNodeID -FolderName $NewFolderName -SCCMSiteCode $SCCMSiteCode -Servername $Servername) {
        Write-Verbose -verbose "Folder exists: [$NewFolderName]"
    } else {
        $ParentFolderCollectionnodeID = Get-FolderContainerNodeID -FolderName $ParentFolderName -SCCMSiteCode $SCCMSiteCode -Servername $Servername
	    $CollectionFolderArgs = @{
	    Name = $NewFolderName;
	    ObjectType = "5000"; 		
	    ParentContainerNodeid = $ParentFolderCollectionnodeID
	    }
    
        $FolderInstance = ([wmiclass]("\\$Servername\ROOT\sms\site_" + $SCCMsiteCode + ":SMS_ObjectContainerNode")).CreateInstance()
	    if ($FolderInstance) {
	    	$FolderInstance.Name = $NewFolderName
            $FolderInstance.ObjectType = "5000"
	    	$FolderInstance.ParentContainerNodeID = $ParentFolderCollectionnodeID
            try {
	    	    $FolderInstance.Put() > $null
                Get-FolderContainerNodeID -FolderName $NewFolderName -SCCMSiteCode $SCCMSiteCode -Servername $Servername
                Write-Verbose -verbose "Folder created: [$NewFolderName]"
                Return $true
            } catch {
                Write-Verbose -verbose "Folder was not created: [$NewFolderName]"
                Return $false
            }
        }
    }    
} # New-CMDeviceCollectionFolder
New-CMDeviceCollectionFolder -NewFolderName 'RC-TestContainer' -ParentFolderName "Pending Reboot" -SCCMSiteCode 'ABC' -Servername 'SRV1' 

#Example - create new collection and move to folder
$NewCollection = New-CMDeviceCollection -Name "Test-Collection" -LimitingCollectionName "All desktops with SCCM client"
Move-CMObject -FolderPath "FA0:\DeviceCollection\_personal\MyName\pending reboot\testcontainer" -InputObject $NewCollection


Leave a comment