Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Novice question: How I can query for a variable scope?

Reply
 
Old 11-05-2006   #1 (permalink)
OK


 
 

Novice question: How I can query for a variable scope?

This is related to: RE: Issues of executing a script block in a function

1. How I can query for a variable scope?
I.e. I have a variable $a. How can I tell which scope it belongs to?
Knowing it will greatly to help with debugging! :-)

1.1. Which scope a function parameter belongs to?
1.2.1. Which scope a script (i.e. file with extension ps1) named
parameters/arguments belongs to?
1.2.2. Which scope a script block (i.e. {…}) parameter/argument belongs to?


---------------------------------------------------
From "PowerShell User Guide.doc":
The PowerShell provides four labels for identifying the scope of a variable:
local
global
private
script
----------------------------------------------------

## -- cut here ------------------------------------------------

function test {
"-- function test -----------------------"
"-- local: {0}" -f $local:a
"-- global: {0}" -f $global:a
"-- private: {0}" -f $private:a
"-- script: {0}" -f $script:a
}


[string] $local:a = "I'm local"
[string] $global:a = "I'm global"
[string] $private:a = "I'm local"
[string] $script:a = "I'm script"

"-- local: {0}" -f $local:a
"-- global: {0}" -f $global:a
"-- private: {0}" -f $private:a
"-- script: {0}" -f $script:a

test

## -- cut here ------------------------------------------------

****** output *************************************************

-- local: I'm script
-- global: I'm global
-- private: I'm script
-- script: I'm script
-- function test -----------------------
-- local:
-- global: I'm global
-- private:
-- script: I'm script


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Scope of Err Variable VB Script
Problem with variable scope PowerShell
Variable scope PowerShell
Potenial issue with the get-variable -scope parameter PowerShell
variable inside of a SQL query PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46