Loading the SCCM 2012 R2 Cmdlets from within a module

Hi together,

In the following article was described how to load the Powershell Module for SCCM 2012:

http://blogs.technet.com/b/configmgrteam/archive/2013/03/27/powershell-connecting-to-configuration-manager.aspx

I've taken the code to build a function to load the Powershell Module which works fine when i directly integrate it in my scripts.

Currently i build a Module called SCCM-HelperFunctions.psm1 and the above mentioned function is one which i want to integrate, i named it "Import-Cmdlets". The Import of my Module works fine,  the function "Import-Cmdlets" is available trough IntelliSense and the command ends with no errors in the context of the given Site (ex. "PS CM1:\") but when i try to use SCCM Cmdlets the Command is not found.

I've checked if the Module was loaded successfully but it isn't. Following is the function included in the Module:

Function Import-SCCMCmdlets {
<#
.SYNOPSIS
Ldt die SCCM Cmdlets und wechselt in den Kontext der angegebenen Site.

.DESCRIPTION
Die Funktion Import-SCCMCmdlets ldt die Powershell Cmdlets und wechselt in den PSDrive Kontext des angegebenen Sitecodes.
In diesem Kontext knnen dann die in folgeendem Artikel aufgefhrten Cmdlets ausgefhrt werden:
http://technet.microsoft.com/en-us/library/jj821831%28v=sc.20%29.aspx

.PARAMETER Sitecode 
Eine Zeichenkette mit dem Sitecode der CAS oder Primary Site in dessen Kontext die Cmdlets laufen sollen.

.EXAMPLE
Import-SCCMCmdlets -SiteCode "P01"

.EXAMPLE
$SiteCode = "P01"
Import-SCCMCmdlets -SiteCode $SiteCode
#>
    Param (
		[Parameter(Mandatory=$true)]
		$SiteCode
	)

$null = Import-Module ($env:SMS_ADMIN_UI_PATH.Substring(0,$env:SMS_ADMIN_UI_PATH.Length  5) + '\ConfigurationManager.psd1')
 
Set-Location "$($SiteCode):" | Out-Null
if (-not (Get-PSDrive -Name $SiteCode))
    {
        Throw "Beim Versuch das Powershell Modul zu laden und auf das PSDrive $Sitecode zu wechseln trat ein Fehler auf."
}
}

The exact same function works when i copy it to the ISE and run it.
Does anyone have any ideas and can point me into the right direction? (eventually different workspaces when the function runs from within the module or something like that?)

Best regards

Rolf


  • Moved by Bill_Stewart Monday, March 02, 2015 8:24 PM Move to more appropriate forum
January 14th, 2015 1:19pm

Hi Rolf,

what exactly do you mean? You talk about a function called Import-Cmdlets and you talk about / show a function called Import-SCCMCmdlets. What isn't working?

You should be able to Import / Load a module from another module by doing:

if (!(Test-Path "$($siteCode)`:"))
{
  New-PSDrive -Name $siteCode -PSProvider "AdminUI.PS.Provider\CMSite" -Root "$($sccmServer).$($env:USERDNSDOMAIN)" -Description "$($siteCode) Primary Site" 
}
Set-Location "$($siteCode)`:"

Write-Output "Location successfully set to: $($siteCode)."

Free Windows Admin Tool Kit Click here and download it now
July 10th, 2015 8:59am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics