src/Test-PsState.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#requires -version 6
using namespace System.Linq

function Test-PsState {
  [CmdletBinding(DefaultParameterSetName='Name')]
  param(
    [Parameter(Mandatory, ParameterSetName='Name', Position=0)]
    [ValidateNotNullOrEmpty()]
    [ValidateScript({!!($script:ps = Get-Process $_ -ErrorAction 0)})]
    [String]$Name,

    [Parameter(Mandatory, ParameterSetName='Id', Position=0)]
    [ValidateScript({!!($script:ps = Get-Process -Id $_ -ErrorAction 0)})]
    [Int32]$Id
  )

  process {
    $ps.ForEach{
      [PSCustomObject]@{
        ProcessName = $_.ProcessName
        PID = $_.Id
        Priority = $_.PriorityClass
        Suspended = [Enumerable]::Sum([Int32[]](
          Select-Object -InputObject $_.Threads[0] -Property ThreadState, WaitReason
        ).PSObject.Properties.Value.ForEach{$_ -eq 5}) -eq 2
        Uptime = [DateTime]::Now - $_.StartTime
      }
      $_.Dispose()
    } | Format-Table -AutoSize
  }
}