0

让我用一个简单的例子来解释:

# TestModule.psm1 content 
# which is D:\Projects\(...)\MyProject\\bin\Debug\Modules directory :
function TestMe
{
 Write-Output "TestMe is called!"
}


# SetUpTools.psm1 content 
# which is D:\Projects\(...)\MyProject\\bin\Debug directory :
function Import-AllModulesInside ([string]$path = $(throw "You must specify a path where to import the contents"))
{

    if ( $(Test-Path $path)-eq $false){
        throw "The path to use for importing modules is not valid: $path"}


        # Import all modules in the specified path
        dir ($path | where {!$_.PsIsContainer} )| %{
        $moduleName = $($path + "\" + $_.name)

        import-module "$moduleName"
        Write-Output "importing $moduleName"

        }
}


#MainScript.ps1 content which is D:\Projects\(...)\MyProject\\bin\Debug directory :

# Config values are loaded in the begining
# (......)

# Import SetUpTools.psm1:

 Import-Module SetUpTools.psm1 


# Gets the modules directory's full path which I have loaded before..
$modulesPath = $(Get-ScriptDirectory) + $appSettings["ModulesFullPath"]


Write-Output ($modulesPath) # which writes : D:\Projects\(...)\MyProject\\bin\Debug\Modules

Import-AllModulesInside $modulesPath #Calls the method in SetUpTools.psm1

# I expect TestModule function to be available now:

TestMe  # But PowerShell does not recognize this function as I have not imported it in the main script.

但是当我将 Import-AllModulesInside 函数删除到主脚本时,TestMe 是可调用的。

我希望函数 Import-AllModulesInside 成为我的设置工具的一部分。

问题: 如何使导入模块导入的导入模块可评估到主脚本?

4

1 回答 1

1

import-module -scope global 应该可以解决问题:)

对于 PS V2 il 将是 import-module -global ( http://msdn.microsoft.com/en-us/library/windows/desktop/dd819454.aspx )

于 2012-12-05T11:37:30.643 回答