iainbrighton / PScribo

PowerShell documentation framework
MIT License
230 stars 35 forks source link

[VariableIsUndefined] Error in Word document output #129

Open rebelinux opened 5 months ago

rebelinux commented 5 months ago

Hello,

There is a sporadic error when generating a word document. At first I thought it was related to AsBuiltReport modules but it happened to me in another project not related to Abr.

The error occurs at the end of the script execution and the only way I find to fix it is to:

  1. Relaunch the powershell session
  2. Disable the strict mode: Set-StrictMode -Off
  3. Remove any reference of the Image cmdlet in my script

Error:

PS C:\Users\jocolon> $error
New-AsBuiltReport : The variable '$Images' cannot be retrieved because it has not been set.                                                                                           
At line:1 char:1
+ New-AsBuiltReport -Report Veeam.VB365 -Target veeam-vbo-01v.pharmax.l ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,New-AsBuiltReport

The variable '$Images' cannot be retrieved because it has not been set.
At line:58 char:21
+                     $Images += @($subSection)
+                     ~~~~~~~
    + CategoryInfo          : InvalidOperation: (Images:String) [], RuntimeException
    + FullyQualifiedErrorId : VariableIsUndefined

PS C:\Users\jocolon>

Reference Code:

https://github.com/iainbrighton/PScribo/blob/9382efe46d11a3689b1b6367f0f22ca317d6239d/Src/Plugins/Word/Get-WordDocument.ps1#L62

Environment:

PS C:\Users\jocolon> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.19041.3803
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.3803
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

PS C:\Users\jocolon>

PScribo version:

PS C:\Users\jocolon> get-module pscribo -ListAvailable

    Directory: C:\Program Files\WindowsPowerShell\Modules

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     0.10.0     PScribo                             {BlankLine, Document, DocumentOption, Export-Document...}

PS C:\Users\jocolon>