The SCCM provider comes with some useful methods in particular for collections. You can view them using WBEMtest:
We’ve already used one of these in a previous function. The add-SCCMcollectionrule that Michael Niehaus defined in one of his blog posts uses the addmembership method. Another use is in the add-dirUserCollectionRule defined in our last post which shows the RequestRefresh() method.
So if you go to a Powershell prompt and use one of our “cmdlets” to get a collection:
$collection = get-sccmCollection –filter “collectionID = ‘SMS00001’”
$collection will now represent our “All Systems” collection. We can now get the provider to refresh the collection:
and we can follow the refresh in the colleval log. One of the other methods available to us is GetNumResults. The methods defined for the SMS_Collection class are described in the MOF file which can be viewed using the “Show MOF” button in WBEMtest. The GetNumResults method returns the count of the resources in a collection. This could be really useful and saves us a whole load of scripting in terms of looping through a collections’ members. So:
$count = $Collection.GetNumResults()
should give the collection count, except it doesn’t. Powershell throws us an error shown below.
Well, doesn’t that want to make you throw your Laptop through the window? “SMS_collection doesn’t contain a method called GetNumResults”. Yes it does, I can see it. I got so frustrated that I eventually decided to get round this problem so that I could use this method. A quick Bing lead me to the “invoke-wmimethod” cmdlet. Some playing around with that and I got to create my own function:
$Collectionnamespace = $Sccmnamespace+":SMS_Collection"
$collobject = invoke-wmimethod -path $collectionNameSpace –name` GetNumResults -argumentlist $collectionID,$Whatever
set-variable -name CollectionCount -value $collObject.Result -scope global
Notice here that we first create a collection namespace based on the $SCCMNameSpace variable created when we connect to the site. We invoke the WMI method against this and NOT the specific collection object and then create a global variable against the result.
Once that function is loaded into your profile and you’ve connected to a site, then to get the collection count of the All Systems collection:
write-host “Count = $CollectionCount”
So, the answer to the question in the title? I don’t know. All I can say is that we’ve run the method against the SMS_Collection class and not the collection instance as we do when we invoke the requestrefresh method.
This post was contributed by Tom Parker, a Dedicated Supportability Engineer with Microsoft Premier Field Engineering, UK.