Scope of Windows Powershell variables _powershell

Source: Internet
Author: User
Tags modifiers variable scope visibility

If we do not make a special declaration on a variable, the PowerShell interpreter automatically handles and restricts the scope of the variable. Save the following content command to TEST1.PS1

$windows = $env: Windir
"Windows Folder: $windows"

Then assign the variable $windows to the console and invoke the Test.ps1 script.

ps> $windows = "Hellow"
ps>. \test.ps1
windows folder:c:\windows
ps> $windows
hellow

When the script is invoked, a variable $windows is assigned, and the variable is reclaimed after the script call is completed, and the variables in the script do not affect variables outside the script because they are in different scopes. PowerShell assigns them different scopes for each function and script.

Change the visibility of a variable
You can easily see what happens when there is no PowerShell interpreter automatically restricting visibility, just as the script, just the command, adds one more point to the script when it is run. and a space:

ps> $windows = "Hellow"
ps> \test.ps1
windows folder:c:\windows
ps> $windows
c:windows

When you run a script with a single origin and a space, the PowerShell interpreter does not create its own variable scope for the script itself, it shares the scope of the current console, a less flexible but simple method that must be used with extreme care.

Advantages of enforcing variable visibility restrictions: emptying the initialization environment
It can be assumed that if you accidentally define a read-only constant in the current console, this constant cannot be updated or deleted, which is troublesome. But it's not a problem if you manipulate the variable in a script, because the script has its own scope. For example, save the following text as TEST.PS1 and invoke no problems:

New-variable a-value 1-option Constant
"value: $a" ps>. \test.ps1 value:1 ps>
. \test.ps1
Value: 1

But if you disable the scope limit through a dot, call Test.ps1, there will be an exception, because a constant cannot be created two times.

Ps>. . \TEST.PS1
value:1
ps>. \test.ps1 new-variable:a Variable with
name ' A ' already exists.
Attest.ps1:1 char:13
+ new-variable <<<< a-value 1-option Constant
  + categoryinfo     : Resourceexists: (a:string) [new-variable], sessionstateexception
  + fullyqualifiederrorid: Variablealreadyexists,microsoft.powershell.commands.newvariablecommand

Therefore, the scope limitation of this variable can minimize the conflict of variables.

Set the scope of a single variable
So far, the changes in the scope of the variables are all global and can be customized for the scope of a particular variable.

$global
global variables, valid in all scopes, if you set a global variable in a script or function, the variable is still valid even if both the script and the function end.

$script
Script variables, which are only valid within the script, including the functions in the script, will be reclaimed once the script has finished running.

$private
Private variables, which are only valid in the current scope and cannot be run through other scopes.

$local
The default variable, which can omit modifiers, is valid in the current scope, and the other scopes have read-only access to it.

After opening the PowerShell console, PowerShell automatically generates a new global scope. Additional scopes are generated if functions and scripts are added, or special definitions are made. In the current console, there is only one scope, accessed by modifiers, which actually accesses the same variable:

ps> $logo = "Www.jb51.net"
ps> $logo
www.jb51.net
ps> $private: Logo
www.jb51.net
PS > $script: Logo
www.jb51.net
ps> $private: Logo
www.jb51.net
ps> $global: Logo
Www.jb51.net

When a defined function is invoked, PowerShell generates a second scope that can perform a read operation on the variables in the caller's scope, but cannot perform a write operation.

ps> function f () {"var= $var"; $var = "function inner"; $var}
ps> $var = "I am in console."
ps> $var
I am in console.
Ps> F
var=i am in console.
function inner
ps> $var
I am in console.

The private modifier is useful for protecting variables in the current console from being accessed in functions and scripts.

ps> function f () {"var= $var"; $var = "function inner"; $var}
ps> $private: var= "I am a private variable in console, Other scope can is not access me. "
ps> f
var=
function inner
ps> $private: var
i am a private variable in Console,other scope can no t access me.

Can the variable for $private limit be modified by $global in the function? Not only cannot modify, but also deletes the current $private variable

Ps> Function F () {"var= $var"; $global: var= "Try to change variable in Function"}
ps> $private: var= "I am a private Variable "
ps> $private: var
i am a private variable
ps> $var
I am a private variable
ps> />var=
ps> $private: var
ps> $var
ps>
ps> $private-eq $null
True

But $local-decorated variables can be changed inside the function by $global.

Ps> Function F () {"var= $var"; $global: var= "Try to change variable in Function"}
ps> $var = ' I am a local variable ."
ps> $var
I am a local variable.
Ps> $private: Var
I am a local variable.
Ps> F
var=i am a local variable.
Ps> $var
 Try to change variable in function
ps> $local: Var
 Try to change variable in function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.