【问题标题】:Powershell loading modules inside of a module [Scope]Powershell 在模块内加载模块 [范围]
【发布时间】:2018-11-25 05:33:11
【问题描述】:

解决方案:


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

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


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

每当我将模块包含到测试脚本中时,它都可以正常工作。

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

这是我的负载层次结构:

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

问题:

在不同文件中调用模块函数时,模块会加载,因为我使用-Verbose 开关进行测试,我可以访问变量但 我无法访问函数,因为它们是 not recognized .

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

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


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 范围如何工作?我在文档页面上读到它会将模块加载到当前会话中,所以我假设它是在ConfigModule.psm1 的上下文中加载的,那么如何将上下文传递到我的测试脚本中?
  • 让它工作。
  • 随意将您的解决方案添加为单独的答案并将其标记为已接受的答案。
  • 那是@Tom,如果我自己解决了,我想知道如何正确解决我的问题。

标签: powershell module scope powershell-4.0


【解决方案1】:

有效的解决方案:

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-04
  • 2011-05-13
  • 2017-09-22
  • 1970-01-01
  • 2014-10-03
  • 2023-03-20
相关资源
最近更新 更多