Most Configuration Manager objects share common attributes, however most of them differ in the property name. For example, collections and folders both have IDs, but for collections the ID property is named CollectionID and the folders have the name ContainerNodeID.
In order to maximize pipelineability (apparently I made up a new word) in PowerShell I found it was easiest to encapsulate them into a custom object and give them a more generic name, like ObjectID.
I read some different web articles and merged a few approaches into something that satisfied me; is it the best and only way, no but I’m having fun with it and thought I would share what I learned:
The base of this custom object stems from the managed code approach of defining a class, complete with a constructor, other common properties and also some member functions to perform common actions and then instantiating an instance of it as needed by functions in your script for New/Move/Delete/Other scenarios. Since at some point we actually need to access the WMI object directly to perform specific actions one of the member properties of this object stores the actual WMIObject.
If you are familiar with ConfigMgr lazy properties, which exist on collections (membership rules), advertisements (schedule tokens) and other objects you encounter, wrapping them in other pipeline friendly functions too allows me to not only get the object but also obtain a direct object reference at the same time to access the lazy properties; not a fan of lazy properties but I assume they exist for some specific WMI related reason…
I’m now breaking up with Windows Live Writer 2012 for it’s inability to handle PowerShell code formatting natively or with easy to find plug-ins, plus other general usability annoyances; it does play nice with MyITForum though.
Instead of explaining inline in the article, and while I search for a new blogging tool, download the .ps1 script (link) and read the rest of the story in there.
Have suggestions of a good blog tool? Leave me a comment.
-1 user for Live Writer 2012
The examples for collections and folders in the attached file are a snippet from a larger script that syncs patching collections into maintenance window trees from a SQL database for a Configuration Manager 2012 SP1 instance I’m playing with prior to a Datacenter wide migration to the new platform; I’ll see if I can find the time to publicize the full code one of these days.
Using this new coding style I’m able to send all different kinds of ConfigMgr items through pipeline aware functions and allow the objects to supply their own parameters by a common name (or at least minimize the amount of params needed to work). Since all the functions return objects back to the pipeline you can access native PowerShell pipeline filters too!
I was able to trim paragraphs of code from old scripts into powerful single liners, which I think is the way PoSh intended it. Granted there is a some overhead for the object creation code, but it is easy to snap to what I think is a clean coding style.
Download the example script:: link
Sorry this blog got so dusty, but I’ve been pretty busy in/out of work since my last entry.