1

I have a list of several thousand custom PS Objects that are exported from another system, and all these custom objects are in a variable $queryResult. The system exports these objects with a field time that is in epoch time format in milliseconds (example: 1492536777453 = Tuesday, April 18, 2017 1:32:57 PM) . I need to change all of these time fields in the custom objects into human readable times. I already have a function that converts epoch time to human readable:

Function Convert-FromUnixDate ($UnixDate) {[timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddMilliSeconds($UnixDate))}

I also figured out how to convert all of them, however this just returns a list of times(obviously), and I can't figure out how to update the actual custom objects:

foreach ($i in $queryResult.events.time){$humanReadable = @{};Convert-FromUnixDate $i}

My problem is I want to update the actual value in each PS custom object so that, when I export all the objects to an excel file for the end user, they are readable. Your help is very much appreciated!

EDIT

I forgot to mention what happens when I try to update the values. I tried this statement below:

$aqlQueryResult.events.starttime = Convert-FromUnixDate ($aqlQueryResult.events.starttime)

When I try that, I receive the following error:

Cannot convert argument "value", with value: "System.Object[]", for 
"AddMilliseconds" to type "System.Double": "Cannot convert the 
"System.Object[]" value of type "System.Object[]" to type "System.Double"."

I understand that I'm receiving this error because my conversion function expects a double, but how can I either change this to expect the correct data type, or find another way to do this?

5
  • 2
    Values are set using =. Where is it? (except for the unused hashtable) Commented Apr 18, 2017 at 19:14
  • 1
    You could use a combination of Select-Object, Foreach-Object, and New-Object, I guess this could be done in a single pipeline. You have the main parts of it already. Commented Apr 18, 2017 at 19:20
  • Thanks @FrodeF, that was a silly mistake! I've updated my post to reflect where I tried the = Commented Apr 18, 2017 at 19:40
  • 1
    Now combine the two samples. Your can't mass edit an array, try foreach :-) Commented Apr 18, 2017 at 19:42
  • .Net 4.6 has [System.DateTimeOffset]::FromUnixTimeMilliseconds($UnixDate).LocalDateTime Commented Apr 19, 2017 at 1:58

1 Answer 1

1

So with a bit of help from @FrodeF and @sodawillow, I have figured out how to update the values inside the actual objects! Below is the line I needed to add.

$queryResult.events | ForEach-Object {$_.time = (Convert-FromUnixDate ($_.time))}
Sign up to request clarification or add additional context in comments.

1 Comment

You should avoid parens when calling a PowerShell function; it works for one parameter, but it won't work as you expect for multiple parameters, it will pass them all as the first parameter.

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.