【发布时间】:2016-11-13 23:30:53
【问题描述】:
我在 Windows Server 机器上使用 PowerShell V 4.0。我遇到了无法调试或找到解决方案的问题。
我有一个 ps1 脚本,它导入两个 psm1 模块 A 和 B。(B 反过来导入另一个模块 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
我无法在脚本中使用第二个模块 moduleB 中的函数。当我运行脚本时,它会在使用模块 B 中的函数的地方引发错误。错误如下(get-hostLocalCred为函数名。)
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.
以下是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
}
.
.
.
.
moduleA 中的函数正在正确执行,但我无法执行 moduleB 中的函数。 此外,在控制台中运行脚本后,当我尝试使用以下命令行开关查找模块 B 中可用的功能时,
Get-Command -Module ModuleB
我只看到ModuleB 导入的ModuleC 中定义的函数,但没有看到moduleB 中定义的任何函数。我一直在使用 powershell,但这是我第一次看到这个问题。
当我执行 Get-Module 时,我只看到 moduleA 和 moduleB。
所有模块都通过以下方式导入:
Import-Module $PSScriptRoot/../lib/newmodules/moduleB.psm1
在全球范围内导入模块也没有解决问题。
通过给出如下的实际路径来导入模块也没有解决问题。
Import-Module C:\folderpath\ModuleB.psm1
所有模块中的所有功能都定义如下。任何模块中的函数定义都没有区别。
function get-hostLocalCred {
Param($xmldata)
# Function Definition
return $result
}
我可能错过了一件简单的事情,但我无法得到它。长期以来,我一直在正常导入模块并使用它们,但这是我第一次遇到这个问题。在此先感谢您的帮助。
【问题讨论】:
-
你能提供minimal reproducible example吗?没有复制案例就很难说什么。
-
@PetSerAl:对不起,简短的代码。我添加了部分脚本和模块。
-
ModuleB 中有 Export-ModuleMember 调用吗?
-
@MikeShepard:不,我没有。我在 moduleB 中的函数定义之后添加了 export-modulemember -function get-hostLocalCred。但这没有帮助。错误消息保持不变。
标签: powershell module powershell-3.0 powershell-4.0