可以获取 PowerShell 中最后执行的命令的计数吗?
我正在编写一个脚本,我想吐出一堆记录,然后将计数显示为最后一行。这就是我到目前为止所拥有的:
Get-Whatever -Department $Deparment
Write-Host (Get-Whatever -Department $Deparment).Count " records found"
但我很好奇是否有一种方法可以做到这一点而无需执行两次。我以为我读过你可以在某处使用 $$
但这不起作用。有没有更好的方法来做到这一点,或者我只需要运行两次?
我想要的输出看起来像这样:
Name
-------
Abe
Joe
Bill
3 records found
I am writing a script where I would like to spit out a bunch of records and then display the count as the last line. This is what I have so far:
Get-Whatever -Department $Deparment
Write-Host (Get-Whatever -Department $Deparment).Count " records found"
But i'm curious if there is a way to do it without executing it twice. I thought that I had read you could use $$
somewhere but this isn't working. Is there a better way to do this, or do I just have to run it twice?
My Desired output would look something like this:
Name
-------
Abe
Joe
Bill
3 records found
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不简单地将结果分配给变量呢?
请注意
@(..)
。它确保即使Get-Whatever
不返回任何内容,$d
也将是空数组。其他方式例如
Tee-Object
。但是,它有点“神奇”地创建变量,因此它不如第一种方法那么可读:至于
Tee-Object
(来自文档,请尝试help tee-object -online
):Why don't you simply assign the result to a variable?
Note the
@(..)
. It ensures that even whenGet-Whatever
doesn't return anything,$d
will be empty array.Other way is e.g.
Tee-Object
. However, it somewhat "magically" creates variables, so it is not as readable as the first approach:As for
Tee-Object
(from documentation, tryhelp tee-object -online
):这应该有效:
This should work: