【发布时间】:2011-11-29 13:19:12
【问题描述】:
比较以下三个脚本:
示例 1
$a = GPS | Where {$_.ProcessName -Match 'AcroRd32'}
$a
$a.Count
If ($a.Count -Eq 0)
{
Echo "Adobe Reader is Off"
}
Else
{
Echo "Adobe Reader is On"
}
# If Adobe Reader is not running, how come 0 (zero) is not returned?
# This is prettier, should I use it? Or does it slow down performance?
示例 2
$a = GPS AcroRd32
$a
$a.Count
If ($a.Count -Eq 0)
{
Echo "Adobe Reader is Off"
}
Else
{
Echo "Adobe Reader is On"
}
# If Adobe Reader is not running, how come 0 (zero) is not returned?
# This is uglier, but it doesn't have to pipe any output, so does it have any performance gains?
示例 3
GPS AcroRd32 | Measure | Select -Expand Count
# 0 (zero) is returned, but with an ugly Error
我想我的部分问题是我将 PowerShell 视为 VBS;以这种方式/风格编写代码通常会给我一个整数值零并且不会引发任何错误(当然,如果 Adobe Reader 已关闭)。检查程序实例未运行的正确 PowerShell 方法是什么? cmets 中的性能问题次于“PowerShell Way”问题。
附:老实说,第三个样本返回的错误信息不会破坏任何东西,它只是丑陋,所以它并没有超出实际用途,所以我想真正的问题是我只是一个美学的傻瓜=^D
【问题讨论】:
标签: powershell powershell-2.0 stderr