PowerShell/Polaris

Exception using Start-Polaris when PowerShell strict mode is enabled.

Meglomax opened this issue · 1 comments

Polaris Bug Report

Description of the bug


When PowerShell's strict mode is enabled the Start-Polaris command throws the following exception:

The variable '$Script:ClassDefinitions' cannot be retrieved because it has not been set.
At C:\Program Files\WindowsPowerShell\Modules\Polaris\0.2.0\lib\Polaris.Class.ps1:15 char:33

Steps to reproduce

Steps to reproduce the behavior:

  1. Import-Module Polaris
  2. Set-StrictMode -Version Latest
  3. Start-Polaris
  4. See error.

Expected behavior

Polaris should not throw exceptions in strict mode - at least when these are not related to the Polaris module itself. Using PowerShell's strict mode is good practice for identifying bugs and scripting issues that can otherwise by difficult and time consuming to track down

Verbose output of the script

There is no additional information obtained by setting the Verbose preference. The exception is thrown before any Write-Verbose statements are encountered.

Additional context

The issue is caused by the following line, within the parameter declarations, in Polaris.Class.ps1:

    [string]$ClassDefinitions = $Script:ClassDefinitions

$Script:ClassDefinitions is undefined at the time the parameter declaration is encountered, and therefore in strict mode, this will throw an exception.

It is possible to workaround this issue by using the following script line before calling Start-Polaris:

$Script:ClassDefinitions = [string]::Empty

Having declared the variable, strict mode will no longer cause the command to throw an exception.

Not sure why the parameter in the reference script uses an undeclared variable as a default value. I would suggest a quick fix would be to declare the variable early in the execution lifecycle of the module, perhaps in the .psm1 file, before any command that relies on its value can be invoked.

Version Information


Polaris 0.2.0

NameVersion
Polaris0.2.0
PSVersionPSEditionPSCompatibleVersionsBuildVersionCLRVersionWSManStackVersionPSRemotingProtocolVersionSerializationVersion
5.1.18362.145Desktop1.0.-1.-1, 2.0.-1.-1, 3.0.-1.-1, 4.0.-1.-1, 5.0.-1.-1, 5.1.18362.14510.0.18362.1454.0.30319.420003.02.31.1.0.1

Thanks for the bug report. At first glance, looks like an unused variable. We might be able to just delete that line completely.