Skip to main content

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.



Use Import-Module


Below is a video showing how you can use Import-Module as mentioned in the tweet, if everything goes without any errors :


Below is the code snippet used in the video :


001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
# Create the PSSession
$Session = New-PSSession -ComputerName sccm

# Load the CM Module using Implicit Remoting
Import-Module -Name "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1" -PSSession $Session

# Check the module is available locally
Get-Module -Name ConfigurationManager

# run the CM cmdlets locally
Get-CMSite

# Oops! We need to set the CMSite as our current location to run the CM cmdlets
Invoke-Command -Session $Session {Set-Location -Path DEX:}

# Run the cmdlet again
Get-CMSite


Use Export-PSSession

Now one can easily be tempted to run the Export-PSSession and store the module locally and next time just import the earlier exported module and start using the CM cmdlets, but it is not that straight forward at-least with the CM cmdlets. 

But there is a way to run the exported cmdlets (or Proxy Functions) by explicitly loading the CM module on the remote session and changing to CMSite provider as the current location ( which I think is pointless as the exported Module should be doing it).

Below is a video showing how to do that :

`



Below is the first code snippet (as in above video) used in first PowerShell ISE tab :

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
# Create the PSSession
$Session = New-PSSession -ComputerName sccm

# Load the CM Module in the Remote PSSession and change the current location to your CMSite
Invoke-Command -Session $Session {
                                    Import-Module -Name "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1"
                                    Set-Location -path DEX:
                                    }

# Export the Module Locally
Export-PSSession -OutputModule RemoteCMModule -Session $Session -Module ConfigurationManager

# This will work in the current PowerShell session
Import-Module RemoteCMModule

# run the CM cmdlets
Get-CMsite

Now on another ISE Tab, we have to do below :

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023

# check the current PSSessions
Get-PSSession

# On a new PowerShell session , Import the module
Import-Module -Name RemoteCMModule

# try running a CM cmdlet now
Get-CMsite

# check the current PSSessions
$session = Get-PSSession

# One has to explicitly run this
Invoke-Command -Session $Session {
                                    Import-Module -Name "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1"
                                    Set-Location -path DEX:
                                    }

# try running a CM cmdlet now
Get-CMsite



Use Invoke-Command

One can always just simply use PowerShell remoting like below :

001
002
003
004
005
006
007
008
009
010
011
012
013

# Create the PSSession
$Session = New-PSSession -ComputerName sccm

# Load the CM Module in the Remote PSSession and change the current location to your CMSite
Invoke-Command -Session $Session {
                                    Import-Module -Name "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1"
                                    Set-Location -path DEX:
                                    }

# run the Cmdlets remotely
Invoke-Command -Session $Session {Get-CMSite}



TroubleShooting:

One must remember that even though you are using a PSSession, the ConfigurationManager module should be loaded in the PSSession and your current location should be in the CMSite PSProvider in order to run the CM cmdlets.


You may come across a scenario (similar to mentioned in the tweet) where the CMSite PSProvider might not have loaded correctly when you connected to the PSSession. You can always load the CMSite PSDrive manually , see below video where I deliberately remove the CMSite PSDrive and map it again :

Note - Did you notice, I later used ABC as the name of the PSDrive instead of my CMSite DEX ;)

Hope this gives you a better insight on running CM cmdlets remotely. One has to understand that Remoting should be enabled on the CM server where you are connecting.

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?

Azure DevOps Tips & Tricks - Find private REST APIs

Original source -  Azure DevOps Tip - Find private APIs Often working with Azure DevOps, I hit a wall trying to automate some tasks but there are no REST API's made public yet. It was one of those task of automating creation of Environments in multi-stage YAML based pipelines in AzDO. Quick research reveals that this has been requested in uservoice  (please upvote). Let's see one of the very simple ways to discover some of these APIs.