Powershell 在模块内加载模块 [范围]

标签 powershell module scope powershell-4.0

解决方案:


Import-Module (Join-Path $PSScriptRoot $_.FileName) -Verbose -Global

在我的模块中添加 -Global 开关,使所有函数都可以在它自己的上下文之外访问。


我创建了一个 Powershell 模块 (ConfigModule.psm1),我曾经使用它来处理我的 config.xml

每当我将模块包含到测试脚本中时,一切正常。

我调用一个名为 Import-ConfigModules 的函数,它为 (type="Module") 导入模块并为 (type="script") 来自 XML,其值为 Get-Item...

所以这是我的加载层次结构:

  1. 测试脚本导入 ConfigModule.psm1
  2. ConfigModule.psm1 导入同一目录下的其他模块
  3. 其他模块具有不同的功能。

问题:

在不同文件中调用模块函数时,模块确实加载,因为我使用 -Verbose 开关进行测试,我可以访问变量但我无法访问函数 无法识别

这是一个导入模块范围问题吗?

如果是这样,如何将模块传递到我的测试脚本上下文中?


ConfigModule.psm1 > Import-ConfigModules

Function Import-ConfigModules {
    param([String]$Path)
    $modules = Get-ConfigModules -Path $Path

    # Iterate by FileType
    $modules.Keys | % {

        switch($modules[$_]) {
            {$_.FileType -eq "module"} {
                # If Module exists, load, else find similar modules=
                if(Test-Path (Join-Path $PSScriptRoot $_.FileName)) {

                    Import-Module (Join-Path $PSScriptRoot $_.FileName) -Verbose
                    # Test if module was loaded
                    if(Get-Module $_.VariableName) {
                        Write-Host "Module [Loaded]: " ($_.FileName) -ForegroundColor Green
                    }
                    else {
                        Write-Host "Module [Error]: Could not import module - " ($_.FileName) -ForegroundColor Green
                    }

                }
                else {
                    Write-Host "Module [Missing]: $($_.FileName).`r`nFound similar: " -ForegroundColor Yellow -NoNewline
                    Get-SimilarFile `
                        -Path $PSScriptRoot `
                        -Name $_.FileName
                }
                break
            }
            {$_.FileType -eq "script"} {
                if(Test-Path (Join-Path $PSScriptRoot $_.FileName)) {
                    Write-Host "Script [Loaded]: " ($_.FileName) -ForegroundColor Green

                    # Create variables dynamically
                    Set-Variable -Name "$($_.VariableName)" -Scope Global -Value (Get-Item -Path (Join-Path $PSScriptRoot $_.FileName))
                }
                else {
                    Write-Host "Script [Missing]: $($_.FileName).`r`nFound similar: " -ForegroundColor Yellow -NoNewline
                    Get-SimilarFile `
                        -Path $PSScriptRoot `
                        -Name $_.FileName
                }
                break
            }
            default { Write-Warning "$($_.FileName) : Bad FileType definition. {FileType: $($_.FileType)}" }
        }
        Write-Host
    }
}

这是 Config.xml 中的示例:


<Modules>
  <SFModule filename="sfmodule.psm1" varname="SFModule" type="module" sha256="A1B6AE739F733DD8C75AD2778D395953BF2F6EF61B4240B014C446394B87E881" />
  <ETSModule filename="etsmodule.psm1" varname="ETSModule" type="module" sha256="46FD0887DDFBDD88FAECD173F41A448BC57E26CEE6FF40C32500E5994284F47B" />
  <WPFModule filename="wpfmodule.psm1" varname="WPFModule" type="module" sha256="1BEC1B84778148570774AB4E51126A8FB4F2BA308D5BA391E3198805CC13DB2B" />
  <GetInt filename="getint.ps1" varname="getInt" type="script" sha256="FBAF335E80623F26B343939B3D44B9C847388D3ADB351EAF551C8A35D75DF876" />
  <GetDom filename="getdom.ps1" varname="getDom" type="script" sha256="70F4DA69E99DA6157D8DFB60368D65B132504114FF6F6FACDE351FF0C8B8F820" />
  <CopyResult filename="copy_result.ps1" varname="copyResult" type="script" sha256="DCA12BCF8FAC6F52C6E4721DFA2F77FC78183681F6945CB7FCD2010CA94A86C3" />
</Modules>

最佳答案

有效的解决方案:

Import-Module (Join-Path $PSScriptRoot $_.FileName) -Verbose -Global

通过将 -Global 开关添加到 Import-Module cmdlet,它将模块导入全局范围,允许外部脚本访问它们的功能。

关于Powershell 在模块内加载模块 [范围],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50874428/

相关文章:

powershell - 将文件夹大小导出为 CSV

powershell - 未加载 SQLASCmdlets,因为在任何模块目录中均未找到有效的模块文件

Windows 上的 Python mecab 包导入错误 'not defined'

compiler-construction - 为什么每次编译 .f 文件后 .mod 文件都不同

java - Maven - JDBC jar 文件的正确范围是什么?

powershell - 如何定义函数的返回类型/OutputType

Powershell v2 和 PowerShell v3 对象处理

android - 如何对android模块进行单元测试

javascript - 无法从 chrome.runtime.sendMessage 访问响应变量。 (关闭?)

powershell - PowerShell 模块中 Get-Command 的范围