Set strict mode in powershell for all modules

I currently use the following structure in my scripts:

HighLevel.ps1 LowLevelModule1.psm1 LowLevelModule2.psm1 ... 

Now I set strict mode in the ps1 file to get at least some type safety at runtime. Unfortunately, cllaing Set-StrictMode -Version Latest will enable strict mode only for the current area and all child areas (this is by design. Proof: https://technet.microsoft.com/en-us/library/hh849692.aspx ).

As far as I understand the current PS architecture, changing strict mode in the ps1 file changes the script-level configuration. But modules have their own script scope, so modules do not inherit parent rules.

As a workaround, I can put Set-StrictMode in every PSM1 file, but this is not very suitable, because I could not allow the client of my module to decide whether to enable strict mode or not.

The same problem exists for the configuration of $VerbosePreferences . This configuration is also included for each scope, so I have to spread this information across the borders of the modules on how to do this.

Any suggestions on how to change strict mode and detailed preferences globally?

PS Changing the $ profile for these purposes is not an option.

+6
source share
1 answer

If these are modules that you write yourself, I suggest you set the strict mode that you want in the module. It will only apply to code inside the module, so the setting for the code outside the module set by the caller does not matter.

If these are modules written by others, then you do not want to change strict mode (or its absence) on this code, because it has not been tested with strict mode and could violate this.

+3
source

Source: https://habr.com/ru/post/989236/


All Articles