Can I check for the existence of a script -scoped variable in PowerShell?
I used PowerShell Community Extensions (PSCX) , but I noticed that if you import a module and Set-PSDebug -Strict - set an error occurs:
The variable '$SCRIPT:helpCache' cannot be retrieved because it has not been set. At C:\Users\...\Modules\Pscx\Modules\GetHelp\Pscx.GetHelp.psm1:5 char:24
When researching how I can fix this, I found this piece of code in Pscx.GetHelp.psm1:
#requires -version 2.0 param([string[]]$PreCacheList) if ((!$SCRIPT:helpCache) -or $RefreshCache) { $SCRIPT:helpCache = @{} }
This is pretty simple code; if the cache does not exist or needs updating, create a new empty cache. The problem is that calling $SCRIPT:helpCache while Set-PSDebug -Strict is valid causes an error because the variable is not yet defined.
Ideally, we could use the Test-Variable cmdlet, but such a thing does not exist! I was thinking about searching in variable: provider, but I don't know how to determine the scope of a variable.
So my question is: how can I check for a variable while Set-PSDebug -Strict without causing an error?
powershell
Damian powder
source share