forked from nightroman/PowerShellTraps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLib.ps1
More file actions
29 lines (24 loc) · 711 Bytes
/
Lib.ps1
File metadata and controls
29 lines (24 loc) · 711 Bytes
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
# How to set a variable in the caller scope.
# This works in script functions but not module functions.
function Set-CallerVariable1 {
Set-Variable myVar1 myValue1 -Scope 1
}
# This works in module functions but not script functions.
function Set-CallerVariable2 {
[CmdletBinding()]
param()
$PSCmdlet.SessionState.PSVariable.Set('myVar2', 'myValue2')
}
# This works in both cases (unlikely needed but possible).
function Set-CallerVariable3 {
[CmdletBinding()]
param()
if ($MyInvocation.MyCommand.ModuleName) {
# invoked as the module function
$PSCmdlet.SessionState.PSVariable.Set('myVar3', 'myValue3')
}
else {
# invoked as the script function
Set-Variable myVar3 myValue3 -Scope 1
}
}