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.
$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.