Trying to run a script to collect snapshot info. Sometimes it will work successfully and other times I get this error...
Get-View : 2/21/2011 4:12:48 PM Get-View Exception of type 'System.Ou
tOfMemoryException' was thrown.
At C:\MyDocs\work\Virtualization\VMWare\VMware_Healthcheck\Notes_Fields\TCOMDB_
Data\005c_snapshot-size-vcell03.ps1:17 char:16
+ $vms = Get-View <<<< -ViewType VirtualMachine
+ CategoryInfo : NotSpecified: (:) [Get-View], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomatio
n.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView
Thanks.
Would that by any chance be the script from this post Gathering snapshot size info takes 10+ hours ?
That script uses a recursive function and depending on the size of the environment and the number of snapshots it could consume quite a bit of memory I'm afraid.
No, that one is for the VI3 environment and still takes 10 hours. This particular one is for vSphere 4.1 and when it runs only takes ~15 minutes. The problem is that it doesn't always run and if I close the vSphere PowerCLI and try again, sometimes it works. Today it took about five tries which is why I posted.
$DiskInfoDef = @"
public struct DiskInfo {
public string VMname;
public string TotalSnapMB;
"@
$vms = Get-View -ViewType VirtualMachine
$maxSnapshot = ($vms | %{$_.LayoutEx.Snapshot.Count} | Measure-Object -Maximum).Maximum
1..$maxSnapshot | %{
$DiskInfoDef += ("`n`tpublic string Snap" + $_ + "MB;")
}
$DiskInfoDef += "`n}"
Add-Type -Language CsharpVersion3 -TypeDefinition $DiskInfoDef
$snapHash = @{}
filter Get-SnapHash{
$snapHash[$_.Snapshot.Value] = $_.Name
if($_.ChildSnapshotList){
$_.ChildSnapShotList | Get-SnapHash
}
}
$vms | %{
$vm = $_
if($vm.Snapshot){
$vm.Snapshot.RootSnapshotList | Get-SnapHash
}
$_.Config.Hardware.Device | where {$_.DeviceInfo.Label -like "Hard disk *"} | %{
$hd = $_
$vm.LayoutEx.Disk | where {$_.Key -eq $hd.Key} | %{
$diskFiles = $_.Chain | %{$_.FileKey} | %{$_}
}
$diskInfo = New-Object DiskInfo
$diskInfo.VMname = $vm.Name
$snapNr = 1
if($vm.Snapshot){
$totalSnapMB = 0
$vm.LayoutEx.Snapshot | %{
$_.Disk | where {$_.Key -eq $hd.Key} | %{
$prevFiles = $_.Chain | %{$_.FileKey} | %{$_}
}
$vm.LayoutEx.Disk | where {$_.Key -eq $hd.Key} | %{
foreach($chain in $_.Chain){
if($prevFiles -notcontains $chain.FileKey[0] -and $prevFiles -notcontains $chain.FileKey[1]){
break
}
}
}
$snapFiles = $chain.FileKey | %{$_}
$snapSize = ""
if($snapFiles){
$snapAllocated = 0
$vm.LayoutEx.File | where {$snapFiles -contains $_.Key} | %{
$snapAllocated += $_.Size
}
$snapSize = ("{0:N1}" -f ($snapAllocated / 1MB))
}
$diskInfo.("Snap" + $snapNr + "MB") = $snapSize
$totalSnapMB += ($snapAllocated / 1MB)
$snapNr++
}
$totalSnapMB = ($totalSnapMB/1024)
$diskInfo.TotalSnapMB = ("{0:N1}" -f $totalSnapMB)
}
$diskInfo
}
} | Export-Csv $filename1 -UseCulture -NoTypeInformation
That's the script I meant.
The filter Get-SnapHash is used recursively.
Do you have many snapshots on your guests ?
Are you perhaps running the script on a client that has a rather low amount of physical memory ?
Can you check with the Task Manager or Sysinternal's Process Explorer how much memory your PS script claims ?
Do you run the script from the PowerCLI prompt ? Or from something like PowerGUI ?
Yes, this is a large infrastructure with ~1300 VMs and an abundance of snapshots.
I am running it right in PowerCLI.
I am running on a Windows 7 VM with 4 GB RAM. Looking at the performance, it barely spikes to 40%.
Watching it in Task Manager, it does get up to about 682,000K and errors out.
Are you running this in the 64-bit version of PowerCLI ?
If not, can you give it a try ?
I don't see a 64 bit version on VMware's site. I upgraded to 4.1.1.2816, but still getting the same issue.
When you install PowerCLI (there's just a single download) on a 64 bit system you end up with a shortcut for "Mware vSphere PowerCLI (32-Bit)"and another for "Mware vSphere PowerCLI".
ugh, my OS is 32 bit
Could you provide some more info: Is the script crashing always on the same line, or you're getting System.OutOfMemoryException on random places in the code?
Thanks
I pasted the whole error code above and it is always the same. I don't see any specific line number that is causing the crash.
From the error that you pasted, I see that the script is crashed on "$vms = Get-View -ViewType VirtualMachine" which seems to be on line 17 in your code:
Get-View : 2/21/2011 4:12:48 PM Get-View Exception of type 'System.Ou
tOfMemoryException' was thrown.
At C:\MyDocs\work\Virtualization\VMWare\VMware_Healthcheck\Notes_Fields\TCOMDB_
Data\005c_snapshot-size-vcell03.ps1:17 char:16
+ $vms = Get-View <<<< -ViewType VirtualMachine
+ CategoryInfo : NotSpecified: (:) [Get-View], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomatio
n.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView
If you're saying that the script hasn't consumed all your memory and the error message is always the same, I suspect that there may be a problem with the Get-View cmdlet. I'll try to reporoduce this error, will let you know if found something.
Try to replace in the script the line:
$vms = Get-View -ViewType VirtualMachine
with:
$vms = Get-View -ViewType VirtualMachine -Property "LayoutEx", "Snapshot", "Config.Hardware.Device", "Name"
This will decrease the size of the retreived information significantly.
If it continues to crash, the reason is that on 32 bit windows the size of the heap is limited to 1.5 GB and if you still want to use this script on your environment try with 64 bit windows.
I think that resolved it. I was able to run the script successfully five times in a row. Thank you very much!!