【问题标题】:scoping for "variable:" query in get-item of powershellPowershell的get-item中“变量:”查询的范围
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 那么,为什么这不适用于 set-item、remove-item 和 test-path? IE。为什么get-item的行为和其他三个不一致(都是一致的)?
  • “那个”是 PowerShell MVP 邮件列表中提出的问题。看起来确实不一致。如果我学到了什么,我会回来报告的。
  • 谢谢。 PowerShell MVP 邮件列表中的邮件是否出现在我可以监控或查看的任何在线位置?谢谢。
  • 不,邮件列表是私有的,用于 MVP 和 PowerShell 产品团队之间的通信。它通常包含 NDA 下的主题。
  • 听说这是一个错误,但由于有一个简单的解决方法(使用 Get-Variable),它得到修复的几率很低。除非很多人投票解决它。我提交了错误。你可以在这里投票:connect.microsoft.com/PowerShell/feedback/details/807910/…
猜你喜欢
  • 2012-03-08
  • 1970-01-01
  • 2017-06-20
  • 2011-03-03
  • 1970-01-01
  • 2015-05-02
  • 1970-01-01
  • 2015-07-02
  • 1970-01-01
相关资源
最近更新 更多