Tuesday, August 20, 2013

PowerShell module not working

Recently I wrote a PowerShell script to do some magic stuff triggered by a SCOM notification.

Because it is a SCOM 2012 I loaded the module by

Import-Module OperationsManager

and while running interactively it worked as expected. However, the script failed as soon as it has been fired from the notification channel. Since I have some logging within all my scripts writing some debugging information and the $error variable to the event log it was easy

“The specified module ‘OperationsManager’ was not loaded because no valid module file was found in any module directory.”

That occurs even if the path is registered in the $env:PSModulePath.

After checking the $env:PSModulePath I found that the setup routine did not registered the correct path to the SCOM module:

C:\Users\me\Documents\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules\;C:\Program Fi
les\Microsoft System Center 2012 R2\Operations Manager\Powershell\
;C:\Program Files\Microsoft System Center 2012 R2\Ope
rations Manager\Powershell\

However, using the absolute path to the PSM file helps:

Import-Module “C:\Program Files\System Center 2012\Operations Manager\Powershell\OperationsManager\OperationsManager.psm1”

To get the right path you can do the following:

$setupKey = Get-Item -Path "HKLM:\Software\Microsoft\Microsoft Operations Manager\3.0\Setup"

$installDirectory = $setupKey.GetValue("InstallDirectory") | Split-Path

$psmPath = $installdirectory + ‘\Powershell\OperationsManager\OperationsManager.psm1’

Import-Module $psmPath

All information is provided "as is" without any warranty! Try in lab before. Handle with care in production.

3 comments:

  1. You saved my sanity with this. Thanks!

    ReplyDelete
  2. You're welcome!

    Weird enough this is still a problem in R2 UR3. Use the following lines to asure working PowerShell notifications:

    # Import Operations Manager Module and create Connection
    Import-Module OperationsManager;
    # for SCOM 2012 SP1:
    if ($env:PSModulePath -notlike "*\PowerShell\OperationsManager*") {
    $setupKey = Get-Item -Path "HKLM:\Software\Microsoft\Microsoft Operations Manager\3.0\Setup"
    $installDirectory = $setupKey.GetValue("InstallDirectory") | Split-Path
    $psmPath = $installdirectory + ‘\Powershell\OperationsManager\OperationsManager.psm1’
    Import-Module $psmPath
    }
    New-SCOMManagementGroupConnection (hostname);

    ReplyDelete
  3. 2 days i am trying to solve out this issue... !!
    Thank you very much ! You saved my day !
    My SCOM servers are 2012 R2 UR3 as well.

    ReplyDelete