SCCM and PowerShell Part 2
Add Computer to a specific collection
Ok, our computer is in SCCM, let see how to add it in a specific collection. What told the SDK ? the VBScript sample is :
' Add the computer to the all systems collection.
set collection = connection.Get("SMS_Collection.CollectionID='SMS00001'")
set collectionRule=connection.Get("SMS_CollectionRuleDirect").SpawnInstance_
collectionRule.ResourceClassName="SMS_R_System"
collectionRule.ResourceID= outParams.ResourceID
collection.AddMembershipRule collectionRule
We can reorganize the code as shown below to ease our understanding of the process :
set collectionRule=connection.Get("SMS_CollectionRuleDirect").SpawnInstance_ collectionRule.ResourceClassName="SMS_R_System" collectionRule.ResourceID= outParams.ResourceID
set collection = connection.Get("SMS_Collection.CollectionID='SMS00001'")
collection.AddMembershipRule collectionRule
This sample is about "All Systems" Collection, however this collection isn't really usefull because, as we saw in the previous part of this tutorial, when you add a computer in SCCM, it is added by default in this collection. But this example shows us usefull elements: we know that we need to create an "SMS_CollectionRuleDirect", fills some parameters, then invoke the Method "AddMemberShipRule" with this object as a parameter.
Let's go slowy, step by step...
Create an object from the SMS_CollectionRuleDirect class
With PowerShell, we can initialise an instance of a WMI like this :
$objColRuledirect = [WmiClass]"\\$Computer\ROOT\SMS\site_001:SMS_CollectionRuleDirect"
You can see the properties of this object with the psbase.properties :
In the SDK, we learned that 2 properties has to be filled :
So with PowerShell :
$objColRuleDirect.psbase.properties["ResourceClassName"].value = "SMS_R_System"
$objColRuleDirect.psbase.properties["ResourceID"].value = $objCMComputer.resourceID
As you can see below, the CollectionRuleDirect is now properly set with the ResourceID of the $objCMComputer (same as sample of Part 1) :
We need now to use this object with the AddMemberShipRule method
How to use the AddMemberShipRule Method of the SMS_Collection class
When we say "WMI Method", we call WmiExplorer in order to have à "pre-build" script sample :
What we see here is that we need the ID of the target collection. We have 2 options :
- Using Hard-Coded Collection ID found in the SCCM console (smart)
- Determine this ID with, for example, a collection Name (Smarter)
We are smart people, we will determine this ID with PowerShell!
It's really easy, just create a "classic" connexion to the SMS_Collection class, and filter it with our target collection name (in my example : "PowerShell Collection")
$Collection = gwmi -computer $computer -namespace "root\sms\site_001" -class "SMS_Collection" $PoshCollec = $collectionwhere{$_.Name -eq "PowerShell Collection"}
No difficult at all ! the last thing to do is adapt the WmiExplorer sample with the collectionID of this object, and use our previously build CollectionRuleDirect object :
$Class = "SMS_Collection"
$Method = "AddMembershipRule"
$CollectionID = $PoshCollec.CollectionID
$filter="CollectionID = '$CollectionID'"
$MC = Get-WmiObject $class -computer $Computer -Namespace "ROOT\SMS\site_001" -filter $filter
$InParams = $mc.psbase.GetMethodParameters($Method)
$InParams.collectionRule = $MCCollectionRuledirect
$inparams.PSBase.properties select name,Value Format-Table
$R = $mc.PSBase.InvokeMethod($Method, $inParams, $Null)
Work done : our computer is now in the "PowerShell Collection" collection.
With some R&D, and with a good understanding of these different examples, you are now able to create many kinds of PowerShell scripts with SCCM. Note that nearly all this samples can be used with SMS too !
Hope this helps you SCCM people, have a Ctrl+C below for a complete PowerShell sample script :
**************************************************************************
## Name : Add Computer to Collection SCCM PowerShell script
## Author : Antoine Habert devinfra@gmail.com
### Variables
$strTargetMac = "0A:0B:0C:0D:0E:0F"
$strTargetComputerAccount = "NomDeMachine"
$computer = "."
$strTargetCollection = "La collection cible"
# Create computer in SCCM
$Class = "SMS_Site"
$Method = "ImportMachineEntry"
$MC = [WmiClass]"\\$Computer\ROOT\SMS\site_001:$Class"
$InParams = $mc.psbase.GetMethodParameters($Method)
$InParams.MACAddress = $strTargetMac
$InParams.NetbiosName = $strTargetComputerAccount
$InParams.OverwriteExistingRecord = $true
$inparams.PSBase.properties select name,Value Format-Table
$objCMComputer = $mc.PSBase.InvokeMethod($Method, $inParams, $Null)
# Create Collection Rule Direct
$objColRuledirect = [WmiClass]"\\$Computer\ROOT\SMS\site_001:SMS_CollectionRuleDirect"
$objColRuleDirect.psbase.properties["ResourceClassName"].value = "SMS_R_System"
$objColRuleDirect.psbase.properties["ResourceID"].value = $objCMComputer.resourceID
# Target Collection connection
$Collection = gwmi -computer $computer -namespace "root\sms\site_001" -class "SMS_Collection"
$PoshCollec = $collectionwhere{$_.Name -eq $strTargetCollection}
# Add Computer to Target Collection
$Class = "SMS_Collection"
$Method = "AddMembershipRule"
$CollectionID = $PoshCollec.CollectionID
$filter="CollectionID = '$CollectionID'"
$MC = Get-WmiObject $class -computer $Computer -Namespace "ROOT\SMS\site_001" -filter $filter
$InParams = $mc.psbase.GetMethodParameters($Method)
$InParams.collectionRule = $MCCollectionRuledirect
$inparams.PSBase.properties select name,Value Format-Table
$R = $mc.PSBase.InvokeMethod($Method, $inParams, $Null)
**************************************************************************