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!
That thread is linked to another thread which has updated script
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?
You can use PowerCLI and script posted in this thread Get a VMs last power off date based on the VM's events
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.
That thread is linked to another thread which has updated script
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?
how can I dump this off into a csv please?
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"