Backup the virtual machine configuration file (.vmx) using PowerCLI

For those of you using a backup and restore solution based on a software backup and restore client installed in each virtual machine it is a good idea to secure the virtual machine configuration file, the .vmx file. Backing up the data from within the virtual machine is not enough for making a restore process as reliable and quick as possible. This will make sure you can recreate the VM if needed in vSphere.
Yeah, you can and should document each virtual machine configuration including but not limited to:

  • Number of vCPUs (both sockets and cores)
  • Amount of memory
  • Number of SCSI controllers
  • Number of disks per SCSI controller
  • Number of vNICs and their port group belonging

Over the years i have used a PowerCLI based script to backup the virtual machines configuration (.vmx) file for all my customers not using a backup and restore solution securing the virtual machine vSphere configuration file.

Below you’ll find the script i have used and it was implemented last week in a vSphere 5.5 environment so it works great for the latest available vSphere version.
Change the red marked text in the script to your required values.

# Script to backup the virtual machines configuration (vmx) file
# Version 1.0 Magnus Andersson RTS
# Start of script parameters section
# vCenter Server configuration
$vcenter = “vcsa01.vcdx56.local
$vcenteruser = “vcdx56magnus
$vcenterpw = “notsecret
$date=get-date -uformat %Y-%m-%d
# Backup destination
$backupdir = “E:vSpherescriptsVMVM-konfiguration-files_vmx
# Log file
# Get date parameter
$date=get-date -format f
# End of script parameter section
“———————————–a———————————————” >> $logfile
“Backup of the virtual machine configuration files for Customer XYZ started at:” >> $logfile
$date >> $logfile
“The backup was initiated by user:” >> $logfile
[Security.Principal.WindowsIdentity]::GetCurrent().Name >> $logfile
“” >> $logfile
# Connect to vCenter Server
Connect-viserver $vCenter -user $vcenteruser -password $vcenterpasswd -WarningAction 0
Get-VM | `
  Get-View | `
  ForEach-Object {
    $vmxfile = $_.Config.Files.VmPathName
    $dsname = $vmxfile.split(” “)[0].TrimStart(“[“).TrimEnd(“]”)
    $ds = Get-Datastore -Name $dsname
    New-PSDrive -Name ds -PSProvider VimDatastore -Root ‘/’ -Location $ds
    Copy-DatastoreItem -Item “ds:$($vmxfile.split(‘]’)[1].TrimStart(‘ ‘))” -Destination $backupdir
Remove-PSDrive -Name ds
“Backup of the virtual machine configuration files for Customer XYZ finished was at:” >> $logfile
$date >> $logfile
“——————————————————————————–” >> $logfile

You can schedule the PowerCLI script to run on a daily basis using the instruction in this blog.
When register the backed up .vmx file to the vCenter Server and edit the virtual machine you’ll notice that the virtual disk/virtual disks are missing.
Screen Shot 2013-11-29 at 20.14.19

This is something you need to manage in the restore process including the steps:

  • Remove the virtual disk/disks from the restored virtual machine/virtual machines.
  • Add new virtual disk/disks to the virtual machine.

Thanks to whoever helped me creating this script 4-5 years ago. Bet it was, Alan Renouf, Luc Dekens, Niklas Kånge and/or Niklas Åkerlund

4 pings

Comments have been disabled.