0

I'd like to find a PowerShell script that retrieve the SCCM collections for a given computer or user. I know this can be achieve via a SCCM query, but I'd like to do this using a PowerShell function.

The script should work with SCCM 2007 and SCCM 2012.

2

Here is the PowerShell function to do that :

$Server = "sccm-01"
$site = "S01"

Function Get-Collections 
{
    <# 
            .SYNOPSIS 
                Determine the SCCM collection membership    
            .DESCRIPTION
                This function allows you to determine the SCCM collection membership of a given user/computer
            .PARAMETER  Type 
                Specify the type of member you are querying. Possible values : 'User' or 'Computer'
            .PARAMETER  ResourceName 
                Specify the name of your member : username or computername
            .EXAMPLE 
                Get-Collections -Type computer -ResourceName PC001
                Get-Collections -Type user -ResourceName User01
            .Notes 
                Author : Antoine DELRUE 
                WebSite: http://obilan.be 
    #> 

    param(
    [Parameter(Mandatory=$true,Position=1)]
    [ValidateSet("User", "Computer")]
    [string]$type,

    [Parameter(Mandatory=$true,Position=2)]
    [string]$resourceName
    ) #end param

    Switch ($type)
        {
            User {
                Try {
                    $ErrorActionPreference = 'Stop'
                    $resource = Get-WmiObject -ComputerName $server -Namespace "root\sms\site_$site" -Class "SMS_R_User" | ? {$_.Name -ilike "*$resourceName*"}                            
                }
                catch {
                    Write-Warning ('Failed to access "{0}" : {1}' -f $server, $_.Exception.Message)
                }

            }

            Computer {
                Try {
                    $ErrorActionPreference = 'Stop'
                    $resource = Get-WmiObject -ComputerName $server -Namespace "root\sms\site_$site" -Class "SMS_R_System" | ? {$_.Name -ilike "$resourceName"}                           
                }
                catch {
                    Write-Warning ('Failed to access "{0}" : {1}' -f $server, $_.Exception.Message)
                }
            }
        }

    $ids = (Get-WmiObject -ComputerName $server -Namespace "root\sms\site_$site" -Class SMS_CollectionMember_a -filter "ResourceID=`"$($Resource.ResourceId)`"").collectionID
    # A little trick to make the function work with SCCM 2012
    if ($ids -eq $null)
    {
            $ids = (Get-WmiObject -ComputerName $server -Namespace "root\sms\site_$site" -Class SMS_FullCollectionMembership -filter "ResourceID=`"$($Resource.ResourceId)`"").collectionID
    }

    $array = @()

    foreach ($id in $ids)
    {
        $Collection = get-WMIObject -ComputerName $server -namespace "root\sms\site_$site" -class sms_collection -Filter "collectionid=`"$($id)`""
        $Object = New-Object PSObject
        $Object | Add-Member -MemberType NoteProperty -Name "Collection Name" -Value $Collection.Name
        $Object | Add-Member -MemberType NoteProperty -Name "Collection ID" -Value $id
        $Object | Add-Member -MemberType NoteProperty -Name "Comment" -Value $Collection.Comment
        $array += $Object
    }

    $array
}

Simply adapt the value of the $Server and $Site variables according to your environment.

Here are examples of how to use this function :

Get-Collections -Type computer -ResourceName PC001
Get-Collections -Type user -ResourceName User01

The result will be a table showing the Collection ID, Collection Name and Comment associated with the computer or the user.

Hope this helps!

0

I know this is already answered, but you should check out this Powershell script created by Russ Slaten from Microsoft. He created a GUI in Powershell which you can use to: Get Direct members Add Direct members Remove Direct members in user and device collections.

You can also create a new user collection when adding new members.

I use it for almost a year now and it never failed me.

http://blogs.msdn.com/b/rslaten/archive/2014/03/10/configuration-manager-direct-membership-collection-manager.aspx

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.