Get SCCM COllection Members

I got a task to create a script so other team could use it to retrieve the list of machines from specific collection in SCCM.

<#
.Synopsis
   This script query specific sccm collection and list all its members


.DESCRIPTION
   This powershell script is designed to get list of all devices from a specific collection in SCCM, and it saves the output to a text file in c:\device.txt
   Path:
         by default it saves the output to "c:\device.txt". you can change the path to your specific location, just change the $path parameter value
   Collection:
        The default collection is "workstations" 
        To use specific Collection, first you need to find the Collection ID from sccm console like "SF1003", then change the value of paramater in script  "[string]$SCCMCollectionID = 'SF1003' "


.EXAMPLE
	You can run the script first so it could be saved in current session memory, then call the function using dot source as:
    . .\Get_Sccm_Collection_members_PS.ps1  
  	Get-CollectionMembership

.EXAMPLE
   You can call the script directly
  .\Get_Sccm_Collection_members_PS.ps1  

.EXAMPLE
   To override the default paramters value with different info, use this 
   .\Get_Sccm_Collection_members_PS.ps1  -siteCode 'sitecode' -siteserver 'servername' -collectionID 'collection id' -path 'c:\device.txt" 
   

.EXAMPLE
   To override the default paramters value with different info, use this 
    . .\Get_Sccm_Collection_members_PS.ps1  
    Get-CollectionMembership-siteCode 'sitecode' -siteserver 'servername' -collectionID 'collection id' -path 'c:\device.txt" 

.NOTES  
 

.LINK  
    File Name  : Get_Sccm_Collection_members_PS.ps1 
    Author     : hashmat mohammadi
    Requires   : PowerShell V5

#>
function Get-SccmCollectionMembership
{
    [CmdletBinding()]
    
    Param
    (
        
        
         #[Parameter(Mandatory)]
        [string] $SiteCode = 'SF1',

        #[Parameter(Mandatory)]
       [string] $SiteServer= 'sccm.hashmat.local',

        #[Parameter(Mandatory)]
        [string]$SCCMCollectionID = 'SF1003',

        #[Parameter][strin
       [string]$path = 'c:\device.txt'

    )

    Begin
    {
        $CollectionMembers = Get-WmiObject -Namespace "root\SMS\site_$($SiteCode)" -Query "SELECT * FROM SMS_FullCollectionMembership WHERE CollectionID='$($SCCMCollectionID)' order by name" -ComputerName $SiteServer
        
    }
    Process
    {
        $CollectionMembers.name
        $CollectionMembers.name | Out-File $path | notepad $path

    }
    End
    {
    }
}
Exit mobile version