RickB
2008-10-02 12:54:40 UTC
While debugging a problem that I knew was scope related I created this
function.
It's been so useful I thought it worth posting.
It shows the value of variables at different scopes.
######################################################################
#
# MYVARS
#
# Displays *ALL* Non-AUTOMATIC PS Variables in a scope range.
# With the -del option it cleans up the variables in that range.
# Requires Get-MaxScopeID posted by Kiron 20080917.
#
######################################################################
function MyVars([int]$minScope=0,
[int]$maxScope=$(Get-MaxScopeID),
[switch]$del) {
$ScopeLimit = (Get-MaxScopeID)
if ($minScope -gt $maxScope){throw "Myvars [<min>] [<max>]"}
"Maximum Scope range specification is 0..$($ScopeLimit - 1)"
if ($minScope -lt 0 -or $maxScope -lt 0){throw "Scope out of range."}
$maxScope++ #These offsets are to account
$minScope++ #for $this scope which goes away
if ($maxScope -gt $ScopeLImit){$maxScope = $ScopeLimit}
if ($minScope -gt $maxScope) {$minScope = $maxScope}
$minScope..$maxScope|%{Get-Variable -scope $_|
Select-Object Name,Value|
Add-Member noteproperty 'Scope' ($_ - 1) -
passthru}|
?{$_.name -notmatch (
"^(Co(?:mmandLineParameters|n(?:(?:firmPreferenc|soleFileNam)e))|
DebugPref" +
"erence|E(?:rror(?:ActionPreference|View)?|xecutionContext)|
FormatEnumerat" +
"ionLimit|H(?:OME|ost)|LASTEXITCODE|M(?:aximum(?:(?:Alias|Drive|
Error|Func" +
"tion|History|Variable)Count)|yInvocation)|NestedPromptLevel|
OutputEncodin" +
"g|P(?:ID|ROFILE|S(?:C(?:mdlet|ulture)|HOME|(?:UICultur|
VersionTabl)e)|WD|" +
"rogressPreference)|ReportErrorShow(?:ExceptionClass|InnerException|
S(?:(?" +
":our|tackTra)ce))|S(?:hellId|tackTrace)|VerbosePreference|W(?:
(?:arning|h" +
"atIf)Preference)|args|false|input|null|pattern|true|_|\^|\$|\?|\$)
$"
)}|
Sort-Object -property scope,name|
%{if ($del) {Remove-Variable $_.Name -scope ($_.Scope + 1)
"Deleted $($_.name)"}else{$_}}|
Format-Table Scope,Name,Value -a
}
######################################################################
#
# Get-MaxScopeID
#
# Returns the numeric -scope value currently equivalent to 'global'
# Posted by iron 20080917 on microsoft.public.windows.powershell NG
#
######################################################################
function Get-MaxScopeID () {
# -2 below accounts for this function and its foreach scopes which go
away
trap {return ([int]$id - 2)}
foreach ($id in 0..100) {
gv -s $id >$null
}
}
function.
It's been so useful I thought it worth posting.
It shows the value of variables at different scopes.
######################################################################
#
# MYVARS
#
# Displays *ALL* Non-AUTOMATIC PS Variables in a scope range.
# With the -del option it cleans up the variables in that range.
# Requires Get-MaxScopeID posted by Kiron 20080917.
#
######################################################################
function MyVars([int]$minScope=0,
[int]$maxScope=$(Get-MaxScopeID),
[switch]$del) {
$ScopeLimit = (Get-MaxScopeID)
if ($minScope -gt $maxScope){throw "Myvars [<min>] [<max>]"}
"Maximum Scope range specification is 0..$($ScopeLimit - 1)"
if ($minScope -lt 0 -or $maxScope -lt 0){throw "Scope out of range."}
$maxScope++ #These offsets are to account
$minScope++ #for $this scope which goes away
if ($maxScope -gt $ScopeLImit){$maxScope = $ScopeLimit}
if ($minScope -gt $maxScope) {$minScope = $maxScope}
$minScope..$maxScope|%{Get-Variable -scope $_|
Select-Object Name,Value|
Add-Member noteproperty 'Scope' ($_ - 1) -
passthru}|
?{$_.name -notmatch (
"^(Co(?:mmandLineParameters|n(?:(?:firmPreferenc|soleFileNam)e))|
DebugPref" +
"erence|E(?:rror(?:ActionPreference|View)?|xecutionContext)|
FormatEnumerat" +
"ionLimit|H(?:OME|ost)|LASTEXITCODE|M(?:aximum(?:(?:Alias|Drive|
Error|Func" +
"tion|History|Variable)Count)|yInvocation)|NestedPromptLevel|
OutputEncodin" +
"g|P(?:ID|ROFILE|S(?:C(?:mdlet|ulture)|HOME|(?:UICultur|
VersionTabl)e)|WD|" +
"rogressPreference)|ReportErrorShow(?:ExceptionClass|InnerException|
S(?:(?" +
":our|tackTra)ce))|S(?:hellId|tackTrace)|VerbosePreference|W(?:
(?:arning|h" +
"atIf)Preference)|args|false|input|null|pattern|true|_|\^|\$|\?|\$)
$"
)}|
Sort-Object -property scope,name|
%{if ($del) {Remove-Variable $_.Name -scope ($_.Scope + 1)
"Deleted $($_.name)"}else{$_}}|
Format-Table Scope,Name,Value -a
}
######################################################################
#
# Get-MaxScopeID
#
# Returns the numeric -scope value currently equivalent to 'global'
# Posted by iron 20080917 on microsoft.public.windows.powershell NG
#
######################################################################
function Get-MaxScopeID () {
# -2 below accounts for this function and its foreach scopes which go
away
trap {return ([int]$id - 2)}
foreach ($id in 0..100) {
gv -s $id >$null
}
}