On several occasions I have had the need to pull information on SCCM devices, and recently I was asked to do a backup of all client direct memberships and some specific machine variables.

So I thought I would share my latest version of a script that does that.

The script takes an argument that will allow you to limit the process to only a set of named clients or just one if you prefer.


The script starts by create a class to contain the information pulled from SCCM, making it easier to work with afterwards.

Add-Type -Language CSharpVersion3 @" public class SCCMResource { public string Name; public int ResourceID; public object[] Collections; public object[] Variables; } "@;

Next the script will pull the list of client to process

if ($clients) { foreach ($client in $clients) { $resources += Get-WmiObject -Class SMS_R_System -Filter "Name = '$client'" -Namespace $namespace } } else { $resources = Get-WmiObject -Class SMS_R_System -Namespace $namespace }

Now we get to the good bits, a loop will run through the list of clients and build an object of type SCCMResource

foreach ($res in $resources) { $r = New-Object SCCMResource $r.Name = $res.Name $r.ResourceID = $res.ResourceId

Lets get the machine variables for the resource and add the result to the object

$vars = Get-WmiObject -Class SMS_MachineSettings -Filter "ResourceID = $($res.ResourceID)" -Namespace $namespace if ($vars) { $vars.Get() $r.Variables = $vars.MachineVariables | select Name, Value }

And then the direct memberships using a very loooong WMI query

$r.Collections = Get-WmiObject -query "SELECT C.* FROM SMS_FullCollectionMembership FCM INNER JOIN SMS_Collection C On FCM.CollectionID = C.CollectionID WHERE (FCM.ResourceID = $($res.ResourceID)) AND (FCM.IsDirect = 1)" -Namespace $namespace | Select CollectionID, Name

Finally stuff the object into the result array

$result += $r

Now we have an array containing objects with information on the client, and we can use it for all sorts of funny stuff, if you pipe $result through Out-GridView you get something like this.


You can download the full script from this link

And finally I’d like to wish you a happy christmas and maybe this little gem will you into the right mood for the holidays.