The ff statement works just fine
Get-Process | Group-Object -Property PriorityClass
But when I add the second line (to obtain only High and AboveNormal):
Get-Process | Group-Object -Property PriorityClass |
Where-object {$_.Name -eq 'AboveNormal' -or $_.Name -eq 'High'}
I get the expected (and correct) output, but additionally, the following error message twice:
Group-Object : Exception getting “PriorityClass”: “Access is denied”
At line:2 char:15
Get-Process | Group-Object -Property PriorityClass |
CategoryInfo : InvalidResult: (System.Diagnostics.Process (Idle):PSObject) [Group-Object], Get
ValueInvocationException
FullyQualifiedErrorId : ExpressionEvaluation,Microsoft.PowerShell.Commands.GroupObjectCommand
My understanding of Group-objector its syntax when piping to Where-object must be missing something.Would be grateful for any advice or tips.
Olaf
November 2, 2018, 2:17am
2
I cannot explain why this happens but you could try to specify what properties you’re after with your Get-Process. At least in my environment it works this way.
Get-Process |
Select-Object -Property ProcessName,MainWindowTitle,PriorityClass |
Group-Object -Property PriorityClass |
Where-Object {$.Name -eq ‘AboveNormal’ -or $ .Name -eq ‘High’}
Many thanks Mr Olaf Soyk,
I will most certainly try your suggestion. This has been mind-boggling … At first I wished I had a second laptop nearby to “reproduce” the error – if the error message is to be believed and truly the reason. (no access rights). But since you experienced the same (I assume, from your response), I now assume this is a reproducible error.
The puzzling thing is it still gave me the correct outputs – i.e., identical to the one-liner (which had additional for Normal and Low priorities).
Thanks again,
It’s best to select before a group.
So, why not just do this…
Get-Process | Select PriorityClass | Group-Object -Property PriorityClass |
Where-object {$_.Name -eq 'AboveNormal' -or $_.Name -eq 'High'}
Count Name Group
----- ---- -----
3 High {@{PriorityClass=High}, @{PriorityClass=High}, @{PriorityClass=High}}
3 AboveNormal {@{PriorityClass=AboveNormal}, @{PriorityClass=AboveNormal}, @{PriorityClass=AboveNormal}}
Or this…
Get-Process | Select PriorityClass | Group-Object -Property PriorityClass |
Where-object {$_.Name -eq 'AboveNormal' -or $_.Name -eq 'High'} |
Select Count,Name
Count Name
----- ----
3 High
3 AboveNormal
Or even this
(Get-Process | Select-Object PriorityClass) -match 'AboveNormal|High' | Group-Object -Property PriorityClass
Count Name Group
----- ---- -----
3 High {@{PriorityClass=High}, @{PriorityClass=High}, @{PriorityClass=High}}
3 AboveNormal {@{PriorityClass=AboveNormal}, @{PriorityClass=AboveNormal}, @{PriorityClass=AboveNormal}}
… as you will note, no error are generated.
Many thanks, Mr PostaNote … you have given me excellent examples and they have given me new insights into Powershell.
Much appreciated.