PowerShell and Configuration Manager 2012 R2–Part 1


Summary: Load the Configuration Manager cmdlets and make a basic site connection.

Hey, Scripting Guy! Question Hey, Scripting Guy!

I’ve been doing a lot of work in the Configuration Manager 2012 R2 console and it’s beautiful. But I find I spend a lot of my time clicking all over the place and repeating tasks. Are there PowerShell cmdlets available that I can use?

—SH

Hey, Scripting Guy! Answer Hello SH,

Honorary Scripting Guy, Sean Kearney, is here today to give you the answer you want, which is, “Yes!” There absolutely are cmdlets for Windows PowerShell in Configuration Manager 2012 R2, and boy, are they cool! I’ve had to do some repetitive tasks in Configuration Manager for a client, and I and was thinking exactly the same thing.

   Note  This is a five-part series that includes the following posts:

My first challenge was that I couldn’t actually see a shortcut to the PowerShell cmdlets. But with a quick search online, I found that they are most certainly there.

To load the module, you need to run this code in Windows PowerShell:

Import-Module ‘C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1’

You can now access the list of the available cmdlets by using the Get-Command cmdlet and targeting the new module:

Get-Command –module ConfigurationManager

Before you get too far, remember you’re going to need to bring in Help for this new module. Remember the Help is no longer built-in by default. I had initially forgotten this, and I was uttering “magic phrases” when I could not find any examples to work with.

….and then a few moments later, I smacked the front of my head realizing what I needed to do:

Update-Help –module ConfigurationManager

Moments later, Help was on its way!

Your next challenge seems trivial, but you might try to run something simple like getting a list of boundaries with the Get-CMBoundary cmdlet. You'll run in to this little error message:

Get-CMBoundary: This command cannot be run from the current drive. To run this command, you must first connect to a Configuration Manager drive.

Well, isn’t that a special little message? In reality, you must be in the context of your Configuration Manager site. This is simply an extension of the PSDrives in your Windows PowerShell console, and it is very much the same as changing a drive letter.

The actual “letter” (if you want to think of it like that) is simply your site code. Now I could tell you, “Just go look that up in the GUI.” I could do that.

But this is a Hey, Scripting Guys! Blog post, and it would make far more sense to show you how to get that information with Windows PowerShell, right?

If you were to run Get-PSDrive on a system with the Configuration Manager console installed, you would get output similar to the following:

Image of command output

As you can see from the section highlighted in yellow, the Provider Type I use to target a Configuration Manager site is called CMSite. My site name is NIH (that’s how I keep the users in control, you see—every time I tell them the site name, they back away from the vicious sound of NIH).

Too much Monty Python already? Sorry. I shall avoid saying NIH again. I promise. No more NIH.

To programmatically access the site name, you should use an object like this in a PowerShell script that uses the cmdlets—or perhaps add this to your profile if you regularly work with the Configuration Manager cmdlets:

$CMSite=”$(Get-PSDrive –PSProvider CMSite)`:”

Set-Location $CMSite

When you run this, you’ll see a prompt similar to this on your console (you will see something other than NIH as your site name, followed by a colon):

NIH:

From this point we can directly ask Configuration Manager questions, such as, "What are the boundaries on this site?"

Get-CMBoundary

Or maybe something interesting like finding out the Configuration Manager site system server:

Get-CMSiteSystemServer

And of course, all of this information is easily exportable by using Export-CSV or Export-CLIXML. There are some amazing solutions on the Internet for documenting a Configuration Manager environment by using Windows PowerShell!

SH, that is all there is to getting the Configuration Manager module loaded and getting access to its cmdlets. Stay tuned. Tomorrow I'll explore how to use the Configuration Manager cmdlets to work with collections!

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to them at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. Until then, always remember that with great PowerShell comes great responsibility.

Sean Kearney, Honorary Scripting Guy and Cloud and Datacenter Management MVP 

Comments (2)

  1. DexterPOSH says:

    Nice , so finally ConfigMgr is getting enough traction in the PowerShell community.
    Wanted to point out that there is a new better way for getting regular updates for the PS CM module using the cmdlet library.

    Also it modifies the $Env:PSModulePath to include the CM module , so you can just do a

    Import-Module ConfigurationManager

    Moreover, the cmdlet library checks at regular interval (configurable) if the CM module running on the machine needs an update. For more information read here ->

    https://technet.microsoft.com/en-us/library/dn958404%28v=sc.20%29.aspx

  2. @DexterPOSH

    The ConfigMan modules have been around for the past 3 years but I felt they needed to be shown a little love 🙂

    They’re for the most part 95% there. There’s the odd task (like when trying to add in Registry detection an Application) where it’s falls JUST a little short. I’m really hoping the Configuration Manager team polishes those bits up. I REALLY REALLY would have
    loved to have been using these when I was managing Configuration Manager 2007 🙂

    Sean
    The Energized Tech – Cloud and Datacenter Management MVP
    Honorary Scripting Guy

Skip to main content