A while back I wrote a PowerShell Module that showed some ways to do things in ConfigMgr using PowerShell. Since then I’ve added more and more things to my PowerShell library and thought it was time to demonstrate a slightly better way to import the ConfigMgr PowerShell module.

Anyone who has worked with the ConfigMgr PowerShell module has at some point experienced the moment when they attempt to run

import-module ConfigurationManager

only to receive a bunch of angry red text indicating that the specified module wasn’t found in any module directory.

This is caused by the ConfigMgr module not being loaded to the default PowerShell module library location when the console is installed but rather to the ConfigMgr install directory. So what can we do about that? Well fortunately we know that the ConfigMgr PowerShell cmdlets are all stored in the installation direction. We know this because if in the ConfigMgr console we hit the drop down and select ‘connect via Windows PowerShell ISE’ it will open a PowerShell Script in ISE that shows how the module is loaded and the PSDrive is changed.

 

This gives us the pieces we need to build some code since we now know the location we can build out the following code to ensure we have a full path to the location:

(Join-Path $(Split-Path $ENV:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1)

When we run the above that will produce a path from wherever we currently are to the ConfigMgr directory where the module file lives. While this gives us a way to get the cmdlets I wanted to take things a step further so I wrote a function that allows me to import the module whenever I need it and attached it to my PowerShell profile. It looks something like this.

function Get-CMModule
{
    [CmdletBinding()]
    param()
    Try
    {
        Write-Verbose "Attempting to import SCCM Module"
        Import-Module (Join-Path $(Split-Path $ENV:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1) -Verbose:$false
        Write-Verbose "Successfully imported the SCCM Module"
    }
    Catch
    {
        Throw "Failure to import SCCM Cmdlets."
    } 
}

This now gets us to the point where we have the module loaded but of course we can’t do anything with the module until after we connect to the appropriate drive so I wrote two more functions. One is a generic function that tests if a module is loaded and then a second one that tests specifically for the ConfigMgr module and if you’re connected to a ConfigMgr PowerShell drive.

function Test-ConfigMgrAvailable
{
    [CMdletbinding()]
    Param
    (

    )
        try
        {
            if((Test-Module -ModuleName ConfigurationManager) -eq $false){throw "You have not loaded the configuration manager module please load the appropriate module and try again."}
            write-Verbose "ConfigurationManager Module is loaded"
            Write-Verbose "Checking if current drive is a CMDrive"
            if((Get-location).Path -ne (Get-location -PSProvider 'CmSite').Path){throw "You are not currently connected to a CMSite Provider Please Connect and try again"}
            write-Verbose "Succesfully validated connection to a CMProvider"
            write-verbose "Passed all connection tests"
            return $true
        }
        catch
        {
            $errorMessage = $_.Exception.Message
            write-error -Exception CMPatching -Message $errorMessage
            return $false
        }
}

function Test-Module
{
    [CMdletbinding()]
    Param
    (
        [Parameter(Mandatory = $true)]
        [String]$ModuleName
    )
    If(Get-Module -Name $ModuleName)
    {
        return $true
    }
    If((Get-Module -Name $ModuleName) -ne $true)
    {
        return $false
    }
}

The above will allow you to confirm that the module is present and make sure that you are connected to the ConfigMgr PSDrive when running advanced scripts in your environment.

Hopefully this is helpful!

(2628)

Jordan Benzing

Jordan has been working in the Industry since 2009. Since starting he’s worked with Active Directory, Group Policy, SCCM, SCOM and PowerShell. Jordan most recently worked in the healthcare industry as an SCCM Infrastructure Team lead supporting over 150,000 endpoints. Jordan currently works as a Senior consultant for TrueSec Inc in the U.S. Most recently his focus has been in SQL Reporting for SCCM, creation of PowerShell scripts to automate tasks and PowerBI.

comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.