PriorityClass

Welcome Forums General PowerShell Q&A PriorityClass

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
1 month, 1 week ago.

  • Author
    Posts
  • #121125

    Participant
    Points: 180
    Rank: Participant

    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.

     

  • #121128

    Participant
    Points: 259
    Helping Hand
    Rank: Contributor

    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'}
    • #121132

      Participant
      Points: 180
      Rank: Participant

      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,

  • #121452

    Participant
    Points: 316
    Helping Hand
    Rank: Contributor

    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.

    • #121467

      Participant
      Points: 180
      Rank: Participant

      Many thanks, Mr PostaNote ... you have given me excellent examples and they have given me new insights into Powershell.

      Much appreciated.

       

You must be logged in to reply to this topic.