【问题标题】:Functions from an imported module are not available in a powershell script来自导入模块的函数在 powershell 脚本中不可用
【发布时间】: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


【解决方案1】:

我有类似的问题,它通过将“-Scope Global”添加到 import-module cmdlet 来工作

【讨论】:

  • 这对我也有用。
【解决方案2】:

当您的清单 (.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 = '' 
...

【讨论】:

    【解决方案3】:

    也许您在从 PS 会话测试模块B 中的代码时正在更新它。

    那么根据Microsoft's documentation,如果您的模块在同一调用会话期间发生更改,您应该使用 Import-Module 的 Force 参数。

    Import-Module $PSScriptRoot\..\lib\nwm\moduleB.psm1 -Force
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      相关资源
      最近更新 更多