Discussion:
Something that might be useful
(too old to reply)
RickB
2008-10-02 12:54:40 UTC
Permalink
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
}
}
RickB
2008-10-02 13:05:42 UTC
Permalink
Post by RickB
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.
See un-mangeled version here...
http://cid-bb10621fcfe1bcf8.spaces.live.com/blog/cns!BB10621FCFE1BCF8!126.entry
Marco Shaw [MVP]
2008-10-02 13:23:18 UTC
Permalink
Post by RickB
See un-mangeled version here...
http://cid-bb10621fcfe1bcf8.spaces.live.com/blog/cns!BB10621FCFE1BCF8!126.entry
Introducing: www.poshcode.org

Share it!
--
*Microsoft MVP - Windows Server - Admin Frameworks
https://mvp.support.microsoft.com/profile/Marco.Shaw
*PowerShell Co-Community Director - http://www.powershellcommunity.org
*Blog - http://marcoshaw.blogspot.com
OldDog
2008-10-02 16:23:14 UTC
Permalink
Post by RickB
See un-mangeled version here...
http://cid-bb10621fcfe1bcf8.spaces.live.com/blog/cns!BB10621FCFE1BCF8...
Introducing:www.poshcode.org
Share it!
--
*Microsoft MVP - Windows Server - Admin Frameworkshttps://mvp.support.microsoft.com/profile/Marco.Shaw
*PowerShell Co-Community Director -http://www.powershellcommunity.org
*Blog -http://marcoshaw.blogspot.com
Very interesting;

However, when I run it I get this...

ERROR: Get-Variable : The scope number '4' exceeds the number of
active scopes.
ERROR: Parameter name: scopeID
ERROR: Actual value was 4.
ERROR: At line:65 char:5
ERROR: + gv <<<< -s $id >$null
ERROR: Get-Variable : The scope number '4' exceeds the number of
active scopes.
ERROR: Parameter name: scopeID
ERROR: Actual value was 4.
ERROR: At line:65 char:5
ERROR: + gv <<<< -s $id >$null
Maximum Scope range specification is 0..1

Then The information I would expect. Should I just ignore it or is
there something I should fix?

OldDog
RickB
2008-10-07 13:47:58 UTC
Permalink
Post by OldDog
Post by RickB
See un-mangeled version here...
http://cid-bb10621fcfe1bcf8.spaces.live.com/blog/cns!BB10621FCFE1BCF8...
Introducing:www.poshcode.org
Share it!
--
*Microsoft MVP - Windows Server - Admin Frameworkshttps://mvp.support.microsoft.com/profile/Marco.Shaw
*PowerShell Co-Community Director -http://www.powershellcommunity.org
*Blog -http://marcoshaw.blogspot.com
Very interesting;
However, when I run it I get this...
ERROR: Get-Variable : The scope number '4' exceeds the number of
active scopes.
ERROR: Parameter name: scopeID
ERROR: Actual value was 4.
ERROR: At line:65 char:5
ERROR: +   gv  <<<< -s $id >$null
ERROR: Get-Variable : The scope number '4' exceeds the number of
active scopes.
ERROR: Parameter name: scopeID
ERROR: Actual value was 4.
ERROR: At line:65 char:5
ERROR: +   gv  <<<< -s $id >$null
Maximum Scope range specification is 0..1
Then The information I would expect. Should I just ignore it or is
there something I should fix?
OldDog
Very strange. Is that under 1.0 or CTP?
I'm not sure how to reproduce it.
One of these settings is likely to blame

$DebugPreference SilentlyContinue
$ErrorActionPreference Continue
$ErrorView NormalView
$ProgressPreference Continue
$VerbosePreference SilentlyContinue
$WarningPreference Continue

Are you using anything besides these defaults.

Meanwhile it is safe to ignore them.
The error is being caused on purpose and is
supposed to be trapped and suppressed.
OldDog
2008-10-07 14:14:01 UTC
Permalink
Post by OldDog
Post by RickB
See un-mangeled version here...
http://cid-bb10621fcfe1bcf8.spaces.live.com/blog/cns!BB10621FCFE1BCF8...
Introducing:www.poshcode.org
Share it!
--
*Microsoft MVP - Windows Server - Admin Frameworkshttps://mvp.support.microsoft.com/profile/Marco.Shaw
*PowerShell Co-Community Director -http://www.powershellcommunity.org
*Blog -http://marcoshaw.blogspot.com
Very interesting;
However, when I run it I get this...
ERROR: Get-Variable : The scope number '4' exceeds the number of
active scopes.
ERROR: Parameter name: scopeID
ERROR: Actual value was 4.
ERROR: At line:65 char:5
ERROR: +   gv  <<<< -s $id >$null
ERROR: Get-Variable : The scope number '4' exceeds the number of
active scopes.
ERROR: Parameter name: scopeID
ERROR: Actual value was 4.
ERROR: At line:65 char:5
ERROR: +   gv  <<<< -s $id >$null
Maximum Scope range specification is 0..1
Then The information I would expect. Should I just ignore it or is
there something I should fix?
OldDog
Very strange.  Is that under 1.0 or CTP?
I'm not sure how to reproduce it.
One of these settings is likely to blame
$DebugPreference                   SilentlyContinue
$ErrorActionPreference             Continue
$ErrorView                         NormalView
$ProgressPreference                Continue
$VerbosePreference                 SilentlyContinue
$WarningPreference                 Continue
Are you using anything besides these defaults.
Meanwhile it is safe to ignore them.
The error is being caused on purpose and is
supposed to be trapped and suppressed.- Hide quoted text -
- Show quoted text -
I checked my profile and none of those settings appear. I am using 1.0
( how medieval of me).

OldDog
RickB
2008-10-07 15:10:34 UTC
Permalink
Post by OldDog
Post by OldDog
Post by RickB
See un-mangeled version here...
http://cid-bb10621fcfe1bcf8.spaces.live.com/blog/cns!BB10621FCFE1BCF8...
Introducing:www.poshcode.org
Share it!
--
*Microsoft MVP - Windows Server - Admin Frameworkshttps://mvp.support.microsoft.com/profile/Marco.Shaw
*PowerShell Co-Community Director -http://www.powershellcommunity.org
*Blog -http://marcoshaw.blogspot.com
Very interesting;
However, when I run it I get this...
ERROR: Get-Variable : The scope number '4' exceeds the number of
active scopes.
ERROR: Parameter name: scopeID
ERROR: Actual value was 4.
ERROR: At line:65 char:5
ERROR: +   gv  <<<< -s $id >$null
ERROR: Get-Variable : The scope number '4' exceeds the number of
active scopes.
ERROR: Parameter name: scopeID
ERROR: Actual value was 4.
ERROR: At line:65 char:5
ERROR: +   gv  <<<< -s $id >$null
Maximum Scope range specification is 0..1
Then The information I would expect. Should I just ignore it or is
there something I should fix?
OldDog
Very strange.  Is that under 1.0 or CTP?
I'm not sure how to reproduce it.
One of these settings is likely to blame
$DebugPreference                   SilentlyContinue
$ErrorActionPreference             Continue
$ErrorView                         NormalView
$ProgressPreference                Continue
$VerbosePreference                 SilentlyContinue
$WarningPreference                 Continue
Are you using anything besides these defaults.
Meanwhile it is safe to ignore them.
The error is being caused on purpose and is
supposed to be trapped and suppressed.- Hide quoted text -
- Show quoted text -
I checked my profile and none of those settings appear. I am using 1.0
( how medieval of me).
OldDog- Hide quoted text -
- Show quoted text -
I don't have any machines with 1.0 any more.

Hopefully someone who does can help.

At least it works... despite the errors.
Roman Kuzmin
2008-10-02 16:10:01 UTC
Permalink
This is a very useful tool indeed. That is why I did something like that in
the past, too. In particular I use it in Clear-Session script that removes
all *my* variables. Another task is to check that my script\function does
not leave some unwanted garbage-variables after its work. Debugging is yet
another useful area, of course.

What I do not like in both (yours and mine) solutions is that we have to
filter variables by names and that names are "collected by us". Next version
of PowerShell will introduce new automatic variables or remove\rename some
and we will have to update our scripts, right? There might be a better
solution.

It would be practically useful if variables have a property like IsSystem,
IsAutomatic (or a flag like this). What do you think? BTW, maybe something
like this exists already, anybody knows?
--
Thanks,
Roman Kuzmin

http://code.google.com/p/farnet/
PowerShell and .NET in FAR Manager
Post by RickB
Post by RickB
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.
See un-mangeled version here...
http://cid-bb10621fcfe1bcf8.spaces.live.com/blog/cns!BB10621FCFE1BCF8!126.entry
Roman Kuzmin
2008-10-07 09:41:23 UTC
Permalink
Post by Roman Kuzmin
It would be practically useful if variables have a property like IsSystem,
IsAutomatic (or a flag like this).
I submitted this as a proposal:
https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=373484&SiteID=99
--
Thanks,
Roman Kuzmin

http://code.google.com/p/farnet/
PowerShell and .NET in FAR Manager
RickB
2008-10-07 13:50:03 UTC
Permalink
Post by Roman Kuzmin
It would be practically useful if variables have a property like IsSystem,
IsAutomatic (or a flag like this).
I submitted this as a proposal:https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=3...
--
Thanks,
Roman Kuzmin
http://code.google.com/p/farnet/
PowerShell and .NET in FAR Manager
Good idea.
Thanks
RickB
2008-10-07 19:22:43 UTC
Permalink
Post by RickB
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.
See un-mangeled version here...http://cid-bb10621fcfe1bcf8.spaces.live.com/blog/cns!BB10621FCFE1BCF8...
Fyi, I've added Type data to the output.

I'm probably done messing with it unless
somebody can tell me how to fix OldDog's
problem
http://groups.google.com/group/microsoft.public.windows.powershell/msg/ff8a827ecd828e86

PS 14:16:50 109> myvars
Maximum Scope range specification is 0..0

Scope Name Type Value
----- ---- ---- -----
0 a PSNoteProperty Rick=null
0 b Null
0 file String .\Microsoft.PowerShell_profile.ps1
0 h Null False
0 Instance PSCustomObject @{Rick=}
0 LastReloadFile String .\Microsoft.PowerShell_profile.ps1
0 server Server [pmdras01]
0 this Null

Code here: http://cid-bb10621fcfe1bcf8.spaces.live.com/blog/cns!BB10621FCFE1BCF8!126.entry
RickB
2008-10-17 02:22:30 UTC
Permalink
Post by RickB
Post by RickB
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.
See un-mangeled version here...http://cid-bb10621fcfe1bcf8.spaces.live.com/blog/cns!BB10621FCFE1BCF8...
Fyi, I've added Type data to the output.
I'm probably done messing with it unless
somebody can tell me how to fix OldDog's
problemhttp://groups.google.com/group/microsoft.public.windows.powershell/ms...
PS 14:16:50 109>myvars
Maximum Scope range specification is 0..0
Scope Name           Type           Value
----- ----           ----           -----
    0 a              PSNoteProperty  Rick=null
    0 b              Null
    0 file           String         .\Microsoft.PowerShell_profile.ps1
    0 h              Null           False
    0 LastReloadFile String         .\Microsoft.PowerShell_profile.ps1
    0 server         Server         [pmdras01]
    0 this           Null
Code here:http://cid-bb10621fcfe1bcf8.spaces.live.com/blog/cns!BB10621FCFE1BCF8...
So I finally reproduced the problem that OldDog reported.
It's simply a matter of adding
$erroractionpreference = 'SilentlyContinue'
to Get-MaxScopeID

function Get-MaxScopeID () {
$erroractionpreference = 'SilentlyContinue'
# -2 below accounts for function and foreach scopes which go away
trap {return ([int]$id - 2)}
foreach ($id in 0..100) {
gv -s $id >$null
}
}

Loading...