Skip to content

Instantly share code, notes, and snippets.

@mwallner
Created March 20, 2019 10:47
Show Gist options
  • Save mwallner/75cd7f2352f8e175b39529a575fd8959 to your computer and use it in GitHub Desktop.
Save mwallner/75cd7f2352f8e175b39529a575fd8959 to your computer and use it in GitHub Desktop.
get 'youngest' process with some filter possibilites
param(
[Parameter(Mandatory = $False)]
[int]$NumberOfProcesses = 1,
[Parameter(Mandatory = $False)]
[switch]$IncludeMicrosoftProcesses,
[Parameter(Mandatory = $False)]
[switch]$RequirePath,
[Parameter(Mandatory = $False)]
[string[]]$IgnoredProcesses
)
$ErrorActionPreference = "Stop"
$procs = if (-Not $IncludeMicrosoftProcesses) {
Get-Process | Where-Object {$_.company -notmatch 'microsoft'}
}
else {
Get-Process
}
if ($IgnoredProcesses) {
$procs = $procs | Where-Object {$IgnoredProcesses -notcontains $_.Name}
}
if ($RequirePath) {
$procs = $procs | Where-Object {$_.path}
}
Write-Output ($procs | Select-Object ID, Name, Path, StartTime, @{Name = "RunTime"; Expression = {(Get-Date) - $_.StartTime}}, WS, VM, Company `
| Sort-Object StartTime -descending `
| Select-Object -first $NumberOfProcesses)
@mwallner
Copy link
Author

while (1) {
  $p = ./Get-LatestProcess.ps1
  if ($p.RunTime.TotalMinutes -gt 1) {
    Write-Host "INACTIVITY ALERT! last app '$($p.Name)' is alive for $($p.RunTime.TotalMinutes) minutes."
  }
  Start-Sleep -Seconds 10
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment