如何使用 powershell 获取线程等待原因?
How to Get Thread Waitreasons using powershell?
您好,我已尝试获取线程等待原因来监视线程。但无法获得正确的原因。
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
我正在获取 Enum 的输出。但我需要枚举字符串值。所以我尝试创建一个 Swtich 语句,但我不知道如何从 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 线程状态
$tc.GetStatus("EventPairHigdh")
我需要在 Select-Object 中调用以下方法。
$tc.GetStatus("EventPairHigh")
输出应该是这样的。
Waiting for event pair high.Event pairs are used to communicate with protected subsystems.
当我尝试以下代码时,我没有得到 Process 的输出。
Get-Process -ProcessName Test | Select-Object @{Label='CMPNAME';Expression={$_.Name}} -ExpandProperty Threads | Select-Object {$_.Id}
OUTPUT :
$_.Id
-----
8412
10460
10484
10508
10520
10524
I was getting an output of Enum . But i need Enum string value.
幸运的是,枚举值很容易转换为字符串:
... | Select-Object -Property ID,@{Label='WaitReasons';Expression={$_.WaitReason -as [string]}}, ...
如果您想使用 class 方法而不将其绑定到 class 的实例,您需要将其标记为 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)}}, ...
您好,我已尝试获取线程等待原因来监视线程。但无法获得正确的原因。
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
我正在获取 Enum 的输出。但我需要枚举字符串值。所以我尝试创建一个 Swtich 语句,但我不知道如何从 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 线程状态 $tc.GetStatus("EventPairHigdh")
我需要在 Select-Object 中调用以下方法。
$tc.GetStatus("EventPairHigh")
输出应该是这样的。
Waiting for event pair high.Event pairs are used to communicate with protected subsystems.
当我尝试以下代码时,我没有得到 Process 的输出。
Get-Process -ProcessName Test | Select-Object @{Label='CMPNAME';Expression={$_.Name}} -ExpandProperty Threads | Select-Object {$_.Id}
OUTPUT :
$_.Id
-----
8412
10460
10484
10508
10520
10524
I was getting an output of Enum . But i need Enum string value.
幸运的是,枚举值很容易转换为字符串:
... | Select-Object -Property ID,@{Label='WaitReasons';Expression={$_.WaitReason -as [string]}}, ...
如果您想使用 class 方法而不将其绑定到 class 的实例,您需要将其标记为 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)}}, ...