Skip to main content

SCCM 2007 + PowerShell - WMI is the key to automation Part 2

In Previous post I talked about how to explore the WMI namespace for ConfigMgr 07.
In this post would like to point out on how to use the SCCM-Commands PowerShell Module , this Module leverages WMI with PowerShell.

Many thanks to Michael Niehaus, Rikard Ronnkvist, Stefan Ringler and Ricardo Cheing authors of this Module for sharing this.

Download this module and extract it, it contains a single PowerShell Module file .this is a Script Module (SCCM-Commands.psm1). Place this File under your Modules Directory in a folder named same as the module i.e. SCCM-Commands



Now you should be able to see the Script module in you session like below:


Now you have to import the module and then you can list all the Advanced functions shipped with this module using either of the below 2 ways:
 1. Use Get-Command -Module SCCM-Commands
 2. Or Use Get-SCCMCommands function within the module itself.

Now the Functions in the Module are Advanced ones and don't have much documentation on how to use the module......though it is very intuitive.
So first you connect to the ConfigMgr 07 site Server having SMS Provider installed using Function Connect-SCCMServer (Confused on which hostname to supply refer my previous post to get the site System having SMS Provider installed on it).

Store the object returned back by the function for later use in the variable $SCCMServer :


Now the main task at my hand was to automate the Query Membership Rule Creation for a collection. So am going to head in that direction.


Querying Collection

First to get the details of a collection....use Get-SCCMCollection function. Now take a look at the syntax for the function below:

There is a mandatory parameter named -SccmServer which expects an Object as the input, we will pass the Object stored in $SCCMServer as an argument to it . There is an optional parameter by the name -Filter which takes filter in WMI Format.

There is a test collection in my TEST environment named "Deepak Test 1" having CollectionID 'SCM013EA' , Now I can use either the name or collectionid as the filter here.


Using the Name as the filter:

PS C:\> Get-SCCMCollection -SccmServer $SCCMServer -Filter "Name='Deepak Test 1'"


Similarly, we can use the CollectionID as the filter too. Below is how you will do it:

PS C:\> Get-SCCMCollection -SccmServer $SCCMServer -Filter "CollectionID='SCM013EA'" -Verbose

Now pipe the output of the above to Get-Member to see some interesting methods (marked ones) and properties on the Object. 

Now if you see some of these methods are quite interesting (marked above) which will ultimately help in automation of Query Based Deployments.... Keep hanging !



Querying Collection Rules

In the above  2 screenshots there is a property by the name CollectionRules but it is empty. So in order to query the present Collection Rules (Query or Direct) in a Collection we will use another function Get-SCCMCollectionRules . Before that we will store the Collection Object returned above in a variable to later make it easy to reference the properties.


For my test collection I have 3 Query Membership Rules already present...below is a screenshot of it:

collection When I seek the same info from PowerShell it does return be back the Query Rules:


If we select just the RuleName and QueryID then it show there are three Query Rules...which is the correct information:



This is it for this blog.
Well you can do add new Query Rules to a collection using the  Add-SCCMCollectionRule . There is not direct way to modify a Query Expression so we have to delete the Query Rule first modify it and Add it which will be the topic for my next post....




Popular posts from this blog

Test connectivity via a specific network interface

Recently while working on a Private cloud implementation, I came across a scenario where I needed to test connectivity of a node to the AD/DNS via multiple network adapters.  Many of us would know that having multiple network routes is usually done to take care of redundancy. So that if a network adapter goes down, one can use the other network interface to reach out to the node. In order to make it easy for everyone to follow along, below is an analogy for the above scenario: My laptop has multiple network adapters (say Wi-Fi and Ethernet) connected to the same network. Now how do I test connectivity to a Server on the network only over say Wi-Fi network adapter?

PowerShell + SCCM : Run CM cmdlets remotely

Today I saw a tweet about using implicit remoting to load the Configuration Manager on my machine by Justin Mathews . It caught my eye as I have never really tried it, but theoretically it can be done. Note - The second tweet says "Cannot find a provider with the name CMSite", resolution to which is in the Troubleshooting section at the end.

PowerShell : Trust network share to load modules & ps1

Problem Do you have a central network share, where you store all the scripts or PowerShell modules ? What happens if you try to run the script from a network share ? or if you have scripts (local) which invoke scripts or import PowerShell modules stored on this network share ? Well you would see a security warning like below (Note - I have set execution policy as 'Unrestricted' not 'bypass' here): Run a .ps1 from the network share Well this is a similar warning, which you get when you download scripts from Internet. As the message says run Unblock-File cmdlet to unblock the script and then run it, let's try it.