在PowerShell

时间:2016-08-26 09:16:00

标签: powershell powershell-module

当使用Import-Module导入模块时,我希望该模块在任何执行Import-Module(被调用者)的上下文中为源提供一组脚本(如果存在) - 而不是在模块的上下文中。目前尚不清楚如何做到这一点。

有一个" ScriptsToProcess"模块清单的一部分,其中包含在在被调用者范围内加载模块之前运行的脚本,但是在加载模块之后似乎没有等效项。有没有直接的方法来实现这种行为?

以下是psm1文件中的一个示例,该文件无法正常工作,因为它是点源到模块的范围而不是点源到被调用者的范围(通常在您执行Import-Module的位置)。

# Define plugin location
$PluginLocation = Join-Path (Split-Path $profile) PluginScripts

if ($PSIse) {
# If plugin location does not exist tell the user.
if (-not (Test-Path $PluginLocation) )  {
    Write-Host -ForegroundColor Green "There is no plugin directory $PluginLocation if you wish to automatically load plugin scripts please create this directory and place the scripts there."
} else {
    Write-Host "Looking for scripts in $PluginLocation"
    Get-ChildItem $PluginLocation -Recurse -Filter *.ps1 | % { 
        Write-Host "Sourcing $($_.Name)"
        . $_.FullName 
    } 
}
}

0 个答案:

没有答案