powershell - Powershell 在模块内加载模块 [范围]
问题描述
解决方案:
Import-Module (Join-Path $PSScriptRoot $_.FileName) -Verbose -Global
在我的模块中添加-Global
开关,使所有功能都可以在它自己的上下文之外访问。
我有一个我创建的 Powershell 模块 ( ConfigModule.psm1
),我曾经使用我的config.xml
.
每当我将模块包含到测试脚本中时,它都可以正常工作。
我调用了一个名为的函数Import-ConfigModules
,它为 ( ) 导入模块,并从值为 的 中为 ( type="Module"
) 创建变量。type="script"
XML
Get-Item...
所以这是我的负载层次结构:
- 测试脚本导入
ConfigModule.psm1
ConfigModule.psm1
在同一目录中导入其他模块- 其他模块具有不同的功能。
问题:
在不同文件中调用模块函数时,模块会在我使用-Verbose
switch 进行测试时加载,我可以访问变量但我无法访问函数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 (Join-Path $PSScriptRoot $_.FileName) -Verbose -Global
通过向 cmdlet 添加-Global
开关Import-Module
,它将模块导入全局范围,允许外部脚本访问其功能。
推荐阅读
- castle-windsor - 向 Castle Windsor 注册动态加载的 DLL
- php - 如何检查链接并更改它们?
- angularjs - Http 和范围没有从 mongodb 获取数据
- ios - 类型“NSAttributedStringKey”(又名“NSString”)没有成员“baselineOffset”
- sql - 在我的网站首页上为会员创建一个下拉列表
- reactjs - ReactDOM 将带参数的函数传递给子组件
- android - StackOverflowError 导致 ConstraintLayout 崩溃
- javascript - 如何使用geofirestore查询设置半径内的所有地理查询?
- android - 获取 java.lang.IndexOutOfBoundsException: Index: 3, Size: 3 同时从 firebaseui recyclerview 删除项目
- html - 背景图像在手机上显示奇怪