我是 powershell 脚本编写的初学者,我希望能够区分脚本的输出。让我们以这个脚本 test.ps1 为例:
param([System.String] $w)
$x=$w+" is correct"
$y=$w+ " is false"
$x
$y
要执行它并检索值 $x 和 $y,我正在这样做:
$a=.\test1.ps1 -w test
$a[0] #this is x
$a[1] # this is y
有没有一种方法可以使用类似于 $a.x 的东西来检索 $x 值?
谢谢。
最佳答案
要执行您想要的操作,您将需要一个包含键/值对或命名属性的对象。例如,您可以创建一个名为 a
的对象,其属性为 x
和 y
:
$x = "one"
$y = "two"
$a = [pscustomobject]@{"x"=$x;"y"=$y}
测试上述案例:
$a
x y
- -
one two
$a.x
one
$a.y
two
我喜欢这个链接PSCustomObjects了解创建和使用自定义对象的说明。
使用函数进行测试:
function test {
param([string] $w)
$x = $w + "x"
$y = $w + "y"
[pscustomobject]@{"x"=$x; "y"=$y}
}
$a = test "my property "
$a
x y
- -
my property x my property y
$a.x
my property x
$a.y
my property y
关于powershell - 通过符号名称(键)访问脚本的多个输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54985160/