Tim
Tim

Reputation: 11

How to extract timestamp from output of cmdlet using powershell

I have the following output from a cmdlet in powershell, I would like to extract timestamp from output so that I can select jobs which has elapsed time > 24 hours

Command : Get-JAMSEntry -Name * -Server xxxxxx -State Executing

Output :

Entry  Name                   Description
-----  ----                   -----------
1     Sample                  Executing, elapsed time: 602.18:57:06.
3     Sample                  Executing, elapsed time: 481.18:57:02.
5     Sample                  Halted , elapsed time: 484.18:56:58.
7     Sample                  Hold, elapsed time: 680.18:56:55.
7     Sample                  Executing, elapsed time: 680.18:56:54.
7     Sample                  Executing, elapsed time: 680.18:56:53.
9     Sample                  Halted, elapsed time: 666.18:57:09.
1     Sample                  halted, elapsed time: 684.18:57:01.

Appreciate the help.

Thanks

Upvotes: 1

Views: 145

Answers (1)

user6811411
user6811411

Reputation:

When you pipe the output of your command to a Select-Object which inserts calculated properties by splitting the Description first at the , and then at the : like this:

Get-JAMSEntry -Name * -Server xxxxxx -State Executing |
    Select-Object Entry,Name,@{n='State';e={$_.Description.split(',')[0]}},
        @{n='ElapsedHrs';e={($_.Description.Split(',')[1]).Split(':')[1].Trim()}}

you'll get this output:

Entry Name   State     ElapsedHrs
----- ----   -----     ----------
1     Sample Executing 602.18
3     Sample Executing 481.18
5     Sample Halted    484.18
7     Sample Hold      680.18
7     Sample Executing 680.18
7     Sample Executing 680.18
9     Sample Halted    666.18
1     Sample Halted    684.18

you can further reduce with an appeded

|Where-Object {[double]$_.ElapsedHrs -gt 670}

to get only matching values

Entry Name   State     ElapsedHrs
----- ----   -----     ----------
7     Sample Hold      680.18
7     Sample Executing 680.18
7     Sample Executing 680.18
1     Sample halted    684.18

Upvotes: 1

Related Questions