9

I am working with PowerShell V 4.0 on a Windows Server machine. I have encountered a problem that I am not able to debug or find a solution.

I have a ps1 script that imports two psm1 modules A and B. (B in turn imports another module C).

Import-Module  $PSScriptRoot\..\lib\infra\moduleA.psm1 
Import-Module  $PSScriptRoot\..\lib\nwm\moduleB.psm1 

#get-logger function works fine. This is defined in moduleA
$log = get-logger

$smisXmlData = [xml] $smisConfigData

#The function get-hostLocalCred is defined in moduleB. This is where the error occurs. 
($username, $password) = get-hostLocalCred $smisXmlData

I am not able to use the functions from the second module moduleB in the script. When I run the script, it throws errors where ever a function from the module B is used. The error is below (get-hostLocalCred is the function name.)

get-hostLocalCred : The term 'get-hostLocalCred' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling  of the name, or if a path was included, verify that the path is correct and try again.

The following is the content in the moduleB.

#Importing moduleC. 
Import-Module  $PSScriptRoot/../infra/moduleC.psm1

#Defining required functions. These are the functions that are not available in the above script. 
function get-hostLocalCred {
    Param($xmldata)
    $log.entry()
    $username = $xmldata.component.yourhost.localcred.username
    $log.debug("USERNAME: $username")
    $password = $xmldata.component.yourhost.localcred.password
    $log.exit()
    return $username, $password
}

function new-ArrayCredObj {
Param([Parameter(Mandatory = $true)] $user,
  [Parameter(Mandatory = $true)] $password)
    $log.entry()
    $log.info("Creating custom Object")
    $log.debug("User : $user")
    $log.debug("Pwd : $password")
    $arrayCred = New-Object psobject
    $arrayCred | Add-Member -MemberType NoteProperty -Name auser -Value $user
    $arrayCred | Add-Member -MemberType NoteProperty -Name password -Value $password
    $log.exit()
    return $arrayCred
}
.
.
.
.

The functions from moduleA are being executed properly, but I am not able to execute the function from moduleB. Also, After running the script in the console, When I try to lookup the functions available in the Module B, using the following commandlet,

Get-Command -Module ModuleB

I only see the functions defined in the ModuleC, which is imported by moduleB, but I donot see any of the functions defined in the moduleB. I have been working with powershell but this is the first time I have seen this issue.

When I do a Get-Module, I see only moduleA and moduleB.

All the Modules are imported in the following way:

Import-Module  $PSScriptRoot/../lib/newmodules/moduleB.psm1 

Importing modules Globally has also not solved the problem.

Importing modules by giving actual path like following has also not solved the issue.

Import-Module C:\folderpath\ModuleB.psm1 

All the functions in all the modules have been defined as following. There is no difference in the function definition in any of the modules.

function get-hostLocalCred {
    Param($xmldata)
    # Function Definition 
    return $result
}

I might be missing a simple thing but I am not able to get it. I have been importing modules normally and working with them since long time but this is the first time I ran into this issue. Thanks in advance for the help.

4
  • 1
    Can you provide minimal reproducible example? It is hard to say anything without repro case. Commented Jul 11, 2016 at 22:13
  • @PetSerAl: Sorry about the brief code. I have added some part of the script and module. Commented Jul 11, 2016 at 22:37
  • Do you have Export-ModuleMember calls in ModuleB? Commented Jul 11, 2016 at 23:26
  • @MikeShepard: No, I donot have them. I have added export-modulemember -function get-hostLocalCred after the function definition in moduleB. but that didnot help. The error messages remain the same. Commented Jul 11, 2016 at 23:49

4 Answers 4

8

Maybe you are updating the code in moduleB while testing it from a PS session.

Then according to Microsoft's documentation you should use the Force parameter of Import-Module if your module has changed during the same calling session.

Import-Module $PSScriptRoot\..\lib\nwm\moduleB.psm1 -Force
Sign up to request clarification or add additional context in comments.

Comments

5

This problem occurs when your manifest (.psd1) does not have a root module specified.

@{

# Script module or binary module file associated with this manifest.
RootModule = 'mymodule.psm1' 
...

Previously, it would have been commented out by default when generating it from New-ModuleManifest

@{

# Script module or binary module file associated with this manifest.
# RootModule = '' 
...

Comments

0

My problem was I was re- importing the moduele.psm1 into the ISE and powershell noticed it had already been imported so didn't import it again. Even after I had edited the file.

The sam happens in a powershell console session if you didnt exit and restart before re-importing,

Ie:

  1. (works) edit file
  2. (works) import
  3. (works) edit file again
  4. (doesnt work) import

I had to do a Remove-Module modulename before my re-import with Import-Module for it to see my edits, including my new functions.

Comments

-2

I had similar issue and it worked by adding "-Scope Global" to import-module cmdlet

1 Comment

This worked for me as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.