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.
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 = ''
...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With