0
讓我用一個簡單的例子來說明:爲什麼我在Powershell中看不到導入模塊導入模塊的一部分功能?
# 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.
但是當我刪除了進口AllModulesInside功能主腳本,然後TESTME是可調用。
我希望函數Import-AllModulesInside成爲我的SetUp工具的一部分。
問題: 如何使導入的模塊可以通過導入的模塊導入到主腳本中?
這樣做的原因是,你通常導入模塊做你關心的東西,但你不希望它的內部實現(比如它使用了什麼其他模塊)遍佈你的powershell會話 – JohnL