VMware Cloud Community
chicojr
Enthusiast
Enthusiast
Jump to solution

VMs powered off

We have ESXi 5.5, currently need to run a command to list all vms power off date and who powered them off.

Your help is greatly appreciated!

0 Kudos
1 Solution

Accepted Solutions
bayupw
Leadership
Leadership
Jump to solution

That thread is linked to another thread which has updated script

Re: VM Power Off Date

function Get-VMLastPoweredOffDate {
 
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [
VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl] $vm)
 
process {
   
$Report = "" | Select-Object -Property Name,LastPoweredOffDate,UserName
   
$Report.Name = $vm.Name
   
$Event = Get-VIEvent -Entity $vm -MaxSamples 10000 | `
      Where-Object {
$_.GetType().Name -eq "VmPoweredOffEvent" } | `
     
Sort-Object -Property CreatedTime -Descending | `
     
Select-Object -First 1
   
$Report.LastPoweredOffDate = $Event.CreatedTime
   
$Report.UserName = $Event.UserName
   
$Report
  }
}

function Get-VMLastPoweredOnDate {
 
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [
VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl] $vm)

 
process {
   
$Report = "" | Select-Object -Property Name,LastPoweredOnDate,UserName
   
$Report.Name = $vm.Name
   
$Event = Get-VIEvent -Entity $vm -MaxSamples 10000 | `
      Where-Object {
$_.GetType().Name -eq "VmPoweredOnEvent" -or $_.GetType().Name -eq "DrsVmPoweredOnEvent"} | `
     
Sort-Object -Property CreatedTime -Descending |`
     
Select-Object -First 1
   
$Report.LastPoweredOnDate = $Event.CreatedTime
   
$Report.UserName = $Event.UserName
   
$Report
  }
}

New-VIProperty -Name LastPoweredOffDate -ObjectType VirtualMachine -Value {(Get-VMLastPoweredOffDate -vm $Args[0]).LastPoweredOffDate} -Force
New-VIProperty -Name LastPoweredOffUserName -ObjectType VirtualMachine -Value {(Get-VMLastPoweredOffDate -vm $Args[0]).UserName} -Force
New-VIProperty -Name LastPoweredOnDate -ObjectType VirtualMachine -Value {(Get-VMLastPoweredOnDate -vm $Args[0]).LastPoweredOnDate} -Force
New-VIProperty -Name LastPoweredOnUserName -ObjectType VirtualMachine -Value {(Get-VMLastPoweredOnDate -vm $Args[0]).UserName} -Force

Get-VM | Select-Object -property Name,LastPoweredOnDate,LastPoweredOnUserName,LastPoweredOffDate,LastPoweredOffUserName


Have you try above script?

Bayu Wibowo | VCIX6-DCV/NV
Author of VMware NSX Cookbook http://bit.ly/NSXCookbook
https://github.com/bayupw/PowerNSX-Scripts
https://nz.linkedin.com/in/bayupw | twitter @bayupw

View solution in original post

0 Kudos
5 Replies
bayupw
Leadership
Leadership
Jump to solution

You can use PowerCLI and script posted in this thread Get a VMs last power off date based on the VM's events

Bayu Wibowo | VCIX6-DCV/NV
Author of VMware NSX Cookbook http://bit.ly/NSXCookbook
https://github.com/bayupw/PowerNSX-Scripts
https://nz.linkedin.com/in/bayupw | twitter @bayupw
0 Kudos
chicojr
Enthusiast
Enthusiast
Jump to solution

None appear to work, just hang with no output what so ever, can you or someone put together a quick PS script so that I can edit to interact with my environment please.

0 Kudos
bayupw
Leadership
Leadership
Jump to solution

That thread is linked to another thread which has updated script

Re: VM Power Off Date

function Get-VMLastPoweredOffDate {
 
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [
VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl] $vm)
 
process {
   
$Report = "" | Select-Object -Property Name,LastPoweredOffDate,UserName
   
$Report.Name = $vm.Name
   
$Event = Get-VIEvent -Entity $vm -MaxSamples 10000 | `
      Where-Object {
$_.GetType().Name -eq "VmPoweredOffEvent" } | `
     
Sort-Object -Property CreatedTime -Descending | `
     
Select-Object -First 1
   
$Report.LastPoweredOffDate = $Event.CreatedTime
   
$Report.UserName = $Event.UserName
   
$Report
  }
}

function Get-VMLastPoweredOnDate {
 
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [
VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl] $vm)

 
process {
   
$Report = "" | Select-Object -Property Name,LastPoweredOnDate,UserName
   
$Report.Name = $vm.Name
   
$Event = Get-VIEvent -Entity $vm -MaxSamples 10000 | `
      Where-Object {
$_.GetType().Name -eq "VmPoweredOnEvent" -or $_.GetType().Name -eq "DrsVmPoweredOnEvent"} | `
     
Sort-Object -Property CreatedTime -Descending |`
     
Select-Object -First 1
   
$Report.LastPoweredOnDate = $Event.CreatedTime
   
$Report.UserName = $Event.UserName
   
$Report
  }
}

New-VIProperty -Name LastPoweredOffDate -ObjectType VirtualMachine -Value {(Get-VMLastPoweredOffDate -vm $Args[0]).LastPoweredOffDate} -Force
New-VIProperty -Name LastPoweredOffUserName -ObjectType VirtualMachine -Value {(Get-VMLastPoweredOffDate -vm $Args[0]).UserName} -Force
New-VIProperty -Name LastPoweredOnDate -ObjectType VirtualMachine -Value {(Get-VMLastPoweredOnDate -vm $Args[0]).LastPoweredOnDate} -Force
New-VIProperty -Name LastPoweredOnUserName -ObjectType VirtualMachine -Value {(Get-VMLastPoweredOnDate -vm $Args[0]).UserName} -Force

Get-VM | Select-Object -property Name,LastPoweredOnDate,LastPoweredOnUserName,LastPoweredOffDate,LastPoweredOffUserName


Have you try above script?

Bayu Wibowo | VCIX6-DCV/NV
Author of VMware NSX Cookbook http://bit.ly/NSXCookbook
https://github.com/bayupw/PowerNSX-Scripts
https://nz.linkedin.com/in/bayupw | twitter @bayupw
0 Kudos
chicojr
Enthusiast
Enthusiast
Jump to solution

how can I dump this off into a csv please?

0 Kudos
bayupw
Leadership
Leadership
Jump to solution

You just need to pipe the last command with Export-Csv Using the Export-Csv Cmdlet

Get-VM | Select-Object -property Name,LastPoweredOnDate,LastPoweredOnUserName,LastPoweredOffDate,LastPoweredOffUserName | Export-Csv - Path "D:\Output\poweredOffVM.csv"

Bayu Wibowo | VCIX6-DCV/NV
Author of VMware NSX Cookbook http://bit.ly/NSXCookbook
https://github.com/bayupw/PowerNSX-Scripts
https://nz.linkedin.com/in/bayupw | twitter @bayupw
0 Kudos