Skip to content

When loading PureStoragePowerShellSDK2 Module while using Set-StrictMode -Version Latest, fails with $Pfa2Directory error #28

Open
@acojon

Description

@acojon

Hello,

I'm using Version 2.33.187 of the PureStoragePowerShellSDK2 module. When using a script with Set-StrictMode -Version Latest and setting the $ErrorActionPreference to "stop" the PureStoragePowerShellSDK2 module fails to load with the following error message:

InvalidOperation: C:\Program Files\WindowsPowerShell\Modules\PureStoragePowerShellSDK2\2.33.187\Repair-Pfa2AsemblyBinding.ps1:2
Line |
   2 |  if (!$Pfa2Directory) {
     |       ~~~~~~~~~~~~~~
     | The variable '$Pfa2Directory' cannot be retrieved because it has not been set.

Here are the steps I use to recreate the issue:

Start Powershell

Set-strictMode -version latest
$ErrorActionPreference = "stop"
Import-Module PureStoragePowerShellSDK2

With a little bit of investigation, I've traced the error to the Repair-Pfa2AsemblyBinding.ps1 script that is called when the module is loaded using Import-Module. Here is the code:

# Default the Pfa2Directory to the directory of the entry script for the task.
if (!$Pfa2Directory) {
	$Pfa2Directory = [System.IO.Path]::GetFullPath("$PSScriptRoot")
	Write-Verbose "Defaulted PFA2 directory to: '$Pfa2Directory'"
}

When Set-StrictMode -Version Latest has been set in a script, the script requires that the variable has been created before using the variable in a comparison. The script is erroring out on the IF statement on line 2.

If you change line 2 to look like this:

if (!(Test-Path -Path Variable:global:Pfa2Directory)) {

Then when using Import-Module the script will run in both normal, and strict mode.

Looking at line 10 and 17, these checks are using Test-Path to see if the variable exists.

Would it be possible to have someone update line 2 so that it uses Test-Path and allows the module to be imported for scripts that have turned on Strict mode?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions