通过PowerShell检索SCCM集合成员身份

我想find一个PowerShell脚本来检索给定的计算机或用户的SCCM集合。 我知道这可以通过SCCM查询来实现,但是我想使用PowerShell函数来实现。

该脚本应与SCCM 2007和SCCM 2012一起使用。

这里是PowerShellfunction来做到这一点:

$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和$ Sitevariables的值即可。

以下是如何使用此function的示例:

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

结果将显示与计算机或用户关联的收集ID,收集名称和评论。

希望这可以帮助!

一个小gem..谢谢你..

我改变了一点,但把站点/服务器variables下的参数,以便我可以把它放在我们自己的模块。

  param( [Parameter(Mandatory=$true,Position=1)] [ValidateSet("User", "Computer")] [string]$type, [Parameter(Mandatory=$true,Position=2)] [string]$resourceName, [Parameter(Mandatory=$false,Position=3)] [string]$Server = "sccm-01", [Parameter(Mandatory=$false,Position=4)] [string]$site = "S01" ) #end param