Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

PowerShell count issue

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 08-06-2007   #1 (permalink)
Jay
Guest


 

PowerShell count issue

Here's an enquiry to see if a service exists:

PS U:\> (get-process | where-object {$_.Name -eq "SERVICE1"})

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
234 9 15856 22628 119 197.64 6016 SERVICE1

....and another one...

PS U:\> (get-process | where-object {$_.Name -eq "SERVICE2"})

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
167 9 36300 44920 169 8,886.28 424 SERVICE2
167 9 36880 45504 167 8,936.44 652 SERVICE2
167 9 23376 32200 154 ...78.23 2640 SERVICE2
167 9 18820 27428 151 ...19.47 4500 SERVICE2
167 9 34476 43316 165 ...90.56 4856 SERVICE2
167 9 21428 30092 348 7,172.11 5308 SERVICE2

Perfect: that's correct, and what I need to know, so let's try just getting a count back:

PS U:\> (get-process | where-object {$_.Name -eq "SERVICE1"}).count
PS U:\> (get-process | where-object {$_.Name -eq "SERVICE2"}).count
6

Can anybody tell me why the SERVICE1 count returns nothing?

Out of interest, I also tried this:
PS U:\> (get-process | where-object {$_.Name -eq "SERVICE1"}).count+1-1
0
PS U:\> (get-process | where-object {$_.Name -eq "SERVICE2"}).count+1-1
6

One one of my servers, process 'sqlservr' occurs twice, and that correctly returns a count of 2: it just seems to be an issue when the count is 1.

Any thoughts?

Jay

My System SpecsSystem Spec
Old 08-06-2007   #2 (permalink)
Shay Levi
Guest


 

Re: PowerShell count issue

When there's only one item to return PowerShell treats it as a scalar vs.
multiple
items that returns (generally) as an array (Thanks Kiron).

If you need to check for the length or the count property try to cast it
into an Array


Try this:

@(get-process | where-object {$_.Name -eq "SERVICE1"}).count



Shay
http://scriptolog.blogspot.com



> Here's an enquiry to see if a service exists:
>
> PS U:\> (get-process | where-object {$_.Name -eq "SERVICE1"})
>
> Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
> ------- ------ ----- ----- ----- ------ -- -----------
> 234 9 15856 22628 119 197.64 6016 SERVICE1
> ...and another one...
>
> PS U:\> (get-process | where-object {$_.Name -eq "SERVICE2"})
>
> Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
> ------- ------ ----- ----- ----- ------ -- -----------
> 167 9 36300 44920 169 8,886.28 424 SERVICE2
> 167 9 36880 45504 167 8,936.44 652 SERVICE2
> 167 9 23376 32200 154 ...78.23 2640 SERVICE2
> 167 9 18820 27428 151 ...19.47 4500 SERVICE2
> 167 9 34476 43316 165 ...90.56 4856 SERVICE2
> 167 9 21428 30092 348 7,172.11 5308 SERVICE2
> Perfect: that's correct, and what I need to know, so let's try just
> getting a count back:
>
> PS U:\> (get-process | where-object {$_.Name -eq "SERVICE1"}).count PS
> U:\> (get-process | where-object {$_.Name -eq "SERVICE2"}).count 6
>
> Can anybody tell me why the SERVICE1 count returns nothing?
>
> Out of interest, I also tried this:
> PS U:\> (get-process | where-object {$_.Name -eq
> "SERVICE1"}).count+1-1
> 0
> PS U:\> (get-process | where-object {$_.Name -eq
> "SERVICE2"}).count+1-1
> 6
> One one of my servers, process 'sqlservr' occurs twice, and that
> correctly returns a count of 2: it just seems to be an issue when the
> count is 1.
>
> Any thoughts?
>
> Jay
>



My System SpecsSystem Spec
Old 08-06-2007   #3 (permalink)
Jay
Guest


 

Re: PowerShell count issue

Shay - that's the answer - since posting the message I'd found what the
problem was but was bumbling around trying (and failing) to find an
solution, so many thanks!

Jay


"Shay Levi" <no@addre.ss> wrote in message
news:8766a9443a398c9a65eb15a3ab8@news.microsoft.com...
> When there's only one item to return PowerShell treats it as a scalar vs.
> multiple
> items that returns (generally) as an array (Thanks Kiron).
>
> If you need to check for the length or the count property try to cast it
> into an Array
>
>
> Try this:
>
> @(get-process | where-object {$_.Name -eq "SERVICE1"}).count
>
>
>
> Shay
> http://scriptolog.blogspot.com
>
>
>
>> Here's an enquiry to see if a service exists:
>>
>> PS U:\> (get-process | where-object {$_.Name -eq "SERVICE1"})
>>
>> Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
>> ------- ------ ----- ----- ----- ------ -- -----------
>> 234 9 15856 22628 119 197.64 6016 SERVICE1
>> ...and another one...
>>
>> PS U:\> (get-process | where-object {$_.Name -eq "SERVICE2"})
>>
>> Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
>> ------- ------ ----- ----- ----- ------ -- -----------
>> 167 9 36300 44920 169 8,886.28 424 SERVICE2
>> 167 9 36880 45504 167 8,936.44 652 SERVICE2
>> 167 9 23376 32200 154 ...78.23 2640 SERVICE2
>> 167 9 18820 27428 151 ...19.47 4500 SERVICE2
>> 167 9 34476 43316 165 ...90.56 4856 SERVICE2
>> 167 9 21428 30092 348 7,172.11 5308 SERVICE2
>> Perfect: that's correct, and what I need to know, so let's try just
>> getting a count back:
>>
>> PS U:\> (get-process | where-object {$_.Name -eq "SERVICE1"}).count PS
>> U:\> (get-process | where-object {$_.Name -eq "SERVICE2"}).count 6
>>
>> Can anybody tell me why the SERVICE1 count returns nothing?
>>
>> Out of interest, I also tried this:
>> PS U:\> (get-process | where-object {$_.Name -eq
>> "SERVICE1"}).count+1-1
>> 0
>> PS U:\> (get-process | where-object {$_.Name -eq
>> "SERVICE2"}).count+1-1
>> 6
>> One one of my servers, process 'sqlservr' occurs twice, and that
>> correctly returns a count of 2: it just seems to be an issue when the
>> count is 1.
>>
>> Any thoughts?
>>
>> Jay
>>

>
>



My System SpecsSystem Spec
Old 08-06-2007   #4 (permalink)
Erase Ego
Guest


 

Re: PowerShell count issue

You could use the following to get a result even when there is just
one process matching your "where-object".

([Object[]](Get-Process | ?{$_.Name -eq "explorer"})).Count

MS

My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
count IT STAFF PowerShell 7 07-16-2008 04:52 AM
Count Thomas PowerShell 3 03-02-2007 01:23 AM
PowerShell parsing issue Joachim Meyer PowerShell 1 02-27-2007 10:53 AM
foo.count without formatting Brian Hoort PowerShell 1 01-08-2007 06:28 PM


Update your Vista Drivers Update Your Vista Drivers Now!!

Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008
Page generated in 0.30517 seconds with 10 queries