Show all SCCM collection members

This function is just a standard function to gather the collection members for the specified collection and return this in the output. The output can be used for further functionality or just to show which and how many members there are within the collection. The Full parameter just selects all fields from the Site Server whereas not providing this only returns the member name.

NOTE: By including the SYNOPSIS and other information you can use get-help on the function to find out what parameters you can specify or read what this function actually can be used for. I can highly recommended to always include this information in your PowerShell function(s) so that anybody can understand it.

Examples:
In the below examples names pointing to a sitecode, collection ID or server name are blurred out (this is a real site server behind a FAKE FQDN). The below examples show screenshots with output of just the standard query against the Full query.

Get-SCCMCollectionMember without any further parameters
Get-SCCMCollectionMember with the full parameter

Code:

Function Get-SCCMCollectionMember
{
  <#
    .SYNOPSIS
    Shows all members (clients) below the specified collection using the SCCM Collection ID.

    .EXAMPLE
    Get-SCCMCollectionMember -SCCMCollectionID <sccm collection ID>
    
    .PARAMETER SCCMCollectionID
    This parameter is the CollectionID for which you want to show all members
    .PARAMETER SiteServer
    This parameter defines the site server to connect to and is optional
    .PARAMETER SiteCode
    This parameter is optional and by default uses WMI to collect this from the Site Server
    .PARAMETER Full
    This switch parameter is optional and if provided will grab all fields from the Site Server instead of just the name.
  #>
  Param
  (
    [Parameter(Mandatory=$True,ValueFromPipeline=$true)]
    [string]$SCCMCollectionID,
    [Parameter(Mandatory=$False)]
    [string]$SiteServer = "mysiteserver.example.com",
    [Parameter(Mandatory=$False)]
    [string]$SiteCode = (Get-WmiObject -Namespace "root\SMS" -Class SMS_ProviderLocation -ComputerName $SiteServer).SiteCode,
    [Parameter(Mandatory=$False)]
    [switch]$Full = $false
  )
  Begin
  {
    Write-Verbose "SCCM Site Server                   : $($SiteServer)"
    Write-Verbose "SCCM Site code                     : $($SiteCode)"
    Write-Verbose "SCCM Collection ID                 : $($SCCMCollectionID)"
  }
  Process
  {
    If ($Full)
    {
      $CollectionMembers = Get-WmiObject -Namespace "root\SMS\site_$($SiteCode)" -Query "SELECT * FROM SMS_FullCollectionMembership WHERE CollectionID='$($SCCMCollectionID)' order by name" -ComputerName $SiteServer
    }
    Else
    {
      $CollectionMembers = Get-WmiObject -Namespace "root\SMS\site_$($SiteCode)" -Query "SELECT name FROM SMS_FullCollectionMembership WHERE CollectionID='$($SCCMCollectionID)' order by name" -ComputerName $SiteServer
    }
    If ($VerbosePreference -eq "continue")
    {
      ForEach ($member in $CollectionMembers)
      {
        Write-verbose $member.Name
      }
    }
  }
  End
  {
    return $CollectionMembers
  }
}

Posted in PowerShell, System Center Configuration Manager, WMI.

Leave a Reply

Your email address will not be published.