Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
77 views
in Technique[技术] by (71.8m points)

How to Get Thread Waitreasons using powershell?

Hi I have tried to get the Thread wait reason to monitor the threads. But unable to get the proper Reasons.

Get-Process -ProcessName NpService | Select-Object -ExpandProperty  Threads | Select-Object -Property ID ,  @{Label='WaitReasons';Expression={$.GetStatus(-Property WaitReason)}},  @{N='Current Date';E={ $(Get-Date -Format 'y-M-d H:mm:ss') }} , BasePriority , CurrentPriority , ThreadState , WaitReason , PriorityLevel , TotalProcessorTime , UserProcessorTime , PrivilegedProcessorTime  | ConvertTo-Csv -NoTypeInformation  

I was getting an output of Enum . But i need Enum string value. So i tried creating a Swtich statement but i dont know how to call it from the Select-Object

 class ThreadStatus {

[string]GetStatus($threadId) {
    Write-Output $threadId
$CustomObj = New-Object -TypeName PSObject
 $Wait_ReasonValue  =""

switch($threadId)
{
    "EventPairHigh" {return "Waiting for event pair high.Event pairs are used to communicate with protected subsystems." ; break}
        "EventPairLow" { return "Waiting for event pair low. Event pairs are used to communicate with protected subsystems." ; break}
        #ExecutionDelay { $Wait_ReasonValue = "Thread execution is delayed." ; break}
        #Executive { $Wait_ReasonValue = "The thread is waiting for the scheduler." ; break}
        #FreePage { $Wait_ReasonValue = "Waiting for a free virtual memory page." ; break}
        #LpcReceive { $Wait_ReasonValue = "Waiting for a local procedure call to arrive."; break}
        #LpcReply { $Wait_ReasonValue = "Waiting for reply to a local procedure call to arrive." ; break}
       # PageIn { $Wait_ReasonPropertyValue = "Waiting for a virtual memory page to arrive in memory." ; break}
        #PageOut { $Wait_ReasonValue = "Waiting for a virtual memory page to be written to disk." ; break}
      #  Suspended { $Wait_ReasonValue = "Thread execution is suspended." ; break}
       # SystemAllocation { $Wait_ReasonValue = "Waiting for a memory allocation for its stack." ; break}
        #Unknown { $Wait_ReasonValue = "Waiting for an unknown reason." ; break}
        #UserRequest { $Wait_ReasonValue = "The thread is waiting for a user request." ; break}
        #VirtualMemory { $Wait_ReasonValue = "Waiting for the system to allocate virtual memory." ; break}

        Default { $Wait_ReasonValue = " " ; break }
        }
         # $CustomObj | Add-Member –MemberType NoteProperty –Name "WaitReason" –Value $Wait_ReasonValue  
        # $CustomObj
        # Write-Output "TEST"
       # Write-Output $Wait_ReasonValue 
        return ""

            }

} $tc = New-Object -TypeName ThreadStatus $tc.GetStatus("EventPairHigdh")

I need to call the following method inside the Select-Object .

 $tc.GetStatus("EventPairHigh")

Output should be like this.

 Waiting for event pair high.Event pairs are used to communicate with protected subsystems.

When i tried the following code i am not getting the output for Process.

Get-Process -ProcessName Test | Select-Object @{Label='CMPNAME';Expression={$_.Name}}  -ExpandProperty Threads | Select-Object {$_.Id}

  OUTPUT :
         $_.Id
         -----
         8412
         10460
         10484
         10508
         10520
         10524
question from:https://stackoverflow.com/questions/65918471/how-to-get-thread-waitreasons-using-powershell

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I was getting an output of Enum . But i need Enum string value.

Luckily, enum values are easily converted to strings:

... | Select-Object -Property ID,@{Label='WaitReasons';Expression={$_.WaitReason -as [string]}}, ...

If you want to use a class method without tying it to an instance of the class, you need to mark it static:

class ThreadStatus {
  static [string] GetStatus([System.Diagnostics.ThreadWaitReason]$threadId){
    ...
  }
}

# Now we can invoke the method like this:
... | Select-Object -Property ID,@{Label='WaitReasons';Expression={[ThreadStatus]::GetStatus($_.WaitReason)}}, ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...