FreeNAS 11.3 API 2.0

Marjan

Dabbler
Joined
Jan 12, 2015
Messages
11
Hello,​

I am using FreeNAS 11.3 and I am trying to get alerts using API 2.0. I am doing this with powershell. I do get alerts no problem there. The problem is the date of alerts. This is script I am using:

Code:
$Credentials = IMPORT-CLIXML ".\SecureCredentials.xml"
$RESTAPIUser = $Credentials.UserName
$RESTAPIPassword = $Credentials.GetNetworkCredential().Password
$RESTAPIServer = "ServerFQDN"
$Uri = "https://" + $RESTAPIServer + "/api/v2.0/alert/list"
$Type = "application/json"

$Headers = @{ Authorization = "Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $RESTAPIUser, $RESTAPIPassword))) }
try {
    $result = Invoke-RestMethod -Uri $Uri -Headers $Headers -Method Get -SessionVariable Freenas_S -ContentType $Type
    $result
}
catch {}


This is one of alerts listed:
uuid : ed1857cb-2e2a-4603-95f9-65aab08f4064​
source :​
klass : ScrubFinished​
args : freenas-boot​
node : Controller A​
key : "freenas-boot"​
datetime : @{$date=1592099130997}​
dismissed : False​
mail :​
text : Scrub of pool %r finished.​
id : ed1857cb-2e2a-4603-95f9-65aab08f4064​
level : INFO​
formatted : Scrub of pool 'freenas-boot' finished.​
one_shot : True​

Powershell sees the value for $date as Int64, running [datetime]::FromFileTime($result[1].datetime.'$date') gives this:
Tuesday, January 2, 1601 21:13:29​

I get same date and time for all alerts, although the $date number is different for all the alerts.

I need information how can I convert $date to proper date and time. Any help is appreciated, small "push" to right direction should be enough :)

Cheers
 

Samuel Tai

Never underestimate your own stupidity
Moderator
Joined
Apr 24, 2020
Messages
5,399

Marjan

Dabbler
Joined
Jan 12, 2015
Messages
11
Thanks Samuel. The answer is in the thread that you mentioned.

If this will help anyone, the time returned are miliseconds. This is the code I used to convert to local date and time:
Code:
datetimeoffset]::FromUnixTimeMilliseconds($result.datetime.'$date')


Where $result is the full text of the alert.
 
Top