Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
acojon opened this issue Feb 5, 2025 · 0 comments

Comments

@acojon
Copy link

acojon commented Feb 5, 2025

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant