mikeely
Contributor
Contributor

How to print seconds from Get-VIEvent?

We're trying to track snapshot consolidation times on some VMs and when I look on the VSphere web console I get full timestamps such as "01/14/2021, 1:19:17 AM" for snapshot deleted and "01/14/2021, 1:19:33 AM" for virtual disk consolidation succeeded.

Unfortunately, when I pull the same entity using Get-VIEvent all I have for the CreatedTime field for both entries is "1/14/2021 1:19" without the seconds. Clearly there must be some flag or parameter I can use to get the complete timestamp, right?

0 Kudos
3 Replies
LucD
Leadership
Leadership

That is caused by the default format for the objects returned by Get-VIEvent.
The property is a DateTime object, so you have access.
It just requires a calculated property.
For example

Get-VIEvent -MaxSamples 1 | 
Select @{N='CreatedTime';E={$_.CreatedTime.ToString('HH:mm:ss')}}


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

mikeely
Contributor
Contributor

This is really good, thanks! Now I just need to tie this in with what I'm currently trying to do, which goes like this:

foreach ($vm in (Get-VMHost -Name $hostname | Get-VM)) {Get-VIEvent -Entity $vm | Export-Csv -Path C:\foo.csv -Force -Append }

So far I've integrated your suggestion as follows, but it only returns the timestamps:

foreach ($vm in (Get-VMHost -Name $hostname | Get-VM)) {Get-VIEvent -Entity $vm | Select @{N='CreatedTime';E={$_.CreatedTime.ToString('HH:mm:ss')}}}

What I need is the VM Name, and (associated by ChainId I think) the Task: Remove snapshot and linked "Virtual machine foobar disks consolidated successfully" from FullFormattedMessages.

I can live with the output as in the first one-liner and grind up the data in Excel but I'm always open to a better way.

0 Kudos
LucD
Leadership
Leadership

You could do something like this

$report = foreach ($vm in (Get-VMHost -Name $hostname | Get-VM)) {
    Get-VIEvent -Entity $vm | 
    Select -ExcludeProperty CreatedTime *,@{N='CreatedTime';E={$_.CreatedTime.ToString('HH:mm:ss')}}
}

$report | Export-Csv -Path C:\foo.csv -NoTypeInformation -UseCulture


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