How Can We Help?
A VM memory dump
There are occasions when you need to take a memory dump of a problematic VM.
A Snapshot taken with the Memory option is all that is required.
The following script shows you can automate this for 1 or more VMs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
$data = @' vCenter,VM vcsa1,vm1 vcsa2,vm2 '@ $dumpLocation = 'C:\MemeoryDumps' $dumpItem = Get-Item -Path $dumpLocation ConvertFrom-Csv -InputObject $data -UseCulture -PipelineVariable row | ForEach-Object -Process { Connect-VIServer -Server $row.vCenter $vm = Get-VM -Name $row.VM $snap = New-Snapshot -VM $vm -Name "Memory dump" -Memory -Confirm:$false if($snap){ $vm.ExtensionData.UpdateViewData('Layout.Snapshot') $snapObj = $vm.ExtensionData.LayoutEx.Snapshot | where{$_.Key -eq $snap.Id} if($snapObj){ Get-Item -Path "$dumpLocation\$($row.VM)" | Remove-Item -Recurse -Confirm:$false $tgtDir = New-Item -Path $dumpLocation -Name "$($row.VM)" -ItemType Directory $files = @() $files += $vm.ExtensionData.LayoutEx.File | where{$_.Key -eq $snapObj.MemoryKey} | select -ExpandProperty Name $files += $vm.ExtensionData.LayoutEx.File | where{$_.Key -eq $snapObj.DataKey} | select -ExpandProperty Name $files | ForEach-Object -Process { $dsName,$filePath = $_.Split(' ') $ds = Get-Datastore -Name $dsName.Trim('[]') New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" | Out-Null Copy-DatastoreItem -Item "DS:$filePath" -Destination $tgtDir -Confirm:$false Remove-PSDrive -Name DS -Confirm:$false } } else{ Write-Output "Failed to retrieve snapshot info for $($row.VM)" } Remove-Snapshot -Snapshot $snap -Confirm:$false | Out-Null } else{ Write-Output "Snapshot failed for $($row.VM)" } Disconnect-VIServer -Server $row.vCenter -Confirm:$false } |
The script uses a here-string and the ConvertFrom-Csv cmdlet to get the names of the vCenters and the VMs, but you can easily change this to any source for providing the information.