我想找出是否可以在Powershell中回显多个对象。我尝试搜索找到它,但这不是我想要的。
基本上,我希望能够foreach-object
使用一个命令来回显两个不同的对象(都可以在中看到)。
预期的输入和输出
PS C:\> <echo multiple object command> "test" | % {echo "$_ test"}
test test
test test
我尝试过的输入和输出。
PS C:\> echo "test`ntest" | % {echo "$_ test"}
test
test test
预期产量:
test test
test test
get-process
例如,如果我这样做,例如:
PS C:\> get-process | % {echo "$_ and this"}
输出:
Process1 (Thing) and this
Process2 (Thing) and this
Process3 (Thing) and this
Process4 (Thing) and this
这也适用于像这样的数组:
PS C:\> $test = @("test1","test2","test3")
PS C:\> $test | % {echo "$_ and this"}
test1 and this
test2 and this
test3 and this
但是到目前为止,我还没有找到通过指定除数组之外的某些对象来复制它的任何方法
摘要:有没有办法在Powershell 5.1中编写多对象输出?
总而言之,我想回显多对象输出吗?
根据微软文档
ForEach-Object(%)cmdlet对输入对象集合中的每个项目执行操作。
集合就是数组。因此,您确实需要将数组提供给foreach(%)cmdlet
其他一些例子:
@("test1","test2","test3").gettype() # system.array
(get-process).gettype() # system.array
(1..10).gettype() # system.array
1..10 | % {echo "$_ test"}
"test`ntest".Split("`n").gettype() #system array
"test`ntest".Split("`n") | % {echo "$_ test"}
在您的第一个示例中:echo“ test`ntest” | %{回显“ $ _测试”}
当我们检查(echo "test
ntest“)。gettype()`是什么时,我们可以看到它是一个System.Object而不是一个数组。因此foreach对象会将其看作是一个内部有1个元素的数组。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句