透過 PowerShell 擷取 SCCM 集合成員資格

透過 PowerShell 擷取 SCCM 集合成員資格

我想找到一個 PowerShell 腳本來檢索給定電腦或使用者的 SCCM 集合。我知道這可以透過 SCCM 查詢來實現,但我想使用 PowerShell 函數來實現。

該腳本應適用於 SCCM 2007 和 SCCM 2012。

答案1

以下是執行此操作的 PowerShell 函數:

$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
}

只需根據您的環境調整 $Server 和 $Site 變數的值即可。

以下是如何使用此功能的範例:

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

結果將是一個表格,顯示與電腦或使用者關聯的集合 ID、集合名稱和註釋。

希望這可以幫助!

答案2

我知道這個問題已經得到解答,但您應該查看由 Microsoft 的 Russ Slaten 創建的 Powershell 腳本。他在 Powershell 中建立了一個 GUI,可用於: 取得直接成員 新增直接成員 刪除使用者和裝置集合中的直接成員。

您也可以在新增成員時建立新的使用者集合。

我使用它已經快一年了,它從來沒有讓我失望過。

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

相關內容