Home > database >  Poweshell alias for startTIME of Process
Poweshell alias for startTIME of Process

Time:08-19

I am trying to create a simple powershell script which will allow it to display the PID and the starttime of a process.

For the moment this is my code -->

$proc=$args[0]
    if (get-process -name $proc -ErrorAction SilentlyContinue) { 
    $time= get-process $proc | Select-Object starttime |format-table -HideTableHeaders starttime 
    write-host  $proc $time} 
    else {write-host "non existant"}

When i look at the content of the variable $time , this is what is stored

> Microsoft.PowerShell.Commands.Internal.Format.FormatStartData
> Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Micr
> osoft.PowerShell.Commands.Internal.Format.FormatEntryData
> Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> Microsoft.Powe rShell.Commands.Internal.Format.FormatEntryData
> Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> Microsoft.PowerShell.Com mands.Internal.Format.FormatEntryData
> Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> Microsoft.PowerShell.Commands.Inte rnal.Format.FormatEntryData
> Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> Microsoft.PowerShell.Commands.Internal.Forma t.FormatEntryData
> Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> Microsoft.PowerShell.Commands.Internal.Format.FormatEn tryData
> Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Micro
> soft.PowerShell.Commands.Internal.Format.FormatEndData

Basicaly , what i would like to end with is a script that shows every PID and Startime for a particular application. The $time var would need to store the date "2022-08-18 1:08:39 PM"

I would also create another variable called $id which would store the PID of each process.

1408 notepad Started 2022-08-18 1:08:39 PM

Or for applications with multiple processes

1409 chrome Started 2022-08-18 1:08:39 PM
14264 chrome Started 2022-08-18 1:08:40 PM
4567 chrome Started 202...

For the ones that have multiple processes , I guess I would use an foreach statement and go through every PID available in the variable ? Still new at this so if you have any advice on how to go about it would be appreciated , but in regards to what I know , a foreach statement would help me with this.

CodePudding user response:

There are two common problems with your code:

  • Format-* cmdlets emit output objects whose sole purpose is to provide formatting instructions to PowerShell's for-display output-formatting system. In short: only ever use Format-* cmdlets to format data for display, never for subsequent programmatic processing - see this answer for more information.

  • The reason you tried to use Format-Table to begin with was in order to get just the .StartTime property value. The proper way to get Select-Object to output just a property value is to use the -ExpandProperty parameter, i.e.
    -ExpandProperty starttime instead of [-Property] starrttime.


A PowerShell-idiomatic solution would look something like this:

$proc = $args[0]

# Process all matching processes, if any.
Get-Process $proc -ErrorAction SilentlyContinue |
  ForEach-Object {
    '{0} {1} Started {2}' -f $_.Id, $_.Name, $_.StartTime
  }

# $? contains $false if an error occurred in the previous statement.
if (-not $?) { Write-Warning "Non-existent: $proc" }

That is, matching processes are processed one by one using ForEach-Object, and in the script block ({ ... }) passed to it you can refer to the process-info object (System.Diagnostics.Process) at hand via the automatic $_ variable variable, allowing you to access the properties of interest directly on that object. The -f operator is used to synthesize the output strings in the desired format.

Sample output:

56204 bash Started 8/18/2022 2:15:25 PM
56245 bash Started 8/18/2022 2:15:28 PM

CodePudding user response:

Found an answer for my own question. If anyone else is wondering why this happens , it is all explained in this post.

How can I store output from Format-Table for later use

  • Related