René Nyffenegger
René Nyffenegger

Reputation: 40499

Making a module fail on purpose when trying to import it

Is there a canonical way to make a module fail when it is tried to be imported with import-module, for example because a specific precondition is not met.

Currently, I have the following three lines at the start of the module:

if ( [IntPtr]::Size -ne 4 ) {
    throw '32 bit environment required' 
}

I am wondering if there is a better (or more agreed upon way) to prevent using a module in a 64-bit environment.

Upvotes: 1

Views: 35

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174545

I am wondering if there is a better (or more agreed upon way) to prevent using a module in a 64-bit environment.

PowerShell's Module Manifest schema has a configuration element for just this - ProcessorArchitecture!

New-ModuleManifest -Path My32BitOnlyModule.psd1 -ProcessorArchitecture x86

Or, if you need to add to an existing module manifest:

@{
  # ...
  ProcessorArchitecture = 'x86'
  # ...
}

For 64-bit architectures, the label is Amd64

Upvotes: 1

Related Questions