Hi,
I need to get a VMs last power off date based on the VM's events
Please provide a powershell script for the above query.
Regards,
Jayachandran.N
Hi Jayachandran,
the next script gives you the last power off date and power on date for all virtual machines. The script uses the new PowerCLI v4.1 New-VIProperty cmdlet to extend the virtual machine object with the LastPoweredOffDate and LastPoweredOnDate properties. So be sure to use PowerCLI v4.1. Because the script uses square brackets and the forum software doesn't like them in code, I also attached the script. Only use the attached version because the version in the post will not work. I only inserted it so you will get an idea of how it works without having to download the attached script.
function Get-VMLastPoweredOffDate { param([Parameter(Mandatory=$true,ValueFromPipeline=$true)] [http://VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl|http://VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl] $vm) process { $Report = "" | Select-Object -Property Name,LastPoweredOffDate $Report.Name = $_.Name $Report.LastPoweredOffDate = (Get-VIEvent -Entity $vm | ` Where-Object { $_.Gettype().Name -eq "VmPoweredOffEvent" } | ` Select-Object -First 1).CreatedTime $Report } } function Get-VMLastPoweredOnDate { param([Parameter(Mandatory=$true,ValueFromPipeline=$true)] [http://VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl|http://VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl] $vm) process { $Report = "" | Select-Object -Property Name,LastPoweredOnDate $Report.Name = $_.Name $Report.LastPoweredOnDate = (Get-VIEvent -Entity $vm | ` Where-Object { $_.Gettype().Name -eq "VmPoweredOnEvent" } | ` Select-Object -First 1).CreatedTime $Report } } New-VIProperty -Name LastPoweredOffDate -ObjectType VirtualMachine -Value {(Get-VMLastPoweredOffDate -vm $Args[0]).LastPoweredOffDate} New-VIProperty -Name LastPoweredOnDate -ObjectType VirtualMachine -Value {(Get-VMLastPoweredOnDate -vm $Args[0]).LastPoweredOnDate} Get-VM | Select-Object -property Name,LastPoweredOnDate,LastPoweredOffDate
Regards, Robert
I made some modifications to the script to let the pipeline work.
Message was edited by: RvdNieuwendijk
Thanks for providing the script. Is there any way to identify who has powered off that machine.
I have added the usernames of the persons who have powered off and powered on the virtual machines to the second version of my script. This script is attached to this post. Because the script gets corrupted by the forum software I did not include it in the text.
Excellent The script is working fine :smileygrin: . I need to take this output in to Notpad or excel file. Can you please share.
Add the -Force parameter on the New-VIProperty cmdlet to overwrite an existing definition.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
With thanks to denovojeff who found a flaw in my script from this thread, I made a new version and posted it here: Re: VM Power Off Date.
Regards, Robert
Hi,
Can we save the result of script in form on .csv or .xls file and or email this report.
Thanks a bunch
Can I use the latest powercli here ?
Have a quick query RvdNieuwendijk ,We have 4-5 Datacenters grouped under one VCenter is it possible to get the script running for a specific datacenter and get the below details for the past 7 days...?
Regards
Pavan
@
I am not sure what you want, but will give it a try The following PowerCLI script will report the Name, LastPoweredOffDate. and LastPoweredOnDate of the virtual machines in datacenter Datacenter-02 where the last powered on date is within the last 7 days:
Get-Vm -Location Datacenter-02 | Select-Object -Property Name,LastPoweredOffDate.LastPoweredOnDate |
Where-Object {$_.LastPoweredOnDate -gt (Get-Date).AddDays(-7)}
Hi,
if anybody needs to know, when an VM was powered off, even if the vCenter Event does not exist, you can try to use my script attached to this post. It is based on the last line in vmware.log file.
Some credits to NiTRo from: Get-VMlog : suivre vmware.log en powershell - Hypervisor
Stephan
Love it. Fantastic work, all of you.