I created a PowerShell module MyUtil.psm1 and a manifest file MyUtil.psd1 for it. In the psd1 file it has the prefix defined to prevent name conflicts for exported functions:
DefaultCommandPrefix = 'MyToolbox'
This way, after running Import-Module .\MyUtil.psd1
, a function like Get-Command
in the psm1 file will be Get-MyToolboxCommand
, everything is fine. But if someone runs Import-Module .\MyUtil.psm1
to import the psm1 file directly, the psd1 file is simply not used and the prefix I want won't be applied.
If I want to prevent this (importing MyUtil.psm1 directly), is there a way to ONLY allow importing the corresponding psd1 manifest file instead of the psm1 file? Or a programmatic way to detect that this module was not imported through psd1 so I can warn the user to use psd1?
You can use:
#Requires
For example:
Require that Hyper-V (version 1.1 or greater) is installed.
Requires that Hyper-V (only version 1.1) is installed.
Requires that any version of PSScheduledJob and PSWorkflow, is installed.
About Requires