Currently I'm using following structure in my scripts:
HighLevel.ps1
LowLevelModule1.psm1
LowLevelModule2.psm1
...
Now, I'm setting strict mode in ps1 file to get at least some type-safety during runtime. Unfortunately, cllaing Set-StrictMode -Version Latest
will enable strict mode only for current scope and all child scopes (this is by design. Proof: https://technet.microsoft.com/en-us/library/hh849692.aspx).
As far as I understand current PS architecture, changing strict mode in ps1 file changes script level configuration. But modules has its own script scope so modules does not inherit parents rules.
As a workaround I can put Set-StrictMode
to every PSM1 files, but it seems not very best approach because I would not be able to give a chance to the client of my module to decide, turn on strict mode or not.
The same issue exists for $VerbosePreferences
configuration. This configuration is also enabled per scope, so I have to propagate this information across module boundaries some how.
Any suggestions how to change strict mode and verbose preference globally?
P.S. Changing $profile for this purposes is not an option.
The Set-StrictMode cmdlet configures strict mode for the current scope and all child scopes, and turns it on and off. When strict mode is on, PowerShell generates a terminating error when the content of an expression, script, or script block violates basic best-practice coding rules.
A module is a package that contains PowerShell members, such as cmdlets, providers, functions, workflows, variables, and aliases. People who write commands can use modules to organize their commands and share them with others.
If these are modules you are writing yourself, then I suggest that you set the strict mode that you want inside the module. It will only apply to the code inside the module, so the setting for code outside the module, set by the caller, is irrelevant.
If these are modules written by others, then you do not want to change the strict mode (or lack thereof) on that code, because it was not tested with strict mode on and could break by doing so.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With