Process Run Time

This windows powershell script will determine how long the oldest instance of a process has been running.  For example if you have 10 svchost processes running it will output the time of the oldest.

  • I've been attempting to make a change to this script to get the longest instance of a process via the Title.

    The modifications i have made work fine locally, but not through SAM:

    Function UpTime($ProcessTitle)
    {
    $Process = Get-Process| Where-Object {$_.MainWindowTitle -LIKE "*$ProcessTitle*" } -ErrorAction SilentlyContinue
    $Date = Get-Date

    If($Process -eq $null)
    {
    $Uptime = '0'
    }
    Else
    {
    If($Process.Count -eq $null)
    {
    $Uptime = ($Date.Subtract($Process.StartTime)).TotalMinutes
    }
    Else
    {
    $Sorted = $Process | Sort-Object StartTime
    $Process = $Sorted[0]
    $Uptime = ($Date.Subtract($Process.StartTime)).TotalMinutes
    }
    $Uptime = [int]$Uptime
    }
    Return $Uptime
    }

    $ProcessTitle = $args[0]
    $Uptime = UpTime $ProcessTitle
    Write-Host 'Message: The longest instance of process' $ProcessTitle 'has been running for (in minutes)'
    Write-Host 'Statistic: '$Uptime