4

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>
5
  • 2
    Is this an import-module scope issue? As far as I can see, yes. Commented Jun 15, 2018 at 11:35
  • How does the Import-Module scope work? I've read on the documentation page that it loads the module into the current session, so I presume that it's loaded in the context of ConfigModule.psm1, so how do I pass the context into my test script? Commented Jun 15, 2018 at 12:33
  • Got it to work. Commented Jun 15, 2018 at 12:50
  • Feel free to add your solution as a separate answer and mark it as accepted answer. Commented Jun 15, 2018 at 13:01
  • That's @Tom, I was wondering how to properly resolve my question on here if I solved it myself. Commented Jun 15, 2018 at 13:01

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

0 According to the docs, loading modules from within modules is not a good practice - but still upvoted since it's more convenient than the proposed solution of messing around with nested modules manifests, etc: learn.microsoft.com/en-us/powershell/module/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.