February 2019
Intermediate to advanced
626 pages
15h 51m
English
The script scope is shared across all children in a script or script module. The script scope is a useful place to store variables that must be shared without exposing the variable to the global scope (and therefore to anyone with access to the session).
For example, the following short script stores a version number in a script-level variable. The Get-Version and Set-Version functions both interact with the same variable:
# Script file: example.ps1
[Version]$Script:Version = "0.1"
function Get-Version {
Write-Host "Version: $Version"
}
function Set-Version {
param(
[Version]$version
)
$Script:Version = $version
}
Set-Version 0.2
Write-Host (Get-Version)
The Set-Version function implements a local variable in the param block ...
Read now
Unlock full access