Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Return a Pretty Zero Value with the Count Property from Get-Process?

Compare the three scripts below:

Sample 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?



Sample 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?



Sample 3

GPS AcroRd32 | Measure | Select -Expand Count

# 0 (zero) is returned, but with an ugly Error



I guess part of my problem is that I'm treating PowerShell like it's VBS; writing code in this manner/style would usually yield me an integer value of zero and not throw any errors (if Adobe Reader was off, of course). What's the correct PowerShell way of checking that an instance of a program is not running? The performance questions in the comments are secondary to the "PowerShell Way" question.

P.S. To be honest, the Error Message returned by the 3rd Sample wouldn't break anything, it's just ugly, so it's not beyond practical use, so I guess the real problem is that I'm just a sucker for aesthetics =^D

like image 233
Stisfa Avatar asked Oct 02 '11 20:10

Stisfa


1 Answers

This is a common PowerShell gotcha. A command can return:

  • Nothing ~ null (does not have the property Count, getting it either gets null or fails)
  • A single object (it may have its own property Count property, the most confusing case -- can return anything; or may not have it, then getting Count gets null or fails)
  • 2+ object array which has the property Count.

The solution is simple. When you really need the count of returned objects use the @() operator. The result is always an array which has the property Count.

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

# this is always a number:
$result.Count
like image 151
Roman Kuzmin Avatar answered Nov 15 '22 04:11

Roman Kuzmin