0

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 还有其他原因。谢谢。

4

1 回答 1

2

这似乎是 get-item 的错误,或者可能是其他命令在路径中允许“全局:”的错误。

于 2013-11-02T17:47:13.230 回答