Automation

 View Only
  • 1.  Convert Get-Date into Unix EPOCH Format

    Posted Feb 12, 2018 01:09 PM

    I am trying to convert date into Unix epoch format as I need to insert data into influxdb with historic timestamp.

    (Get-Date $StartDate -UFormat '%s').Replace((Get-Culture).NumberFormat.NumberDecimalSeparator, '')

    -> I am getting a value of 15 digits.

    But I am not able to insert into influxdb because, as per InfluxData | Documentation | Writing Data

    date stamp should be in nanoseconds and is 19 digits.

    So how can I get datestamp in nanoseconds and is in unix epoch format.



  • 2.  RE: Convert Get-Date into Unix EPOCH Format
    Best Answer

    Posted Feb 12, 2018 01:29 PM

    The difference in length is due to the base you are using.

    With UFormat '%s' you are calculating "Seconds elapsed since January 1, 1970 00:00:00"

    But you need nanoseconds, hence the longer number.

    The following show the 2 methods.

    $now = Get-Date

    (Get-Date -Date $now -UFormat '%s').Replace((Get-Culture).NumberFormat.NumberDecimalSeparator,'')

    $unixEpochStart = New-Object DateTime 1970,1,1,0,0,0,([DateTimeKind]::Utc)

    [int64]((([datetime]$now) - $unixEpochStart).TotalMilliseconds*1000000)



  • 3.  RE: Convert Get-Date into Unix EPOCH Format

    Posted Feb 12, 2018 05:05 PM

    Excellent! InfluxDB is great and it's quite easy to get things going. Check out my github for example usage at:

    GitHub - vmkdaily/vFlux-Stats-Kit: PowerCLI scripts to gather VMware performance stats and write them to InfluxDB

    As for the timestamp I think you should be good to go now, but for completeness, this is how my scripts do it:

    ## nanoseconds since Unix epoch

    [long]$timestamp = (([datetime]::UtcNow)-(Get-Date -Date '1/1/1970')).TotalMilliseconds * 1000000

    Note:  I used to do [int64], which worked fine, but now I do [long] for whatever reason. Maybe a recommendation from one of my million dollar script editors :smileygrin:

    Tip: I see you are already aware of PowerShell culture which is good.  When writing to InfluxDB you may observe that it prefers the PowerShell culture to be en-US or your stats that looks like '0,57' won't write when InfluxDB was expecting them to look like '0.57'.

    PS - Also check out VMware Wavefront as an alternative:

    https://www.wavefront.com/



  • 4.  RE: Convert Get-Date into Unix EPOCH Format

    Posted Feb 13, 2018 06:27 AM

    Really?
    The guy repeats my answer, and gets the Correct Answer?!?



  • 5.  RE: Convert Get-Date into Unix EPOCH Format

    Posted Feb 13, 2018 06:49 AM

    Sorry, That was by mistake..



  • 6.  RE: Convert Get-Date into Unix EPOCH Format

    Posted Feb 13, 2018 07:13 AM

    Sorry, nothing personal, but I see this happening way too often.

    People rewording answers.
    It must be vExpert application time :smileygrin: