VMware Cloud Community
tutoki
Contributor
Contributor
Jump to solution

Date format problem

Hi,

I have this ps, that I used to collect information about ESX servers, and I need to insert this information in a MySQL DB.

The timestamp came with this format "14/02/2014 10:00:00" and I need to change it ti 2014-02-14 10:0:0. I have test with  -Useculture, format and ToString method also change regional setting without success

Any ideas will be appreciate

The original code

////////

$IntervalFinish = Get-Date

$IntervalStart = $IntervalFinish.addDays(-30)

$esx = Get-VMHost |Where-object {$_.powerstate -eq "poweredon"}

Get-Stat -entity $esx -stat cpu.usage.average, mem.usage.average -Start $IntervalStart -Finish $IntervalFinish -IntervalMins 120 | where{$_.instance -eq ""} | Export-Csv temp_monthl.csv -NoTypeInformation -UseCulture

////////////////////////

SALUD....

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

$IntervalFinish = Get-Date
$IntervalStart = $IntervalFinish.addDays(-30)
$esx = Get-VMHost |Where-object {$_.powerstate -eq "poweredon"}
Get-Stat -Entity $esx -Stat cpu.usage.average, mem.usage.average -Start $IntervalStart -Finish $IntervalFinish  -IntervalMins 120 |
where{$_.instance -eq ""} |
Select -ExcludeProperty Timestamp -Property *,@{N="TimeStamp";E={$_.Timestamp.ToString("yyyy-MM-dd hh:%m:%s")}} |
Export-Csv temp_monthl.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
5 Replies
CRad14
Hot Shot
Hot Shot
Jump to solution

This has the get-date formatting info

Windows PowerShell Tip: Formatting Dates and Times

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! :slightly_smiling_face:
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I assume you want the TimeStamp property in that specific format ?

Then you could do something like this

Get-Stat -Realtime -Stat cpu.usage.average -Entity $esx -MaxSamples 1 |
Select @{N="TimeStamp";E={$_.Timestamp.ToString("yyyy-MM-dd hh:%m:%s")}}

Don't mind the Get-Stat part, it's the calculated property


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
CRad14
Hot Shot
Hot Shot
Jump to solution

Atleast I got a couple responses in before The Wizard LucD returned Smiley Happy

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! :slightly_smiling_face:
tutoki
Contributor
Contributor
Jump to solution

Wow, works.

Obviuosly only TimeStamp is exported, How can I integrate with my own code?

Have I to include in the SELECT the name of the objets that I want to export?

SALUD

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

$IntervalFinish = Get-Date
$IntervalStart = $IntervalFinish.addDays(-30)
$esx = Get-VMHost |Where-object {$_.powerstate -eq "poweredon"}
Get-Stat -Entity $esx -Stat cpu.usage.average, mem.usage.average -Start $IntervalStart -Finish $IntervalFinish  -IntervalMins 120 |
where{$_.instance -eq ""} |
Select -ExcludeProperty Timestamp -Property *,@{N="TimeStamp";E={$_.Timestamp.ToString("yyyy-MM-dd hh:%m:%s")}} |
Export-Csv temp_monthl.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos