2

I am working on a PowerShell script for generating reports, I am loading and calling modules provided by the end user.

First I call a user provided setup function, then load the modules:

# Load user provided modules
($userModules = Get-ChildItem ($userModHome + "*.psm1")) | ForEach-Object { Import-Module (join-path $userModHome $_.Name) } | Out-Null

After calling setup and loading, I loop through the list of modules and call each one. This is all straight forward, but what I would like to accomplish is not requiring global variables for everything that a function would need.

I could require the setup function return an object that contains everything that any function may need, and then pass that data structure in to each module, but that is possibly passing a lot of extra stuff.

I am wondering if there is someway to determine at run-time what to pass to a module, or some other elegant solution to dynamically load and execute modules without using global variables.

In a nutshell I think I am asking if Powershell has reflection to can be used to determine the details of a module and then call. The difficult part I assume is determining what parameters to pass a specific module.

1 Answer 1

1

Not sure to fully understand your question but here are two way to find the module on a computer.

First, you can use the classical $env:PSModulePath to store the place where the client store his module and just put extra code to take appart Microsoft native modules.

Second, if you deliver your own code as a module, you can parameter it as shown here under.

#REQUIRES -Version 2.0
#MyModule.PSM1
param ([strin]$ModulePath, [string] $choix)

switch ($choix)
{
  "developpement" {Import-Module $ModulePath\Modules-0201.psm1 -Verbose}
  "Integration" {Import-Module $ModulePath\Modules-0202.psm1 -Verbose}
  default {Import-Module $ModulePath\Modules-0203.psm1 -Verbose}
}

You can load your module using :

Import-Module .\Mydules.psm1 -ArgumentList "C:\Développements\Pgdvlp_Powershell\Apprentissage","developpement"
COMMENTAIRES : Chargement du module à partir du chemin
« C:\Développements\Pgdvlp_Powershell\Apprentissage\Modules-0201.psm1 ».
COMMENTAIRES : Exportation de la fonction « Bonjour ».
COMMENTAIRES : Importation de la fonction « Bonjour ».
Sign up to request clarification or add additional context in comments.

1 Comment

What I am trying to determine is if there is a way to determine at run time the details of a module and how to call it passing in any parameters it may need. In a nutshell does Powershell have any way to use reflection to determine the details of a module and then call.

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.