none
Cmdlet to find collection membership

    Question

  • Hi,

    For one computer, which cmdlet would list all computers?

    1. Computer is member of collection A,B,C,...

    2. If a computer is member of a collection by a query, is it possible to retrieve collections only by direct membership?

    Thanks,

    Friday, February 9, 2018 10:06 PM

Answers

  • Hi,

    For one computer, which cmdlet would list all computers?

    1. Computer is member of collection A,B,C,...

    2. If a computer is member of a collection by a query, is it possible to retrieve collections only by direct membership?

    Thanks,

    Try copying and saving the code below, I think it will get you started toward what you are after. Save it as "Get-DeviceCollectionMembership.ps1"

    The code will search collections for the specified computername and tell you the collection name and whether it is "direct" or "query" based membership for that computer.

    #Usage:
    #      .\Get-DeviceCollectionMembership.ps1 -CompName Laptop01 -SiteCode PR1 -CMProviderFQDN CM01.contoso.com
    
    Param(
        [Parameter(Mandatory=$True)][String]$CompName,
        [Parameter(Mandatory=$True)][String]$SiteCode,
        [Parameter(Mandatory=$True)][String]$CMProviderFQDN
    )
    
    $ProviderMachineName = "$($CMProviderFQDN)" # SMS Provider machine name
    
    $initParams = @{}
    # Import the ConfigurationManager.psd1 module 
    if((Get-Module ConfigurationManager) -eq $null) {
        Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1" @initParams 
    }
    
    # Connect to the site's drive if it is not already present
    if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {
        New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName @initParams
    }
    
    # Set the current location to be the site code.
    Set-Location "$($SiteCode):\" @initParams
    
    $Collections = (Get-CMCollection | ? {($_.CollectionType -eq "2") -and ($_.CollectionID -notlike "*SMS*")}).Name
    Write-Host "'$($CompName)' is a member of the following Collections"
    Foreach ($Collection in $Collections){
        $Members = (Get-CMCollectionMember -CollectionName $Collection)
        Foreach ($Member in $Members){
            $MemName = $Member.Name
            $MemType = $Member.IsDirect
            If ($MemName -eq $CompName){
                Write-Host "   $($Collection)" -ForegroundColor Green
                Switch($MemType){
                    $True{ Write-Host "      Direct Membership"
                        }
                    $False{ Write-Host "      Query Membership"
                        }
                }
            }
        }      
    }

    • Edited by Phil Pritchett Saturday, February 10, 2018 2:24 AM Error in code
    • Proposed as answer by Phil Pritchett Tuesday, February 13, 2018 8:30 PM
    • Marked as answer by FRacine Wednesday, February 14, 2018 11:04 AM
    Saturday, February 10, 2018 2:16 AM

All replies

  • Have you installed right-click Tools? When using this you can right-click on a device and get it to list all the collections its a memeber of?

    https://recastsoftware.com/ - free version is right at the bottom of the page.

    Friday, February 9, 2018 11:07 PM
  • Hi,

    It is interesting but what I want is running a PowerShell script and during that run extract all collections name containing that computername.

    Thanks,

    Saturday, February 10, 2018 12:47 AM
  • Hi,

    For one computer, which cmdlet would list all computers?

    1. Computer is member of collection A,B,C,...

    2. If a computer is member of a collection by a query, is it possible to retrieve collections only by direct membership?

    Thanks,

    Try copying and saving the code below, I think it will get you started toward what you are after. Save it as "Get-DeviceCollectionMembership.ps1"

    The code will search collections for the specified computername and tell you the collection name and whether it is "direct" or "query" based membership for that computer.

    #Usage:
    #      .\Get-DeviceCollectionMembership.ps1 -CompName Laptop01 -SiteCode PR1 -CMProviderFQDN CM01.contoso.com
    
    Param(
        [Parameter(Mandatory=$True)][String]$CompName,
        [Parameter(Mandatory=$True)][String]$SiteCode,
        [Parameter(Mandatory=$True)][String]$CMProviderFQDN
    )
    
    $ProviderMachineName = "$($CMProviderFQDN)" # SMS Provider machine name
    
    $initParams = @{}
    # Import the ConfigurationManager.psd1 module 
    if((Get-Module ConfigurationManager) -eq $null) {
        Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1" @initParams 
    }
    
    # Connect to the site's drive if it is not already present
    if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {
        New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName @initParams
    }
    
    # Set the current location to be the site code.
    Set-Location "$($SiteCode):\" @initParams
    
    $Collections = (Get-CMCollection | ? {($_.CollectionType -eq "2") -and ($_.CollectionID -notlike "*SMS*")}).Name
    Write-Host "'$($CompName)' is a member of the following Collections"
    Foreach ($Collection in $Collections){
        $Members = (Get-CMCollectionMember -CollectionName $Collection)
        Foreach ($Member in $Members){
            $MemName = $Member.Name
            $MemType = $Member.IsDirect
            If ($MemName -eq $CompName){
                Write-Host "   $($Collection)" -ForegroundColor Green
                Switch($MemType){
                    $True{ Write-Host "      Direct Membership"
                        }
                    $False{ Write-Host "      Query Membership"
                        }
                }
            }
        }      
    }

    • Edited by Phil Pritchett Saturday, February 10, 2018 2:24 AM Error in code
    • Proposed as answer by Phil Pritchett Tuesday, February 13, 2018 8:30 PM
    • Marked as answer by FRacine Wednesday, February 14, 2018 11:04 AM
    Saturday, February 10, 2018 2:16 AM
  • Hi,

    I will give it a try. Thaks!

    François

    Saturday, February 10, 2018 12:03 PM
  • If my answer works, please mark it. If not, let me know what your results were. I may be able to assist further.
    Tuesday, February 13, 2018 8:29 PM