VMware Cloud Community
chr1s86
Enthusiast
Enthusiast
Jump to solution

PowerCLI find shutdown date from powered off VMs

The question is old, but nowadays where vents are only keept for a short time in vCenter DB its impossible to find it on the old way like https://code.vmware.com/forums/2530/vmware-powercli#540397

I'm wondering about this timestamp:

$vm = get-vm myVM

$vm.ExtensionData.Storage.Timestamp

 

Otherwise my toughts are going to filter powered off vms, find their folderpath and check the modify date of the nvram file.

Any other hints?

 

$vm = get-vm myVM
 
$vm.ExtensionData.Storage.Timestamp
Blog: http://vblog.hochsticher.de/
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I'm not sure why you say "...nowadays where events are only kept for a short time in vCenter DB".
That is still a decision you, as the administrator, make. If you have the disk space, you can make that retention longer.
And admittedly, the maximum is about 1 year.

The Timestamp in the VirtualMachineStorageInfo you mention is not showing the exact last shutdown date afaik.

As the API Reference states, it shows "Time when values in this structure were last updated."
This update happens when the VM is powered on and on a schedule.

The correctness of that timestamp will be within the update cycle interval length.

And just like the next option, this timestamp changes when the VM's files are moved, by for example a svMotion.

If you are not using SDRS, this might be a reasonable option.

The timestamp on the .nvram file is a slightly better option.
But also here, that timestamp is updated when the VMs files are moved.

Ultimately, the event DB is still the most reliable option (with all it's limitations).
The following snippet, which might take quite some time to complete, shows how accurate the different options are.
Make sure to do a svMotion, to see the impact on the timestamps.

Get-VM -Name MyVM |

Select Name,PowerState,

   @{N='Event Timestamp';E={

   (Get-VIevent -Entity $_ -MaxSamples ([int]::MaxValue) |

   where{$_ -is [VMware.Vim.VmPoweredOffEvent]} |

   Sort-Object -Property CreatedTime)[-1].CreatedTime

   }},

   @{N='Storage Timestamp';E={$_.ExtensionData.Storage.Timestamp.ToLocalTime()}},

   @{N='NVRAM Timestap';E={

   $dsName = ($_.ExtensionData.LayoutEx.File | where {$_.Type -eq 'nvram'}).Name.Split(' ')[0].Trim('[]')

   $ds = Get-Datastore -Name $dsName

   New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" | Out-Null

   $file = Get-ChildItem -Path "DS:\$($_.Name)\$($_.Name).nvram"

   Remove-PSDrive -Name DS -Confirm:$false

   $file.LastWriteTime

   }}

---------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.


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

View solution in original post

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

I'm not sure why you say "...nowadays where events are only kept for a short time in vCenter DB".
That is still a decision you, as the administrator, make. If you have the disk space, you can make that retention longer.
And admittedly, the maximum is about 1 year.

The Timestamp in the VirtualMachineStorageInfo you mention is not showing the exact last shutdown date afaik.

As the API Reference states, it shows "Time when values in this structure were last updated."
This update happens when the VM is powered on and on a schedule.

The correctness of that timestamp will be within the update cycle interval length.

And just like the next option, this timestamp changes when the VM's files are moved, by for example a svMotion.

If you are not using SDRS, this might be a reasonable option.

The timestamp on the .nvram file is a slightly better option.
But also here, that timestamp is updated when the VMs files are moved.

Ultimately, the event DB is still the most reliable option (with all it's limitations).
The following snippet, which might take quite some time to complete, shows how accurate the different options are.
Make sure to do a svMotion, to see the impact on the timestamps.

Get-VM -Name MyVM |

Select Name,PowerState,

   @{N='Event Timestamp';E={

   (Get-VIevent -Entity $_ -MaxSamples ([int]::MaxValue) |

   where{$_ -is [VMware.Vim.VmPoweredOffEvent]} |

   Sort-Object -Property CreatedTime)[-1].CreatedTime

   }},

   @{N='Storage Timestamp';E={$_.ExtensionData.Storage.Timestamp.ToLocalTime()}},

   @{N='NVRAM Timestap';E={

   $dsName = ($_.ExtensionData.LayoutEx.File | where {$_.Type -eq 'nvram'}).Name.Split(' ')[0].Trim('[]')

   $ds = Get-Datastore -Name $dsName

   New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" | Out-Null

   $file = Get-ChildItem -Path "DS:\$($_.Name)\$($_.Name).nvram"

   Remove-PSDrive -Name DS -Confirm:$false

   $file.LastWriteTime

   }}

---------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.


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

Reply
0 Kudos
chr1s86
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

thanks a lot!

I say nowadays, because default setting is now 30 Days. In older versions i tought this was a longer period.

Think I go with the faster extensiondata timestamp and live with the inaccuracy regarding to SDRS.

You helped me a lot!

Blog: http://vblog.hochsticher.de/
Reply
0 Kudos
chr1s86
Enthusiast
Enthusiast
Jump to solution

Found that the extensiondata timestamps also gets updated when you refresh the datastore capacity information. Smiley Sad

So, I now prefer the nvram file timestamp!

Blog: http://vblog.hochsticher.de/
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That only changes after a svMotion.
Watch out, the datastore PSProvider is not the fastest around.

You might want to consider getting all nvram files in one go, and then create your report from that list.

Something along these lines.
Note that this runs per datastore

$dsName = 'MyDatastore'

$ds = Get-Datastore -Name $dsName


New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" | Out-Null

$files = Get-ChildItem -Path "DS:" -Filter "*.nvram" -Recurse

Remove-PSDrive -Name DS -Confirm:$false


Get-VM -Datastore $ds |

select Name,

   @{N='PowerOff Time';E={

   $nvram = $_.ExtensionData.LayoutEx.File | where {$_.Type -eq 'nvram'}

   ($files | where{$_.DatastoreFullPath -eq $nvram.Name}).LastWriteTime

   }}


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

Reply
0 Kudos
chr1s86
Enthusiast
Enthusiast
Jump to solution

Brilliant! :smileycool:

I spend a beer or cigarettes at VMworld EU!

See you ... !

Blog: http://vblog.hochsticher.de/
Reply
0 Kudos
chr1s86
Enthusiast
Enthusiast
Jump to solution

Also possible:

$datastores = Get-Datastore MyDatastore

dir -recurse -Include "*.nvram" (get-datastore $datastores).datastorebrowserpath | select Name,LastWriteTime

Blog: http://vblog.hochsticher.de/
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Nice shortcut :smileycool:


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

Reply
0 Kudos