我正在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
}
.
.
.
.
来自模块A的功能正在正确执行,但是我无法从模块B执行功能。另外,在控制台中运行脚本后,当我尝试使用以下命令行开关查找模块B中可用的功能时,
Get-Command -Module ModuleB
我只看到在ModuleC中定义的功能,该功能是由moduleB导入的,但是看不到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
}
我可能错过了一件简单的事情,但我无法做到。很长时间以来,我一直在正常导入模块并使用它们,但这是我第一次遇到此问题。在此先感谢您的帮助。
当您的清单(.psd1)没有指定根模块时,会发生此问题。
@{
# Script module or binary module file associated with this manifest.
RootModule = 'mymodule.psm1'
...
以前,从New-ModuleManifest生成它时,默认情况下它会被注释掉
@{
# Script module or binary module file associated with this manifest.
# RootModule = ''
...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句