There have been quite a few times that I’ve needed to do some comparisons, or even move some objects from one SCCM environment to another. The System Center Configuration Manager console has quite a few options for exporting objects, or saving things to CSV, but then you still need to import it. Check out the rest of this blog post to see how easy it is to report, compare or recreate things between your SCCM environments with PowerShell.

Using SCCM CmdLets

If you’ve ever delved into the world of PowerShell and SCCM, it’s actually quite simple. Install the Configuration Manager console and voila the CmdLets are installed for you. To access the SCCM Module, it’s not as easy as the built in modules.

I’ll say that there are two common ways of loading the module.

  1. Open the SCCM Console
    1. On the blue drop down icon on the top left, click it and then choose Connect via Windows PowerShell

  2. Manually import the SCCM Module into an existing PowerShell console.
    1. Open a PowerShell Window, and run the .PSD1 file
      Set-Location C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin
      Import-Module .\ConfigurationManager.psd1 -verbose 
      

The Magic

After you’ve imported the module, the CmdLets are available, but they act a little differently. The SCCM CmdLets do not run unless you are in the SCCM Site “context”. By this I mean, we need to look behind the curtain and it will make sense. PowerShell uses from time to time what we call PSDrive Providers.

When you type, you’ll see what “Drives” are available to you. If you ever worked with the C:\, D:\ etc, you’ve been using the File System Provider. Have you ever tried looking at the certificates on your machine? Try Set-Location Cert: or perhaps you’ve queried or manipulated the registry by using Set-Location HKLM

Looking behind the curtain with SCCM, you’ll noticed after the module is imported, it was kind enough to create a new PSDrivve Provider for your site. Now when you use Set-Location YXZ: PowerShell will interact with the SCCM PSProvider. Once inside the provider, we can work with SCCM!

Run Get-PSDrive and see which providers you have available.

Get-PSDrive 

Connecting to Multiple SCCM Environments

Now that we know about PSDrive Providers and that when we import the ConfigurationManager.psd1 module, a new PSDrive Provider is added for our current site. Perfect! Well in order to work with another site, why not just create another PSDrive Provider? Sure, let’s do it, because it’s actually quite easy!

We need to know 3 things to do this:

  1. The Site Code of the environment
  2. The PSProvider, in our case for SCCM it is: AdminUI.PS.Provider\CMSite
  3. The Primary Site Server of the environment

Now to tie it all together in a PowerShell command and create a new PSDrive Provider, let’s run:

$DevSCCMSite = "Dev"
New-PSDrive -Name $DevSccmSite -PSProvider "AdminUI.PS.Provider\CMSite"  -Root SCCM01-Dev.corp.ad
Set-Location "$($DevSCCMSite):"

Now that we have 2 or more multiple SCCM PSDrive Providers, we can switch between them within a single PowerShell session. Now I can query objects in one site and manipulate objects in another site

Example

In this example we can do something simple such as connect to our Dev Server, get the member of a collection and add similar named resources to a collection in the Production SCCM site.

$DevSCCMSite = "Dev"
$ProdSCCMSite = "Prod"
$DevCollection = "DMZService-XYZ"
$ProdCollection = "DMZ-Service-XYZ"



New-PSDrive -Name $DevSccmSite -PSProvider "AdminUI.PS.Provider\CMSite"  -Root SCCM01-Dev.corp.ad
Set-Location "$($DevSCCMSite):"
$members = (Get-CMDeviceCollection -Name $DevCollection | Get-CMCollectionMember).Name

Set-Location "$($ProdSCCMSite):"
Foreach ($MemberName in $members) {
    $ResourceID = (Get-CMDevice -Name "PRD$($MemberName)").ResourceID
    If ($ResourceID) {
        Add-CMDeviceCollectionDirectMembershipRule -CollectionName $ProdCollection -ResourceId $ResourceID
    }
}