# Date format is in string

**URL:** <https://discuss.elastic.co/t/date-format-is-in-string/235288>\
**Category:** Elasticsearch\
**Created:** [June 2, 2020, 7:58am UTC](https://discuss.elastic.co/t/date-format-is-in-string/235288 "2020-06-02T07:58:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sridhar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sridhar/32/64860_2.png) [@Sridhar](https://discuss.elastic.co/u/Sridhar)\
**Post date:** [June 2, 2020, 7:58am UTC](https://discuss.elastic.co/t/date-format-is-in-string/235288/1 "2020-06-02T07:58:16Z")

</div>

Hi all,  
Below is the powershell script I try to push the data to Elasticsearch. I'm able to do that. but the @timestamp field is loaded as string. Please help me.. what exactly I'm missing.. I'm not using logstash.. everything I need to manage in powershell

```auto
$hstnme=Hostname
$Time = (get-date).tostring("dd-MM-yyyy HH:mm:ss z")
$ComputerMemory = Get-WmiObject -Class WIN32_OperatingSystem
$Memory = ((($ComputerMemory.TotalVisibleMemorySize - $ComputerMemory.FreePhysicalMemory)*100)/ $ComputerMemory.TotalVisibleMemorySize)
$CPUFull = (Get-WmiObject -class win32_processor | Measure-Object -property LoadPercentage -Average)
$CPU = $CPUFull.Average
$diskdetails = Get-WmiObject win32_logicaldisk |select name,freespace,size,@{Name='Used %';Expression={100-($_.freespace / $_.size)*100}}
$services = Get-Content "C:\serv.txt"
$serv = foreach ($serv in $services){
get-service $serv | select Name,Status
}
$Object = New-Object PSObject -Property ([ordered]@{ 

		"@timestamp" = (get-date).tostring("dd-MM-yyyy HH:mm:ss z")
		"Hostname" = $hstnme
		"CPU" = $CPU
		"Memory" = $Memory
		"Disk" = $diskdetails
		"Services" = $serv 
})
$jobj = $Object | convertTo-Json
$dte=(Get-Date).tostring("MM-dd-yyyy")
$url = "http://1.2.3.4:9200/lndex-$dte/stats
Invoke-RestMethod -Method Post -Uri $url -ContentType 'application/json'-Body $jobj -ErrorAction Stop | Out-Null

```

Thanks,

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [June 3, 2020, 5:54am UTC](https://discuss.elastic.co/t/date-format-is-in-string/235288/2 "2020-06-03T05:54:49Z")

</div>

> [@Sridhar](#):
>
> `"@timestamp" = (get-date).tostring("dd-MM-yyyy HH:mm:ss z")`

This format is not one that will be inferred by Elasticsearch to be a `date` field data type using the default `format` of a `date` data type.

You have a couple of options

1. Create a mapping when creating the index that defines the `@timestamp` field explicitly as a `date` type and [provides a format that matches the one you'll be sending](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html)

2. Use a date format that will be inferred to be `date` type by Elasticsearch. For example, if you change to

```powershell
"@timestamp" = (get-date).tostring("yyyy-MM-dd'T'HH:mm:sssz")

```

Then it will be mapped as a `date` datatype. You can check this by performing a `GET` request on the index.

Personally, I would go with option 2, unless you have a compelling reason to use a different format.

* * *

An aside, you may be interested in using the [`Elastic.Console` PowerShell module](https://www.powershellgallery.com/packages/Elastic.Console/7.7.0-rc1) to index this into Elasticsearch. The script would look like

```powershell
$hstnme=Hostname
$ComputerMemory = Get-WmiObject -Class WIN32_OperatingSystem
$Memory = ((($ComputerMemory.TotalVisibleMemorySize - $ComputerMemory.FreePhysicalMemory)*100)/ $ComputerMemory.TotalVisibleMemorySize)
$CPUFull = (Get-WmiObject -class win32_processor | Measure-Object -property LoadPercentage -Average)
$CPU = $CPUFull.Average
$diskdetails = Get-WmiObject win32_logicaldisk |select name,freespace,size,@{Name='Used %';Expression={100-($_.freespace / $_.size)*100}}
$services = Get-Content "C:\serv.txt"
$serv = foreach ($serv in $services) {
    get-service $serv | select Name,Status
}

$body = @{ 
    "@timestamp" = (Get-Date).ToString("o")
    "Hostname" = $hstnme
    "CPU" = $CPU
    "Memory" = $Memory
    "Disk" = $diskdetails
}

$dte=(Get-Date).tostring("MM-dd-yyyy")
$url = "http://1.2.3.4:9200/lndex-$dte"

es $url -Body $body -ErrorAction Stop | Out-Null

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 1, 2020, 5:54am UTC](https://discuss.elastic.co/t/date-format-is-in-string/235288/3 "2020-07-01T05:54:51Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
