VMware Cloud Community
Mike1903
Contributor
Contributor

Script for getting Power off VMs date fails after a few VMs

Hi,

I have the script below that fails after about 5 VMs with 

Get-VIEvent : 11/10/2018 12:54:35       Get-VIEvent             Exception has been thrown by the target of an invocation.
At line:1 char:1
+ Get-VIEvent -Entity $VMs -MaxSamples ([int]::MaxValue) |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-VIEvent], VimException
    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetEvent

 

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

$Report = @()

 

$VMs = get-vm |Where-object {$_.powerstate -eq "poweredoff"}

 

Get-VIEvent -Entity $VMs -MaxSamples ([int]::MaxValue) |

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

Group-Object -Property {$_.Vm.Name} | %{

  $lastPO = $_.Group | Sort-Object -Property CreatedTime -Descending | Select -First 1

  $vm = Get-VM -Name $_.Name

  $row = '' | select VMName,Powerstate,PowerOFF

    $row.VMName = $vm.Name

    $row.Powerstate = $vm.Powerstate

    $row.PowerOFF = $lastPO.CreatedTime

    $report += $row

}

 

$report | Sort Name | Export-Csv -Path "C:\temp\Powered_Off_VMs11.csv" -NoTypeInformation -UseCulture

 

Any help would great.

Thanks

Michael

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership

Which PowerCLI version are you using?

There has been a bug in the Get-VIEvent cmdlet that was fixed in 10.2.0.


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

Reply
0 Kudos
Mike1903
Contributor
Contributor

6.5 release 1 build 4624819
Reply
0 Kudos
LucD
Leadership
Leadership

You will need to upgrade.
See Welcome PowerCLI to the PowerShell Gallery – Install Process Updates


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

Reply
0 Kudos
Mike1903
Contributor
Contributor

Thanks
Reply
0 Kudos