【发布时间】:2013-11-01 14:39:37
【问题描述】:
powershell 中的get-item 用于获取全局变量时,似乎不起作用。当然它与 set-item、remove-item 或 test-path 的行为不一致。特别是,有人可以解释这个脚本的输出吗?
#
#Script to execute
#
$Global:g_test="Hello World"
test-path "variable:\Global:g_test"
$Global:g_test
set-item -path "variable:\Global:g_test" -value "Goodbye Cruel World"
$Global:g_test
$Global:g_test.getType()
write-host '$l_tmp=$(get-item -path variable:\Global:g_test).value'
$l_tmp=$(get-item -path variable:\Global:g_test).value
$l_tmp
$l_tmp.gettype()
write-host '$l_tmp=$(get-item -path "variable:\Global:g_test").value'
$l_tmp=$(get-item -path "variable:\Global:g_test").value
$l_tmp
$l_tmp.gettype()
write-host '$l_tmp=$(get-item -path variable:Global:g_test).value'
$l_tmp=$(get-item -path variable:Global:g_test).value
$l_tmp
$l_tmp.gettype()
write-host '$l_tmp=$(get-item -path "variable:Global:g_test").value'
$l_tmp=$(get-item -path "variable:Global:g_test").value
$l_tmp
$l_tmp.gettype()
remove-item "variable:\Global:g_test"
test-path "variable:\Global:g_test"
预期输出:
True
Hello World
Goodbye Cruel World
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
$l_tmp=$(get-item -path variable:\Global:g_test).value
Goodbye Cruel World
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
$l_tmp=$(get-item -path "variable:\Global:g_test").value
Goodbye Cruel World
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
$l_tmp=$(get-item -path variable:Global:g_test).value
Goodbye Cruel World
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
$l_tmp=$(get-item -path "variable:Global:g_test").value
Goodbye Cruel World
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
False
实际输出:
True
Hello World
Goodbye Cruel World
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
$l_tmp=$(get-item -path variable:\Global:g_test).value
Property 'value' cannot be found on this object. Make sure that it exists.
At C:\redacted\testing.ps1:11 char:11
+ $l_tmp=$(get-item -path variable:\Global:g_test).value
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], PropertyNotFoundException
+ FullyQualifiedErrorId : PropertyNotFoundStrict
You cannot call a method on a null-valued expression.
At C:\redacted\testing.ps1:13 char:2
+ $l_tmp.gettype()
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
$l_tmp=$(get-item -path "variable:\Global:g_test").value
Property 'value' cannot be found on this object. Make sure that it exists.
At C:\redacted\testing.ps1:15 char:11
+ $l_tmp=$(get-item -path "variable:\Global:g_test").value
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], PropertyNotFoundException
+ FullyQualifiedErrorId : PropertyNotFoundStrict
You cannot call a method on a null-valued expression.
At C:\redacted\testing.ps1:17 char:2
+ $l_tmp.gettype()
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
$l_tmp=$(get-item -path variable:Global:g_test).value
Property 'value' cannot be found on this object. Make sure that it exists.
At C:\redacted\testing.ps1:19 char:11
+ $l_tmp=$(get-item -path variable:Global:g_test).value
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], PropertyNotFoundException
+ FullyQualifiedErrorId : PropertyNotFoundStrict
You cannot call a method on a null-valued expression.
At C:\redacted\testing.ps1:21 char:2
+ $l_tmp.gettype()
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
$l_tmp=$(get-item -path "variable:Global:g_test").value
Property 'value' cannot be found on this object. Make sure that it exists.
At C:\redacted\testing.ps1:23 char:11
+ $l_tmp=$(get-item -path "variable:Global:g_test").value
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], PropertyNotFoundException
+ FullyQualifiedErrorId : PropertyNotFoundStrict
You cannot call a method on a null-valued expression.
At C:\redacted\testing.ps1:25 char:2
+ $l_tmp.gettype()
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
False
#>
重点是,get-item -path variable:\Global:g_test 不会失败,而是只返回 $null,而不是请求的变量。是的,我知道我可以使用“$(get-variable g_test -scope Global).value”,但还有其他原因导致我尝试使用 get-item 而不是 get-variable。谢谢。
【问题讨论】:
-
全局变量在没有
global:的情况下总是可以访问的,除非你有一个同名的中间作用域变量隐藏了全局变量。这就是你想要达到的目标吗? -
我正在为一个小型库编写一些实用函数。该库将由其他开发人员使用。因此,我无法预测中间范围内可能还有哪些其他变量。虽然我可以(并且已经)使用“get-variable”而不是“get-item”编写工作代码,但代码还需要访问 env: 变量。如果我可以使用“variable:global:g_xxx”结构,代码会更加优雅和统一。我只是无法理解为什么它应该适用于 set-item、remove-item 和 test-path,但是 silently 却无法用于 get-item。
标签: powershell