
특정 컴퓨터나 사용자에 대한 SCCM 컬렉션을 검색하는 PowerShell 스크립트를 찾고 싶습니다. 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를 만들었습니다. Direct 구성원 가져오기 Direct 구성원 추가 사용자 및 장치 컬렉션에서 Direct 구성원을 제거합니다.
새 구성원을 추가할 때 새 사용자 컬렉션을 생성할 수도 있습니다.
나는 지금 거의 1년 동안 그것을 사용하고 있으며 결코 실패하지 않았습니다.