Powershell在模块内加载模块[范围]
解决方案:Powershell在模块内加载模块[范围],powershell,module,scope,powershell-4.0,Powershell,Module,Scope,Powershell 4.0,解决方案: 在我的模块中添加-Global开关,使所有功能都可以在自己的上下文之外访问 我创建了一个Powershell模块(ConfigModule.psm1),用于处理我的config.xml 每当我将模块包含到测试脚本中时,它都可以正常工作 我调用名为Import ConfigModules的函数,它为(type=“Module”)导入模块,并从XML中为(type=“script”)创建变量,值为Get Item… 这就是我的负载层次结构: 测试脚本导入ConfigModule.ps
在我的模块中添加
-Global
开关,使所有功能都可以在自己的上下文之外访问
我创建了一个Powershell模块(
ConfigModule.psm1
),用于处理我的config.xml
每当我将模块包含到测试脚本中时,它都可以正常工作
我调用名为Import ConfigModules
的函数,它为(type=“Module”
)导入模块,并从XML
中为(type=“script”
)创建变量,值为Get Item…
这就是我的负载层次结构:
ConfigModule.psm1
ConfigModule.psm1
导入同一目录中的其他模块问题: 调用不同文件中的模块函数时,模块会加载,因为我使用了
-Verbose
开关进行测试,我可以访问变量,但我无法访问函数,因为它们无法识别
这是导入模块范围问题吗
如果是,如何将模块传递到我的测试脚本上下文中?
ConfigModule.psm1>导入配置模块
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
中的一个示例:
有效的解决方案:
导入模块(连接路径$PSScriptRoot$\文件名)-Verbose-Global
通过添加-Global
切换到导入模块
cmdlet,它将模块导入到全局作用域,允许外部脚本访问其功能。这是导入模块作用域的问题吗?据我所知,是的。导入模块
作用域是如何工作的?我在文档页面上看到它将模块加载到当前会话中,因此我假设它是在ConfigModule.psm1
的上下文中加载的,那么我如何将上下文传递到我的测试脚本中?让它正常工作。请随意添加解决方案作为单独的答案,并将其标记为已接受的答案。这就是@Tom,如果我自己解决了这个问题,我想知道如何正确地解决我在这里的问题。0根据文档,从模块内加载模块不是一个好的做法-但仍然是上策,因为它比建议的解决方案更方便,比如混淆嵌套模块清单等:
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
}
}
<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>