Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何从Get进程返回一个带有Count属性的非常零值?_Powershell_Powershell 2.0_Stderr - Fatal编程技术网

Powershell 如何从Get进程返回一个带有Count属性的非常零值?

Powershell 如何从Get进程返回一个带有Count属性的非常零值?,powershell,powershell-2.0,stderr,Powershell,Powershell 2.0,Stderr,比较以下三种脚本: 样本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

比较以下三种脚本:

样本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方法是什么?评论中的性能问题仅次于“PowerShell Way”问题


另外,老实说,第三个示例返回的错误消息不会破坏任何东西,只是很难看,因此它并不超出实际使用范围,所以我想真正的问题是我只是一个美学迷=^D

我建议您做如下操作:

$count = @(get-process | ?{$_.ProcessName -eq "AcroRd32"}).count

if($count -eq 0){

write-host "Adobe Reader is Off"

} else{

write-host "Adobe Reader is On"

}
上面所做的是将返回的对象强制放入一个数组中,因此如果没有正在运行的读卡器进程,您将得到一个空数组,因此其计数将为零。当你有进程的时候,你在数组中有它们,你得到了非零计数,上面的代码将按预期工作

基于样本2/样本3的备选方案:

$acrobat = gps AcroRd32 -ErrorAction SilentlyContinue
if($acrobat){

    write-host "Adobe Reader is On"

} else{

    write-host "Adobe Reader is Off"

}
在上面,如果读卡器未运行,我们将抑制该错误。然后,如果设置了$acrobat,您就知道读卡器正在运行

对代码的观察:


读卡器未运行时,$a未被分配任何内容,因此
$a.Count-eq 0
将为false。当读卡器运行时,$a被分配给这些进程对象,而您得到的$a.Count为1或更多,因此再次为false。因此,您将始终看到阅读器处于打开状态。

这是一个常见的PowerShell问题。命令可以返回:

  • Nothing~null(不具有属性
    Count
    ,获取该属性要么为null,要么失败)
  • 单个对象(它可能有自己的属性
    Count
    property,这是最容易混淆的情况——可以返回任何东西;或者可能没有它,然后获取
    Count
    将为null或失败)
  • 2+具有属性
    Count
    的对象数组
解决办法很简单。如果确实需要返回对象的计数,请使用
@()
运算符。结果总是一个具有属性
Count
的数组

# this is always an array
$result = @(<command returning something or nothing>)

# this is always a number:
$result.Count
#这始终是一个数组
$result=@()
#这始终是一个数字:
$result.Count

您在第三个示例中提到了错误-您可以使用SilentlyContinue ErrorAction隐藏消息:

GPS -ea silentlycontinue AcroRd32 | Measure | Select -Expand Count

@()
运算符与第一个示例的初始变量赋值相结合,使其非常漂亮且功能强大-
$a=@(GPS |其中{$\u.ProcessName-Match'AcroRd32'})
。为了满足学究般的冲动,我必须问,与只做
$a=@(GPS AcroRd32)
相比,美学方面的努力(从
GPS
Where
)是否会导致性能下降?如果是这样的话,我意识到它会很小,不会被我们注意到,但我仍然想知道,这样我就可以让自己少一点无知。XDI测量了一个进程“远”:
@(让进程远)
~1.6毫秒<代码>@(获取进程|其中{$\进程名-eq'far'})~6.8毫秒。根据我的经验,
其中Object
ForEach Object
基本上非常慢,我在脚本中避免了它们(在V2.0中,这总是可能的)。但是,它们仍然可以在typing.BTW的命令行中使用,如果您使用
@(GPS AcroRd32)
,那么当该过程不存在时,请做好出错准备。如果出现错误,则速度会很慢,如果错误首选项为“停止”,则可能会失败。为了避免错误,您可以使用以下技巧:。那么
@(GPS[A]croRd32)
应该是性能最好的。迷人的通配符解决方案!为了熟悉所有这些PowerShell特性,我应该查看哪些资源?本网站绝对是具有良好搜索功能的最佳资源之一:例如键入
[PowerShell]
,搜索,按投票排序,享受: