Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test for existence of a script-scoped variable in PowerShell?

Tags:

powershell

Is it possible to test for the existence of a script-scoped variable in PowerShell?

I've been using the PowerShell Community Extensions (PSCX) but I've noticed that if you import the module while Set-PSDebug -Strict is set, an error is produced:

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

While investigating how I might 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 straight forward code; if the cache doesn't exist or needs to be refreshed, create a new, empty cache. The problem is that calling $SCRIPT:helpCache while Set-PSDebug -Strict is in force casues the error because the variable hasn't been defined yet.

Ideally, we could use a Test-Variable cmdlet but such a thing doesn't exist! I thought about looking in the variable: provider but I don't know how to determine the scope of a variable.

So my question is: how can I test for the existence of a variable while Set-PSDebug -Strict is in force, without causing an error?

like image 237
Damian Powell Avatar asked May 04 '10 08:05

Damian Powell


1 Answers

Use test-path variable:SCRIPT:helpCache

if (!(test-path variable:script:helpCache)) {
  $script:helpCache = @{}
}

This works for me without problems. Checked using this code:

@'
Set-PsDebug -strict
write-host (test-path variable:script:helpCache)
$script:helpCache = "this is test"
write-host (test-path variable:script:helpCache) and value is $script:helpCache
'@ | set-content stricttest.ps1

.\stricttest.ps1
like image 194
stej Avatar answered Sep 18 '22 23:09

stej