Author Posts

May 4, 2017 at 3:50 pm

I have a module that creates a disconnected session to perform a task and provides functions to interact with it. As it may be run from multiple places, obviously I need to ensure the version of the module is the same that originally called the task, otherwise that it is the latest version.

This ideally needs to be checked by the module as it is loaded however whatever I've tried, Get-Module returns nothing if called while the module is loading. I understand this is probably because the module isn't loaded yet so there's nothing to get but I don't want to resort to duplicating the version in the module itself to keep it admin-friendly, any suggestions?

The "ScriptsToProcess" property of the Module Manifest also returns nothing, the Manifest itself is a hash table so doesn't have access to itself while being built (I think), not sure where else to go.

May 4, 2017 at 4:54 pm

You can access the module version via $MyInvocation.MyCommand.Module.Version from any function inside your module. The statement must be in a function and can only be invoked once the module is fully imported. You can't access the information during import, e.g. ScriptsToProcess or the PSM1 file.

If you know you've only one version of the module installed you could also use below which would work during ScriptsToProcess or the PSM1 file.

$module = Get-Module -ListAvailable -Name MyModuleName
$module.Version

May 5, 2017 at 2:46 pm

$MyInvocation won't help as I need to know as the module is loaded. The commands are rather complex, involving a number of parameters and some time-sensitivity so only finding out when you go to run a command isn't acceptable.

Get-Module -ListAvailable does the trick perfectly, thank you! Yes only one version should ever be installed and will be updated when required.