[download id=”87″]

Working with System Center Configurations Manager 2012, I had to list the ID of a folder to use for our Pre-stage HTA Solution.

so I thought a PowerShell function for listen this folder info and collections would be a nice tool to have.

So I ended up writing a complete PS function that includes help and pipeline binding.

To use the script start by loading the function into memory but running the script (dot sourced).

Navigate to the folder where the script is located

. .\Get-CollectionsInDeviceFolder.ps1

 

To Get Help for the function use the builtin help:

Get-Help Get-CollectionsInDeviceFolder -Full

 

 

Function Get-CollectionsInFolder
{
	# .SYNOPSIS
	#        A function for listing collections inside af configmgr 2012 device folder
	#		This function defaults to Device Collections! use FolderType parameter to switch to user collections
	#
	# .PARAMETER  siteServer
	#		NETBIOS or FQDN address for the configurations manager 2012 site server
	#
	# .PARAMETER  siteCide
	#		Site Code for the configurations manager 2012 site server
	#
	# .PARAMETER  FolderName
	#		Folder name(s) of the folder(s) to list
	#
	# .PARAMETER  FolderType
	#		Device or User Collection (Valid Inputs: Device, User)
	#    .EXAMPLE
	#       Get-CollectionsInFolder -siteServer "CTCM01" -siteCode "PS1" -folderName "Coretech"
	#       Listing all collections inside Coretech Folder on CTCM01
	#
	#    .EXAMPLE
	#       Get-CollectionsInFolder -siteServer "CTCM01" -siteCode "PS1" -folderName "Coretech","HTA-Test"
	#       Listing all collections inside multiple folders
	#
	#    .EXAMPLE
	#       "HTA-Test", "Coretech" | Get-CollectionsInFolder -siteServer "CTCM01" -siteCode "PS1"
	#       Listing all collections inside multiple folders using pipe
	#
	#    .EXAMPLE
	#       Get-CollectionsInFolder -siteServer "CTCM01" -siteCode "PS1"  -FolderName "CCO" -FolderType "User"
	#       Listing all collections inside a user collection folder
	#
	#    .INPUTS
	#      Accepts a collection of strings that contain folder name, and each folder will be processed
	#
	#    .OUTPUTS
	#        Custom Object (Properties: CollectionName, CollectionID)
	#
	#    .NOTES
	#        Developed by Jakob Gottlieb Svendsen - Coretech A/S
	#        Version 1.0
	#
	#    .LINK
	#        https://blog.ctglobalservices.com
	#        https://blog.ctglobalservices.com/jgs

    [CmdletBinding(SupportsShouldProcess=$True,
                         ConfirmImpact="Low")]
    param(
    [parameter(Mandatory=$true, HelpMessage=”System Center Configuration Manager 2012 Site Server - Server Name”,ValueFromPipelineByPropertyName=$true)]
    $siteServer = "",

    [parameter(Mandatory=$true, HelpMessage=”System Center Configuration Manager 2012 Site Server - Site Code”,ValueFromPipelineByPropertyName=$true)]
    [ValidatePattern("\w{3}")]
    [String] $siteCode = "",

    [parameter(Mandatory=$true, HelpMessage=”System Center Configuration Manager 2012 Site Server - Folder Name”,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
    [String[]]
    $folderName = "",

	[parameter(Mandatory=$false)]
    [String]
    [ValidateRange("Device","User")]
	$FolderType = "Device"

    )

    Begin{
			Switch ($FolderType)
			{
			"Device" { $ObjectType = "5000" }
			"User" { $ObjectType = "5001" }
			}
	}
    Process
    {
           foreach ($folderN in $folderName)
           {
            $folder = get-wmiobject -ComputerName $siteServer -Namespace root\sms\site_$siteCode  -class SMS_ObjectContainernode -filter "ObjectType = $ObjectType AND NAme = '$folderN'"

			if ($folder -ne $null)
			{
	            "Folder: {0} ({1})" -f $folder.Name, $folder.ContainerNodeID | out-host

	            get-wmiobject -ComputerName $siteServer -Namespace root\sms\site_$siteCode  -class SMS_ObjectContainerItem -filter "ContainerNodeID = $($folder.ContainerNodeID)" |
	            select @{Label="CollectionName";Expression={(get-wmiobject -ComputerName $siteServer -Namespace root\sms\site_$siteCode  -class SMS_Collection -filter "CollectionID = '$($_.InstanceKey)'").Name}},@{Label="CollectionID";Expression={$_.InstanceKey}}
			}
			else
			{
				Write-Host "$FolderType Folder Name: $folderName not found"
			}
           }
    }
    End{}
 }   

#Examples
#Get-CollectionsInFolder -siteServer "CTCM01" -siteCode "PS1" -folderName "CCO" -FolderType User
#Get-CollectionsInFolder -siteServer "CTCM01" -siteCode "PS1" -folderName "Coretech","HTA-Test"
#"HTA-Test", "Coretech" | Get-CollectionsInFolder -siteServer "CTCM01" -siteCode "PS1"