Monday, January 23, 2017

Scope in Powershell

Long Time No See!!!!!          


      An item included in a scope is visible in the scope in which it was created and in any child scopes, unless it is explicitly made private.
An item can only be changed within the scope in which it was created, unless you explicitly specify a different scope.
                 If you create an item in a scope and the item shares its name with an item in a different scope the original item might be hidden by the new item, but it is not overridden or changed.
Visualize bubbles within bubbles. When the inner bubble pops, all its contents are gone.

Local Scope


$local - the current scope (whatever that may be at the time)
$time = get-date
$day = (get-date).day
function get-theTime()
{
 #create a local $time variable
 $time=(get-date).Minute
 $milli = (get-date).Millisecond
 Write-Output "Function: '`$Time' variable is $time"
 Write-Output "Function: '`$day' variable is $day"
 Write-Output "Function: '`$milli' variable is $milli"
}
 get-theTime Write-Output "Script: '`$Time' variable is $time"
 Write-Output "Script: '`$Day' variable is $day"
 Write-Output "Script: '`$milli' variable is $milli"
 Write-Output "NOTE: '`$milli' only existed within the life of the function call!"


Private Scope


$private – these cannot be seen outside the current scope.

$time = get-date
$Private:Pday = (get-date).Day
 # new-variable -name Pday -Option private -Value (get-date).day
 function Look-AtVars()
 {
 Write-Output "Function: '`$time' is viewable and has a value of $time"
 Write-Output "Function: '`$Pday' is private and has a value of $Pday"
 }
 Write-Output "Script: '`$time' is viewable and has a value of $time"
 Write-Output "Script: '`$Pday' is private and has a value of $Pday"
 Look-AtVars
Write-Output "NOTE: the Pday was not viewable from within the function!"


Script Scope


$script - the things within the script as the script runs.

$time = get-date
 function Set-ScriptVar()
 {
   $time = 1200
   Write-Output "Function: '`$time' has a value of $time"
   Write-Output "Function: script level '`$time' has a value of $script:time"
   $time = 1300
   $script:time = (get-date).AddYears(-6)
   Write-Output "Function: altered value of '`$time' to $time"
   Write-Output "Function: altered value of script level '`$time' to $script:time"
 }
 Write-Output "Script: '`$time' has a value of $time"
 Set-ScriptVar Write-Output "Script: '`$time' has a value of $time"
 Write-Output "NOTE: the year was changed from within the function!"

Global Scope


$global - automatic variables, preference variables, etc. $home for example.

# get-Variable -Scope Global
 Write-Output "'`$HOME' is: $HOME"
 Write-Output "'`$PSCULTURE' is: $PSCULTURE"
 Write-Output "'`$ERRRORACTIONPREFERENCE' is: $ERRORACTIONPREFERENCE"
 Write-Output " " $global:MYGLOBAL="MyGlobalVariable"
 Write-Output "'`$MYGLOBAL' is: $MYGLOBAL"
 Write-Output "NOTE: Global '`$MYGLOBAL' will be viewable in other scripts and functions"  Write-Output " " Write-Output "Try this: Get-Variable -Scope Global"

No comments:

Post a Comment

Windows Server 2016 Key Features and License Prospects

If you are planning to move to Windows 2016 and looking for benefits, I have listed down below : Why Windows Server 2016? ·     ...