Export VM to a Local Folder

Export VM KB ID 0001912

Problem

We had a situation recently where a client who was leaving us needed to take their data, the VM in question was very large (12.5Tb in size) Exporting to an OVA file was problematical, (due to the size of the file). So Getting the flat files for the VM seemed like the best option.

Solution : Export VM

Firstly when using PowerShell there’s a tendency for it to use the %TEMP% and %TMP% directories to store files, in most cases that’s fine, but when we are talking about 12.5 Tb of files you probably WON’T have that free (specifically you want have that free on the C: drive in the Windows\Temp directory. So the first step it to change the location of those to a drive that has enough capacity (in the example below D:\Exported.

# Set new TEMP and TMP path
$newTempPath = "D:\Exported"
 
# Change system environment variables
[System.Environment]::SetEnvironmentVariable("TEMP", $newTempPath, [System.EnvironmentVariableTarget]::Machine)
[System.Environment]::SetEnvironmentVariable("TMP", $newTempPath, [System.EnvironmentVariableTarget]::Machine)
 
# Verify the changes
Write-Host "System TEMP and TMP variables updated to: $newTempPath"
 
# Prompt the user to restart for changes to take effect
Write-Host "A restart is required for changes to apply system-wide."

MAKE SURE YOU REBOOT AFTER THIS IS DONE, TO TAKE EFFECT

Export VM Change Temp locations

Once rebooted, you can check the system variables have changed with the following commands.

Write-Host "System TEMP: $([System.Environment]::GetEnvironmentVariable('TEMP', 'Machine'))"
Write-Host "System TMP:  $([System.Environment]::GetEnvironmentVariable('TMP', 'Machine'))"

Export VM Check Temp locations

Now you need to know the datastore and folder, (within that datastore) the VM you want to extract is in (for example see below).

Export VM datastore and folders

Then use the following script to extract the VM.

#Change the following Variables to fit your requirements

$VIServer = "vc-80.pnl.com"
$outputFolder = "D:\Exported"
$dsName = 'ISCSI-VOL-1'
$folder = '2016-Server'

#Do not change the following script

Connect-VIServer -Server $VIServer
$ds = Get-Datastore -Name $dsName
New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" | Out-Null
Get-ChildItem -Path "DS:/$folder" -Recurse | %{
   Copy-DatastoreItem -Item "DS:/$($_.DatastoreFullPath.Split(' ')[1])" -Destination $outputFolder
}
Remove-PSDrive -Name DS -Confirm:$false


Exporting a VM With PowerShell

Don’t forget, if you changed your %temp% and %tmp% system variables (earlier).  To change them back to C:\Windows\Temp to avoid any future problems.

Related Articles, References, Credits, or External Links

Special thanks to Darren Edmunds for his perseverance and presenting me with an interesting problem.

Author: PeteLong

Share This Post On

Submit a Comment

Your email address will not be published. Required fields are marked *