Reputation: 53313
Let me explain with a simple example:
# 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.
But when I remove the Import-AllModulesInside function to the main script, then TestMe is callable.
I want function Import-AllModulesInside to be part of my SetUp tools.
Question: How can I make the imported-modules that were imported by an imported module assessable to the main script?
Upvotes: 0
Views: 157
Reputation: 26170
import-module -scope global should do the trick :)
for PS V2 il would be import-module -global ( http://msdn.microsoft.com/en-us/library/windows/desktop/dd819454.aspx )
Upvotes: 1