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.
1 2 3 4 5 6 7 |
<div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #0000FF;">Param</span><span style="color: #000000;">(</span><span style="color: #800080;">$clients</span><span style="color: #000000;">) </span></div> |
The script starts by create a class to contain the information pulled from SCCM, making it easier to work with afterwards.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #000000;">Add</span><span style="color: #000000;">-</span><span style="color: #000000;">Type </span><span style="color: #000000;">-</span><span style="color: #000000;">Language CSharpVersion3 </span><span style="color: #000000;">@</span><span style="color: #800000;">"</span><span style="color: #800000;"> public class SCCMResource { public string Name; public int ResourceID; public object[] Collections; public object[] Variables; } </span><span style="color: #800000;">"</span><span style="color: #000000;">@</span><span style="color: #000000;">;</span></div> |
Next the script will pull the list of client to process
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #800080;">$clients</span><span style="color: #000000;">) { </span><span style="color: #0000FF;">foreach</span><span style="color: #000000;"> (</span><span style="color: #800080;">$client</span><span style="color: #000000;"> </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> </span><span style="color: #800080;">$clients</span><span style="color: #000000;">) { </span><span style="color: #800080;">$resources</span><span style="color: #000000;"> </span><span style="color: #000000;">+=</span><span style="color: #000000;"> Get</span><span style="color: #000000;">-</span><span style="color: #000000;">WmiObject </span><span style="color: #000000;">-</span><span style="color: #000000;">Class SMS_R_System </span><span style="color: #000000;">-</span><span style="color: #0000FF;">Filter</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">Name = '$client'</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">-</span><span style="color: #000000;">Namespace </span><span style="color: #800080;">$namespace</span><span style="color: #000000;"> } } </span><span style="color: #0000FF;">else</span><span style="color: #000000;"> { </span><span style="color: #800080;">$resources</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> Get</span><span style="color: #000000;">-</span><span style="color: #000000;">WmiObject </span><span style="color: #000000;">-</span><span style="color: #000000;">Class SMS_R_System </span><span style="color: #000000;">-</span><span style="color: #000000;">Namespace </span><span style="color: #800080;">$namespace</span><span style="color: #000000;"> } </span></div> |
Now we get to the good bits, a loop will run through the list of clients and build an object of type SCCMResource
1 2 3 4 5 6 7 8 9 10 |
<div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #0000FF;">foreach</span><span style="color: #000000;"> (</span><span style="color: #800080;">$res</span><span style="color: #000000;"> </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> </span><span style="color: #800080;">$resources</span><span style="color: #000000;">) { </span><span style="color: #800080;">$r</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> New</span><span style="color: #000000;">-</span><span style="color: #000000;">Object SCCMResource </span><span style="color: #800080;">$r</span><span style="color: #000000;">.Name </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$res</span><span style="color: #000000;">.Name </span><span style="color: #800080;">$r</span><span style="color: #000000;">.ResourceID </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$res</span><span style="color: #000000;">.ResourceId </span></div> |
Lets get the machine variables for the resource and add the result to the object
1 2 3 4 5 6 7 8 9 10 11 12 |
<div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #000000;"> </span><span style="color: #800080;">$vars</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> Get</span><span style="color: #000000;">-</span><span style="color: #000000;">WmiObject </span><span style="color: #000000;">-</span><span style="color: #000000;">Class SMS_MachineSettings </span><span style="color: #000000;">-</span><span style="color: #0000FF;">Filter</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">ResourceID = $($res.ResourceID)</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">-</span><span style="color: #000000;">Namespace </span><span style="color: #800080;">$namespace</span><span style="color: #000000;"> </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #800080;">$vars</span><span style="color: #000000;">) { </span><span style="color: #800080;">$vars</span><span style="color: #000000;">.Get() </span><span style="color: #800080;">$r</span><span style="color: #000000;">.Variables </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$vars</span><span style="color: #000000;">.MachineVariables </span><span style="color: #000000;">|</span><span style="color: #000000;"> select Name, Value } </span></div> |
And then the direct memberships using a very loooong WMI query
1 2 3 4 5 6 7 |
<div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #000000;"> </span><span style="color: #800080;">$r</span><span style="color: #000000;">.Collections </span><span style="color: #000000;">=</span><span style="color: #000000;"> Get</span><span style="color: #000000;">-</span><span style="color: #000000;">WmiObject </span><span style="color: #000000;">-</span><span style="color: #000000;">query </span><span style="color: #800000;">"</span><span style="color: #800000;">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)</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">-</span><span style="color: #000000;">Namespace </span><span style="color: #800080;">$namespace</span><span style="color: #000000;"> </span><span style="color: #000000;">|</span><span style="color: #000000;"> Select CollectionID, Name </span></div> |
Finally stuff the object into the result array
1 2 3 4 5 6 7 |
<div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #000000;"> </span><span style="color: #800080;">$result</span><span style="color: #000000;"> </span><span style="color: #000000;">+=</span><span style="color: #000000;"> </span><span style="color: #800080;">$r</span><span style="color: #000000;"> </span></div> |
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.
Leave A Comment