VMware Cloud Community
Jaya2010
Contributor
Contributor

Get a VMs last power off date based on the VM's events

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

15 Replies
RvdNieuwendijk
Leadership
Leadership

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

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Jaya2010
Contributor
Contributor

Thanks for providing the script. Is there any way to identify who has powered off that machine.

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

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.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Jaya2010
Contributor
Contributor

Excellent The script is working fine :smileygrin: . I need to take this output in to Notpad or excel file. Can you please share.

Reply
0 Kudos
russjar
Enthusiast
Enthusiast

I'm getting this error...? (see attached)

VCP,MCSE NT4/W2k/W2k3, MCSA W2k3

VCP,MCSE NT4/W2k/W2k3, MCSA W2k3
Reply
0 Kudos
LucD
Leadership
Leadership

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

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

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

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
vinod_sikka
Enthusiast
Enthusiast

Hi,

Can we save the result of script in form on .csv or .xls file and or email this report.

Thanks a bunch

Reply
0 Kudos
esxi1979
Expert
Expert

Can I use the latest powercli here ?

Reply
0 Kudos
McKenning
Contributor
Contributor

I've tested the attached script (taken from v2 of the OP's script) on PowerCli 5.8.

The script has been modified to connect to the localhost vCenter and then output the results to a csv file in the same directory as the script.

McKenning VCP5-DCV
Reply
0 Kudos
alumini2005
Contributor
Contributor

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

@

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

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)}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
GiteshKarande
Contributor
Contributor

Guys, Scripts are working but when we checked the out put file no powered off date show in tab. please help me. attached file your reference

Thanks for your prompt support.  

Reply
0 Kudos
Stephan84
Contributor
Contributor

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

ktungeman
Contributor
Contributor

Love it. Fantastic work, all of you.

Reply
0 Kudos