Automation

 View Only
  • 1.  Powershell out of memory error

    Posted Jun 23, 2011 01:48 AM

    Anyone seen this before?  Seems Powershell is consuming a huge amount of memory over the course of a heavy, reporting script.

    Get-View : 6/22/2011 9:44:32 PM    Get-View    C2C68470-B5E0-4388-B595-CD931CCB1F20    Exception of type 'System.OutOfM
    emoryException' was thrown.
    At S:\SCRIPTS\READ ONLY SCRIPTS\MAC Addess Compare\esx_vswitch_policy_report.ps1:1 char:9
    + Get-View <<<<  -ViewType HostSystem | ForEach-Object {
        + CategoryInfo          : NotSpecified: (:) [Get-View], VimException
        + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.Commands.DotNetInterop.GetVIView



  • 2.  RE: Powershell out of memory error

    Posted Jun 23, 2011 02:22 AM

    Hello, mark.chuman-

    I have encountered this before.  There are several things that you can do in your scripts to allow memory to be released back to the system.  One thing that is probably directly relevant to the script you were running when you received that error (since the error is on call to Get-View):

    use the "-Property" parameter for Get-View so as to retrieve only the properties that you will be using later in the script

    This will not only greatly reduce the amount of memory consumed, it will substantially increase the speed of the call, as Get-View will only be retrieving the given properties from the .Net View objects, instead of all of the properties (default).

    Example:  say you wanted to get host memory info, you could use the following to just retrieve the pertinent info:

    Get-View -ViewType HostSystem -Property Name,Hardware.MemorySize | `

         Select Name,@{n="Mem(GB)"; e={[Math]::round($_.Hardware.MemorySize / 1GB, 0)}}

    That way, rather than taking the extra time and consuming the extra memory to retrieve all of the properties of the HostSystem View object, you get just the ones that you intend to utilize later in the script.

    Another way to save memory is to utilize the pipeline, rather than storing results in an array/hashtable.  It can take a bit of re-writing, and some extra planning for the script, but it is well worth it to not run out of memory in PowerShell when the script is 75 minutes in...

    If you feel like sharing that script, I'm sure that we could optimize it such that it will cause no more memory issues.



  • 3.  RE: Powershell out of memory error

    Posted Jun 23, 2011 12:04 PM

    Thanks for the information.  The script is probably running up against problems also due to the large environment.  The vCenter it was scanning had 150+ hosts on it.  It looks like the breaking point is about 130 hosts as it succeeded on 2 other vCenter servers managing under 150 hosts.  Thank you.

    Get-View -ViewType HostSystem | ForEach-Object {
      $VMHostView =$_
      $VMHostView.Config.Network.vSwitch | where {$_.Spec.Policy.Security.macChanges -eq $true -or $_.Spec.Policy.Security.ForgedTransmits -eq $true} |`
    ForEach-Object {
        $Report = "" | Select-Object -Property VMHost
        $Report.VMHost = $VMHostView.Name
        $Report
      }
    }



  • 4.  RE: Powershell out of memory error
    Best Answer

    Posted Jun 23, 2011 12:17 PM

    Can you try this version ?

    Get-View -ViewType HostSystem -Property Name,Config.Network.vSwitch | `
    where {($_.Config.Network.vSwitch | where{$_.Spec.Policy.Security.macChanges -eq $true -or $_.Spec.Policy.Security.ForgedTransmits -eq $true})} | `
    Select Name


  • 5.  RE: Powershell out of memory error

    Posted Jun 23, 2011 12:34 PM

    Awesome.  Huge difference, thanks.