我正在尝试从运行Windows Server 2012的终端服务器获取所有挂起的任务。
我尝试过使用带有wmi对象的powershell,如下所示:
Get-WmiObject -Class Win32_Process -ComputerName computername -Property status
但是所有进程的status
属性都是空的,但它显示在任务管理器的详细信息视图中,如下所示:
我还尝试了以下代码来尝试获取正在运行的线程的状态:
$processes = Get-Process * -ComputerName ppivts | select name,threads
foreach ($process in $processes)
{
foreach ($thread in $process.Threads)
{
if($thread.ThreadState -ne "Wait"){
$process.Name
$thread.ThreadState
}
}
}
这也不起作用。如何获取流程的状态,更具体地说是暂停流程的状态?
答案 0 :(得分:1)
您可以按如下方式改进后面的代码段:
$processes = Get-Process *
$processHt = @{} # empty hash table
foreach ($process in $processes) {
foreach ($thread in $process.Threads) {
if($thread.ThreadState -eq "Wait") {
if ( $processHt.Containskey( $process.Name ) ) {
if ( $processHt[$process.Name] -match $($thread.WaitReason.ToString()) ) {
} else {
$processHt[$process.Name] += ",$($thread.WaitReason.ToString())"
}
} else {
$processHt.Add( $process.Name , $thread.WaitReason.ToString() )
}
}
}
}
"`n=== all threads suspended ==="
$processHt.Keys | Where-Object { $processHt[$_] -eq 'Suspended' }
"`n=== some thread suspended ==="
$processHt.Keys | Where-Object { $processHt[$_] -match 'Suspended' } |
ForEach-Object { @{ $_ = $processHt[$_] } } |
Format-Table -AutoSize -HideTableHeaders # merely for simple output look
示例输出:
PS D:\PShell> D:\PShell\SO\46546587.ps1
=== all threads suspended ===
WWAHost
=== some thread suspended ===
System FreePage,Executive,EventPairLow,Suspended,VirtualMemory,LpcReceive,ExecutionDelay
WWAHost Suspended
explorer UserRequest,Executive,EventPairLow,Suspended
PS D:\PShell>
相应的任务管理器截图:
答案 1 :(得分:0)
此Powershell WMI代码将在本地和远程PC上均可使用
$fname = "csrss.exe"
$ComputerName = "Server"
Get-WmiObject -ComputerName $ComputerName Win32_Process | where Name -eq $fname |
Foreach{
$processHandle = $_.handle
echo "processHandle=$processHandle"
$Threads = Get-WmiObject -ComputerName $ComputerName -Class Win32_Thread | Where-Object { $_.ProcessHandle -eq $processHandle }
"The $name process has $($threads.count) threads"
$threads | Format-Table -Property priority, Handle, ProcessHandle, thread*, ProcessCreation, ClassName, User*Time, kernel*Time
}
# ThreadStates:
# 0 - Initialized. It is recognized by the microkernel.
# 1 - Ready. It is prepared to run on the next available processor.
# 2 - Running. It is executing.
# 3 - Standby. It is about to run. Only one thread may be in this state at a time.
# 4 - Terminated. It is finished executing.
# 5 - Waiting. It is not ready for the processor. When ready, it will be rescheduled.
# 6 - Transition. The thread is waiting for resources other than the processor.
# 7 - Unknown. The thread state is unknown.
# ThreadWaitReason:
# 0 - Executive
# 1 - FreePage
# 2 - PageIn
# 3 - PoolAllocation
# 4 - ExecutionDelay
# 5 - FreePage
# 6 - PageIn
# 7 - Executive
# 8 - FreePage
# 9 - PageIn
# 10 - PoolAllocation
# 11 - ExecutionDelay
# 12 - FreePage
# 13 - PageIn
# 14 - EventPairHigh
# 15 - EventPairLow
# 16 - LPCReceive
# 17 - LPCReply
# 18 - VirtualMemory
# 19 - PageOut
# 20 - Unknown