Search code examples
powershellmodulescopepowershell-4.0

Powershell loading modules inside of a module [Scope]


Solution:


Import-Module (Join-Path $PSScriptRoot $_.FileName) -Verbose -Global

Adding -Global switch in my module, made all functions to be accessed outside of it's own context.


I have a Powershell module (ConfigModule.psm1) I have created which I used to work with my config.xml.

It all works fine whenever I include the module into a test script.

I invoke a function named Import-ConfigModules and it imports modules for (type="Module") and creates variables for (type="script") from the XML with the value of Get-Item....

So this is my load hierarchy:

  1. Test Script imports ConfigModule.psm1
  2. ConfigModule.psm1 imports other modules within the same directory
  3. Other modules have different functionality.

Problem:

Upon invoking the module functions in a different file, the modules do load as I used -Verbose switch to test, I can access the variables but I cannot access functions as they are not recognized.

Is this an import-module scope issue?

If so how to pass the modules into my test script context?


ConfigModule.psm1 > Import-ConfigModules

Function Import-ConfigModules {
    param([String]$Path)
    $modules = Get-ConfigModules -Path $Path

    # Iterate by FileType
    $modules.Keys | % {

        switch($modules[$_]) {
            {$_.FileType -eq "module"} {
                # If Module exists, load, else find similar modules=
                if(Test-Path (Join-Path $PSScriptRoot $_.FileName)) {

                    Import-Module (Join-Path $PSScriptRoot $_.FileName) -Verbose
                    # Test if module was loaded
                    if(Get-Module $_.VariableName) {
                        Write-Host "Module [Loaded]: " ($_.FileName) -ForegroundColor Green
                    }
                    else {
                        Write-Host "Module [Error]: Could not import module - " ($_.FileName) -ForegroundColor Green
                    }

                }
                else {
                    Write-Host "Module [Missing]: $($_.FileName).`r`nFound similar: " -ForegroundColor Yellow -NoNewline
                    Get-SimilarFile `
                        -Path $PSScriptRoot `
                        -Name $_.FileName
                }
                break
            }
            {$_.FileType -eq "script"} {
                if(Test-Path (Join-Path $PSScriptRoot $_.FileName)) {
                    Write-Host "Script [Loaded]: " ($_.FileName) -ForegroundColor Green

                    # Create variables dynamically
                    Set-Variable -Name "$($_.VariableName)" -Scope Global -Value (Get-Item -Path (Join-Path $PSScriptRoot $_.FileName))
                }
                else {
                    Write-Host "Script [Missing]: $($_.FileName).`r`nFound similar: " -ForegroundColor Yellow -NoNewline
                    Get-SimilarFile `
                        -Path $PSScriptRoot `
                        -Name $_.FileName
                }
                break
            }
            default { Write-Warning "$($_.FileName) : Bad FileType definition. {FileType: $($_.FileType)}" }
        }
        Write-Host
    }
}

Here's a sample from Config.xml :


<Modules>
  <SFModule filename="sfmodule.psm1" varname="SFModule" type="module" sha256="A1B6AE739F733DD8C75AD2778D395953BF2F6EF61B4240B014C446394B87E881" />
  <ETSModule filename="etsmodule.psm1" varname="ETSModule" type="module" sha256="46FD0887DDFBDD88FAECD173F41A448BC57E26CEE6FF40C32500E5994284F47B" />
  <WPFModule filename="wpfmodule.psm1" varname="WPFModule" type="module" sha256="1BEC1B84778148570774AB4E51126A8FB4F2BA308D5BA391E3198805CC13DB2B" />
  <GetInt filename="getint.ps1" varname="getInt" type="script" sha256="FBAF335E80623F26B343939B3D44B9C847388D3ADB351EAF551C8A35D75DF876" />
  <GetDom filename="getdom.ps1" varname="getDom" type="script" sha256="70F4DA69E99DA6157D8DFB60368D65B132504114FF6F6FACDE351FF0C8B8F820" />
  <CopyResult filename="copy_result.ps1" varname="copyResult" type="script" sha256="DCA12BCF8FAC6F52C6E4721DFA2F77FC78183681F6945CB7FCD2010CA94A86C3" />
</Modules>

Solution

  • The solution that worked:

    Import-Module (Join-Path $PSScriptRoot $_.FileName) -Verbose -Global

    By adding -Global switch to Import-Module cmdlet, it imports the module into the global scope, allowing outside scripts to access their functionality.