让我用一个简单的例子来解释:
# 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成为我的SetUp工具的一部分。
问题: 如何使导入模块导入的导入模块可以评估为主脚本?
答案 0 :(得分:1)
import-module -scope global应该做的诀窍:)
对于PS V2 il,将是import-module -global(http://msdn.microsoft.com/en-us/library/windows/desktop/dd819454.aspx)