Why I cannot see an function that is part of an imported module's imported module in Powershell?

StackOverflow https://stackoverflow.com/questions/13719848

質問

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?

役に立ちましたか?

解決

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 )

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top