0

I have an array I would like to display formatted. I have the start and end time in ticks to do some grouping and get a min/max (you can't do min/max with Measure on a datetime format). The issue is I want to display the final output as a datetime, not ticks. I searched and have this code, but can't get it to work. I get an error "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not valid or not in the correct sequence"

$OpenArr | Group-Object Name | %{
    New-Object psobject -Property @{
        Item = $_.Name
        Sum = ($_.Group | Measure-Object TotalSeconds -Sum)
        StartTime = ($_.Group | Measure-Object StartTime -MIN)
        EndTime = ($_.Group | Measure-Object EndTime -MAX)
    }
}

#$OpenArr

$c1 = @{Expression={$_.Name}}
$c2 = @{Expression={$_.StartTime.ToString("yyyyMMdd")};Label="Start"}

$OpenArr | Sort-Object Name  | Format-Table $c1,$c2,TotalSeconds

I also tried just casting the StartTime as a datetime. In playing with it, that isn't even the problem. When I just do Format-table $c1, it gives an error too.

4
  • In your objects that you create I don't see a "TotalSeconds" property that you're using in Format-Table. You're not changing $OpenArr. Just keep it all in one pipeline $OpenArr | Group-Object Name | % {New-Object psobject ...} | Sort-Object Item | Format-Table Item,StartTime,EndTime. Not sure how you want to display DateTime from ticks? Like this, [datetime]1000000000 - [datetime]0? Commented Jun 4, 2013 at 16:06
  • The point is changing the ticks to a date. This is doen wtih a cast to datetime, but that isn't working in the expression. The rest all works (except Sum should have read TotalSeconds). Casting in the Format-TAble without an expression doesn't work: $OpenArr | Sort-Object Name | Format-Table Name,[datetime]StartTime,EndTime,TotalSeconds. Commented Jun 4, 2013 at 16:38
  • So in your expression $c2 = @{Expression={$_.StartTime.ToString("yyyyMMdd")};Label="Start"} the "$_.StartTime" is in ticks? Commented Jun 4, 2013 at 16:56
  • Yes, it is in ticks. I tried casting it in the expression prefixing it with [datetime]. Commented Jun 4, 2013 at 18:24

1 Answer 1

1

Try using a TimeSpan structure:

$c2 = @{l="Start";e={New-Object -Type System.TimeSpan -Arg $_.StartTime}}
$c3 = @{l="End";e={New-Object -Type System.TimeSpan -Arg $_.EndTime}}

$OpenArr | sort Name | Format-Table Name,$c2,$c3,TotalSeconds

or, using the [TimeSpan] type accelerator as suggested by @BobLobLaw:

$c2 = @{l="Start";e={[TimeSpan]$_.StartTime}}
$c3 = @{l="End";e={[TimeSpan]$_.EndTime}}

$OpenArr | sort Name | Format-Table Name,$c2,$c3,TotalSeconds
Sign up to request clarification or add additional context in comments.

5 Comments

You could use the type accelerator [timespan]5403666660000.
I get an error: The object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not valid or not in the correct sequence. This is likely caused by a use r-specified "format-table" command which is conflicting with the default formatting. + CategoryInfo : InvalidData: (:) [out-lineoutput], InvalidOperationException + FullyQualifiedErrorId : ConsoleLineOutputOutOfSequencePacket,Microsoft.PowerShell.Commands.OutLineOutputCommand
To add a little more info, I am getting the date from a logfile and converting it: $StartTime = $line.substring(0,19) $StartTime= [DateTime]$StartTime $StartTime = $StartTime.Ticks
Even this gives the error: $OpenArr | sort Name | Format-Table Name. That is why I am confused...it doesn't seem to have to do with the Dates.
Thanks for the help. To be honest, there were several problems with the code. It turned out the measure tunred the int64 to something else and it couldn't then convert it to a datetime. I cast it as a Int64 first and then a datetime and it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.