#
#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。感谢。
答案 0 :(得分:2)
这似乎是get-item的错误,或者是其他命令在路径中允许“global:”的错误。