I Need Some Introspection because there are many versions of the same module name floating around in different directories and PSRepositories and the running PS script needs to know which version is currently running.
Given a boilerplate powershell .\MyModule\MyModule.psd1
manifest and .\MyModule\MyModule.psm1
module, how does one reference the values in MyModule.psd1
from within MyModule.psm1
?
# MyModule.psd1:
@{
# Script module or binary module file associated with this manifest.
RootModule = 'MyModule.psm1'
# Version number of this module.
ModuleVersion = '2020.1.12.1611'
# Author of this module
Author = 'MeMyselfI'
}
And the module MyModule.psm1:
#MyModule.psm1
write-host "$RootModule VERSION: $ModuleVersion by $Author"
write-host "For help, please see $HelpInfoURI"
write-host "$PrivateData[PSData].Tags"
How does one get the value of MyModule.psd1
hash entry constants such as RootModule
, ModuleVersion
, HelpInfoURI
, Path
, and even $PrivateData[$PSData].Tags
from within the corresponding MyModule.psm1
? Introspection in PowerShell must be easy, but my module does not know what it is, help it find itself:)
You simply need to use Get-Module
along with the name of your module and from there, you have access to all those values.
$ModuleInfos = Get-Module -Name MyModule
write-host "$($ModuleInfos.RootModule) VERSION: $($ModuleInfos.Version) by $($ModuleInfos.Author)"
write-host "For help, please see $($ModuleInfos.HelpInfoUri)"
write-host $ModuleInfos.PrivateData.PSData.Item('Tags')
You can also do it from any Powershell console window from anywhere provided your module is already imported (From within the psm1, you do not need to import the module though).
Here is an alternative method to achieve the same thing (from within the psm1 file)
$ModuleInfos = Import-PowerShellDataFile -Path "$PsScriptRoot\MyModule.psd1"