PowerShell 范围标识符 [英] PowerShell Scope Identifier
问题描述
我是 PowerShell 新手,试图更好地理解作用域.有没有办法从范围内识别范围?某些变量或函数会给我某种范围名称或范围 Guid 或范围 ID 吗?
I'm a PowerShell newbie, trying to understand scopes better. Is there a way to identify a scope from within the scope ? Some variable or function that would give me scope name or scope Guid or scope Id of some sort ?
例如,我如何知道局部作用域是否为全局作用域?
How could I, for example, know if the local scope is the global scope ?
推荐答案
例如,我如何知道局部作用域是否为全局作用域?
How could I, for example, know if the local scope is the global scope?
[bool] $isGlobalScope = -not $(try { Get-Variable -Scope 1 PSHOME } catch { })
$PSHOME
是一个 自动变量 带有选项 AllScope
,因此根据定义,它存在于每个范围内.它还具有选项Constant
,这意味着它既不能删除也不能修改.
$PSHOME
is an automatic variable with option AllScope
, so it is by definition present in each and very scope. It also has option Constant
, which means that it cannot neither removed nor modified.
通过询问Get-Variable
要从 parent 范围中检索此变量,-Scope 1
,如果您在全局范围内,则操作 失败,因为全局作用域是整个作用域层次结构的根,因此没有父作用域.
By asking Get-Variable
to retrieve this variable from the parent scope, -Scope 1
, the operation fails if you're in the global scope, because the global scope is the root of the entire scope hierarchy and therefore has no parent.
结果是:
如果您不在全局范围内,
Get-Variable
调用 succeeds 并返回一个 非空字符串(PowerShell 的主路径).
If you're not in the global scope, the
Get-Variable
call succeeds and returns a nonempty string (PowerShell's home path).
- 布尔
-not
运算符 将该字符串强制转换为 Boolean ([bool]
),其计算结果为$false
,假设非空字符串被视为$true
(无论其内容如何)并应用否定.
- The Boolean
-not
operator coerces that string to a Boolean ([bool]
), which evaluates to$false
, given that nonempty strings are considered$true
(irrespective of their content) and negation is applied.
如果您在全局范围内,则尝试访问 -Scope 1
会导致 statement-terminating 错误,这会导致触发 catch 块noreferrer">try
/catch
/finally
语句.
If you are in the global scope, the attempt to access -Scope 1
causes a statement-terminating error, which triggers the catch
block of the try
/ catch
/ finally
statement.
- 由于
catch
块不执行任何操作,它的行为类似于表达式上下文中的$null
-not
将$null
强制转换为布尔值.$null
在布尔上下文中是$false
,因此-not
的否定计算结果为$true
,表明代码在全局范围内运行.
- Since the
catch
block takes no action, it behaves like$null
in an expression context -not
coerces$null
to a Boolean.$null
is$false
in a Boolean context, so the negation with-not
evaluates to$true
, indicating that the code runs in the global scope.
注意:线程/后台作业和远程会话都有自己的全局作用域,上面的测试无法区分它们.
Note: Thread / background jobs and remote sessions have their own global scopes, and the test above cannot distinguish between them.
注意:这个答案的原始形式建议将 (Get-PSCallStack).Count -eq 1
作为测试;Get-PSCallStack
输出 [System.Management.Automation.CallStackFrame]
实例,以相反的顺序描述对当前作用域的调用链.因此,但是,此测试不会通过 dot-sourcing.
Note: The original form of this answer suggested (Get-PSCallStack).Count -eq 1
as a test; Get-PSCallStack
outputs [System.Management.Automation.CallStackFrame]
instances that describe the chain of invocations to the current scope in reverse order.
Therefore, however, this test doesn't detect running in the global scope via dot-sourcing.
这篇关于PowerShell 范围标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!