forked from stcu/SharedScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd-ScopeLevel.ps1
More file actions
47 lines (38 loc) · 831 Bytes
/
Copy pathAdd-ScopeLevel.ps1
File metadata and controls
47 lines (38 loc) · 831 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<#
.SYNOPSIS
Convert a scope level to account for another call stack level.
.LINK
Stop-ThrowError.ps1
.LINK
Get-PSCallStack
.EXAMPLE
Add-ScopeLevel.ps1 Local
1
.EXAMPLE
Add-ScopeLevel.ps1 3
4
.EXAMPLE
Add-ScopeLevel.ps1 Global
Global
#>
#Requires -Version 3
[CmdletBinding()] Param(
# The requested scope from the caller of the caller of this script.
[Parameter(Position=0,Mandatory=$true)][string] $Scope
)
if($Scope -match '\A\d+\z') {return 1+[int]$Scope}
switch($Scope)
{
Global {'Global'}
Local {1}
Private {1}
Script
{
$stack = Get-PSCallStack
for($i = 2; $i -lt $stack.Length; $i++)
{
if($stack[$i].Command -and $stack[$i].FunctionName -like '<ScriptBlock>*') {return $i-1}
}
Stop-ThrowError.ps1 'Unable to find Script scope' -Argument Scope
}
}