Where is a collection included in

It’s time for my first public blog post. What should it be about ?? It will be about collections in ConfigMgr 2012. I got a question from a customer: How can I find out which other collections this collection is used in ? It’s very easy to see if there is collections included by looking in the Referenced Collections. Open every collection to see the name is time consuming. A script is needed :)

IncludedCollections

After some research I find this blog. http://blog.coretech.dk/kaj/csv-report-get-configmgr-collection-excluded-or-included-collection-rules/

But I wanted to have it a little bit different. PowerShell is not my strongest skill, but I have a college (Simon Wåhlin) who is VERY skilled. He gladly helped me and the result is the following:

[CmdLetBinding()]
PARAM(
[Parameter(Mandatory=$False,HelpMessage='Please Enter Site Server Site code')]
$SiteCode = 'PS1',
[Parameter(Mandatory=$False,HelpMessage='Please Enter Site Server name')]
$SiteServer = 'CM01',
[Parameter(Mandatory=$False,HelpMessage='Please Enter CSV File location')]
$OutPut,
[Parameter(Mandatory=$True,HelpMessage='Please Enter CollectionID')]
$CollectionID
)

$CollectionsQuery = Get-WmiObject -namespace "root\SMS\Site_$($SiteCode)" -class SMS_Collection -ComputerName $SiteServer |
ForEach-Object {[WMI]($_.__PATH)}

$Result = foreach($item in $CollectionsQuery)
{
foreach($Rule in $item.CollectionRules)
{
if($CollectionID -match '\S')
{
$Expression = '$Rule.__CLASS -eq "SMS_CollectionRuleIncludeCollection" -and $Rule.IncludeCollectionID -Like $CollectionID'
}
else
{
$Expression = '$Rule.__CLASS -eq "SMS_CollectionRuleIncludeCollection"'
}
if(Invoke-Expression -Command $Expression)
{
$DObject = New-Object PSObject
$DObject | Add-Member -MemberType NoteProperty -Name 'CollectionName' -Value $item.Name
$DObject | Add-Member -MemberType NoteProperty -Name 'CollectionID' -Value $item.CollectionID
$DObject
}
}
}
if($OutPut -match '\S')
{
$Result | Export-Csv $OutPut –NoTypeInformation -Delimiter ','
Write-Host "File exported to: $Output" -BackgroundColor Black -ForegroundColor Yellow
}
else
{
$RuleName = $CollectionsQuery.CollectionRules | Where {$_.IncludeCollectionID -Like $CollectionID} | Select -ExpandProperty RuleName -Unique
Write-Host
Write-Host "The CollectionID $CollectionID with name $RuleName is included in the following collections:" -BackgroundColor Black -ForegroundColor Yellow

$Result
Pause
}

Here is a video recording from running the script:

Have a nice day Johnny