use*_*477 6 powershell module powershell-3.0 powershell-4.0
我正在 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
Run Code Online (Sandbox Code Playgroud)
我无法使用脚本中第二个模块 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.
Run Code Online (Sandbox Code Playgroud)
以下是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
}
.
.
.
.
Run Code Online (Sandbox Code Playgroud)
moduleA 中的函数正在正确执行,但我无法执行 moduleB 中的函数。此外,在控制台中运行脚本后,当我尝试使用以下命令行开关查找模块 B 中可用的功能时,
Get-Command -Module ModuleB
Run Code Online (Sandbox Code Playgroud)
我只看到ModuleC 中定义的函数,它是由moduleB 导入的,但我没有看到moduleB 中定义的任何函数。我一直在使用 powershell,但这是我第一次看到这个问题。
当我执行 Get-Module 时,我只看到 moduleA 和 moduleB。
所有模块都按以下方式导入:
Import-Module $PSScriptRoot/../lib/newmodules/moduleB.psm1
Run Code Online (Sandbox Code Playgroud)
全局导入模块也没有解决问题。
通过提供如下实际路径来导入模块也没有解决问题。
Import-Module C:\folderpath\ModuleB.psm1
Run Code Online (Sandbox Code Playgroud)
所有模块中的所有功能定义如下。任何模块中的函数定义都没有区别。
function get-hostLocalCred {
Param($xmldata)
# Function Definition
return $result
}
Run Code Online (Sandbox Code Playgroud)
我可能错过了一件简单的事情,但我无法得到它。长期以来,我一直在正常导入模块并使用它们,但这是我第一次遇到这个问题。在此先感谢您的帮助。
小智 6
也许您正在更新 moduleB 中的代码,同时从 PS 会话测试它。
然后根据Microsoft 的文档,如果您的模块在同一调用会话期间发生了更改,您应该使用 Import-Module 的 Force 参数。
Import-Module $PSScriptRoot\..\lib\nwm\moduleB.psm1 -Force
Run Code Online (Sandbox Code Playgroud)
当您的清单 (.psd1) 未指定根模块时,就会出现此问题。
@{
# Script module or binary module file associated with this manifest.
RootModule = 'mymodule.psm1'
...
Run Code Online (Sandbox Code Playgroud)
以前,当从 New-ModuleManifest 生成它时,它会被默认注释掉
@{
# Script module or binary module file associated with this manifest.
# RootModule = ''
...
Run Code Online (Sandbox Code Playgroud)