
特定のコンピューターまたはユーザーの 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 を作成しました: 直接メンバーの取得、直接メンバーの追加、ユーザー コレクションとデバイス コレクションでの直接メンバーの削除。
新しいメンバーを追加するときに、新しいユーザー コレクションを作成することもできます。
もうほぼ 1 年間使用していますが、一度も故障したことはありません。