【发布时间】:2021-05-01 05:56:50
【问题描述】:
您好,我已尝试获取线程等待原因来监视线程。但无法得到正确的原因。
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 = 新对象 -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
【问题讨论】:
-
在类定义中
[string]GetStatus之前添加static关键字,然后在Select-Object属性表达式中将$.GetStatus更改为[ThreadStatus]::GetStatus。或者删除类定义并执行@{Name='WaitReason';Expression={$_.WaitReason -as [string]}} -
@Mathias : GetStatus 有要传递的参数是线程的属性 @{Label='WaitReasons';Expression={[ThreadStatus]::GetStatus(-Property WaitReason)}} .. 我是出现我无法传递参数的错误
-
$_指的是管道中的当前对象,因此您需要将$_.WaitReason作为参数传递给GetStatus参数 -
我什至尝试了硬编码但没有得到输出 Get-Process -ProcessName NpService |选择对象-ExpandProperty 线程 |选择对象@{Label='WaitReason';Expression={[ThreadStatus]::GetStatus("EventPairHigh")}}
-
您在
GetStatus方法中有return "",所以结果是总是 将是一个空字符串。你的意思是return $Wait_ReasonValue?