VMware Cloud Community
TheVMinator
Expert
Expert
Jump to solution

Report fails with 'Exception calling "ReadNextEvents"'

I'm using get-vieventplus to get events from multiple vCenters on the same report.  The report runs for days and never completes.  It eats up all the RAM on the VM it is running on - even 12GB of RAM even though nothing else is running on this VM but this report.  When I look at the performance chart I can see it go from almost no RAM utilization before the report started, then rapidly up to consume all the RAM on the VM the report runs on, then continues at almost 100% RAM utilization for days.    When I log in I see this error on the console of the PowerShell ISE.  The error seems to repeat itself multiple times.  The error points to line 163 in the get-vieventplus function.  I highlighted line 163 that it refers to.  Also I included the report below:

#Define and connect to vCenters

$vcenters= "vcenter1","vcenter2","vcenter3","vcenter4"

Connect-VIServer -Server $vCenters -User user1 -Password password1

write-output "Connect-VIServer Complete"

#This section defines the variables $start and $finish

write-output "Calculating Previous Month Variables"

$now = Get-Date -Hour 0 -Minute 0 -Second 0

$daysBack = - $now.Day + 1

$Start = $now.AddMonths(-1).AddDays($daysBack)

$DaysInMonth = [DateTime]::DaysInMonth($Start.Year,$Start.Month)

$Finish = $Start.AddDays($DaysInMonth)

#Capture all events representing the creation of a new virtual machine

#For the stated time range

write-output "Getting VIEventPlus events"

$report = Get-VIEventPlus -start $start -finish $finish |

Where-Object {"VmCreatedEvent","VmClonedEvent","VmDeployedEvent" -contains $_.Gettype().Name} | %{

write-output "VIEventPlus Events captured.  Iterating through events %{}"

    $evtThisNewVMEvent = $_

   #Determine if the virtual machine is still in inventory.  If not, terminate the current iteration

    Try {

        $vm = Get-VM -Id $_.Vm.VM -ErrorAction Stop

        write-output "Try Clause"

    }

    Catch {

         write-output "Catch Clause"

        return

    }

    $vm | Select "-----",

    @{N="VM Name";E={$_.name}},

    @{N="vCPUCount";E={$_.NumCPU}},

    @{N="MemoryGB";E={$_.MemoryGB}},

    @{N="Guest OS";E={$_.guest.toString().Split(":")[1]}},

    @{N="ProvisionedSpaceGB";E={[math]::Round(($_.ProvisionedSpaceGB),1)}},

    @{N="UsedSpaceGB";E={[math]::Round(($_.UsedSpaceGB),1)}},

    @{N="FreeSpaceGB";E={[math]::Round(($_.ProvisionedSpaceGB - $_.UsedSpaceGB),1)}},

    @{N="Current ESXi Host";E={$_.vmhost.Name.Split(".")[0]}},

    @{N="vCenter Server";E={$_.Uid.Split(":")[0].Split("@")[1]}},

    @{N="createdTime";E={$evtThisNewVMEvent.createdTime}},

    @{N="Created By";E={$evtThisNewVMEvent.UserName}},

    @{N="VMID";E={$_.vm.vm}},

    @{N="Original ESXi Host";E={$_.VMHost.Name.Split(".")[0]}}  

} | Sort-Object createdTime

write-output "`$report object creation finalized. Calcuating Total variables"

$totalVMCount =$report.count

$totalvCPUCount = [math]::Round(($report | Measure-Object -Property vCPUCount -Sum | Select -ExpandProperty Sum),1)

$totalMemoryGB = [math]::Round(($report | Measure-Object -Property MemoryGB -Sum | Select -ExpandProperty Sum),1)

$totalProvisionedSpaceGB = [math]::Round(($report | Measure-Object -Property ProvisionedSpaceGB -Sum | Select -ExpandProperty Sum),1)

$totalUsedSpaceGB = [math]::Round(($report | Measure-Object -Property UsedSpaceGB -Sum | Select -ExpandProperty Sum),1)

$totalFreeSpaceGB = [math]::Round(($report | Measure-Object -Property FreeSpaceGB -Sum | Select -ExpandProperty Sum),1)

write-output "Total Variable calculation finalized. Calcuating Average variables"

$avgvCPUCount= [math]::Round(($report | Measure-Object -Property vCPUCount -Average | Select -ExpandProperty Average),1)

$avgMemoryGB = [math]::Round(($report | Measure-Object -Property MemoryGB -Average | Select -ExpandProperty Average),1)

$avgProvisionedSpaceGB = [math]::Round(($report | Measure-Object -Property ProvisionedSpaceGB -Average | Select -ExpandProperty Average),1)

$avgUsedSpaceGB = [math]::Round(($report | Measure-Object -Property UsedSpaceGB -Average | Select -ExpandProperty Average),1)

$avgFreeSpaceGB = [math]::Round(($report | Measure-Object -Property FreeSpaceGB -Average | Select -ExpandProperty Average),1)

write-output "Adding Custom Properties"

$report += New-Object PSObject -Property @{

"-----" = "------------"

}

$report += New-Object PSObject -Property @{

"-----" = "Totals"

"VM Name"= $totalVMCount

"vCPUCount" = $totalvCPUCount

"MemoryGB" = $totalMemoryGB

"ProvisionedSpaceGB" = $totalProvisionedSpaceGB

"UsedSpaceGB" = $totalUsedSpaceGB

"FreeSpaceGB" = $totalFreeSpaceGB

}

$report += New-Object PSObject -Property @{

"-----" = "Averages"

"vCPUCount" = $avgvCPUCount

"MemoryGB" = $avgMemoryGB

"ProvisionedSpaceGB" = $avgProvisionedSpaceGB

"UsedSpaceGB" = $avgUsedSpaceGB

"FreeSpaceGB" = $avgFreeSpaceGB

}

write-output $report

Here is the actual get-vieventplus function with the line highlighted that the error points to:

process {

    $eventnumber = 100

    $events = @()

    $eventMgr = Get-View EventManager

    $eventFilter = New-Object VMware.Vim.EventFilterSpec

    $eventFilter.disableFullMessage = ! $FullMessage

    $eventFilter.entity = New-Object VMware.Vim.EventFilterSpecByEntity

    $eventFilter.entity.recursion = &{if($Recurse){"all"}else{"self"}}

    $eventFilter.eventTypeId = $EventType

    if($Start -or $Finish){

      $eventFilter.time = New-Object VMware.Vim.EventFilterSpecByTime

    if($Start){

        $eventFilter.time.beginTime = $Start

    }

    if($Finish){

        $eventFilter.time.endTime = $Finish

    }

    }

  if($User -or $System){

    $eventFilter.UserName = New-Object VMware.Vim.EventFilterSpecByUsername

    if($User){

      $eventFilter.UserName.userList = $User

    }

    if($System){

      $eventFilter.UserName.systemUser = $System

    }

  }

  if($ScheduledTask){

    $si = Get-View ServiceInstance

    $schTskMgr = Get-View $si.Content.ScheduledTaskManager

    $eventFilter.ScheduledTask = Get-View $schTskMgr.ScheduledTask |

      where {$_.Info.Name -match $ScheduledTask} |

      Select -First 1 |

      Select -ExpandProperty MoRef

  }

  if(!$Entity){

    $Entity = @(Get-Folder -Name Datacenters)

  }

  $entity | %{

      $eventFilter.entity.entity = $_.ExtensionData.MoRef

      $eventCollector = Get-View ($eventMgr.CreateCollectorForEvents($eventFilter))

      $eventsBuffer = $eventCollector.ReadNextEvents($eventnumber)

      while($eventsBuffer){

        $events += $eventsBuffer

        $eventsBuffer = $eventCollector.ReadNextEvents($eventnumber)

      }

      $eventCollector.DestroyCollector()

    }

    $events

  }

}

get-vieventplus.jpg

0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

The Get-VIEventPlus function does not support connections to multiple vCenter Servers. You should run this function while only a single vCenter Server is connected.

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

View solution in original post

0 Kudos
2 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

The Get-VIEventPlus function does not support connections to multiple vCenter Servers. You should run this function while only a single vCenter Server is connected.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
TheVMinator
Expert
Expert
Jump to solution

OK thanks.  I did reformat the report to run against only on vCenter at a time.  Still having issues though which I'll address on a separate post

0 Kudos