VMware Communities
DanlOfRI
Contributor
Contributor

Automate cloning of workstation image

Our company uses VMWare Workstation 10 and I have several machines configured as software build machines.

One of my tasks is to create weekly backups, e.g., clones, of the build machines. I would love to automate this process so that the machines are shutdown, previous snapshot is deleted, new snapshot is created, a full clone is created, and once that is completed the system is restarted.

Below is my manual process:

  • Log into the build machine host
  • Check Build machine to ensure that a build is not currently running. If running, then monitor the build until complete.
  • Stop Build machine.
  • Delete previous 'WeeklyBackup' snapshot
  • Create new 'WeeklyBackup' snapshot
  • Create new full clone of build machine.
  • Start Build machine and log into the operating system
  • Make a copy of cloned machine to network resource for redundancy.

I am fairly good with PowerShell and have tried my hand at creating a script that clones a machine using vmrun.exe. However, I would like to somehow inject a progress indicator to show that something is still happening. Also I would like to automate the entire process so that I can schedule this to run on the weekend.

Below is the code that I have so far, but I have to think that there is a better way to manage this? Is there a PowerShell module for VMWare Workstation that can help with this?

param (

  [Parameter(Mandatory=$true)]

  #This is usually the branch / internal name for the build machine. It is used to specify the clone name and destination path for the weekly backup

  [string]$machineName,

  [Parameter(Mandatory=$true)]

  #This is the name of the VMX file

  [string]$sourceFileName,

  #This is the path to the VMX file

  [string]$sourcePath

)

function main

{

  #date stamp is concatenated to machine name to create a unique destination folder name for the clone

  $dateStamp = Get-Date -Format yyyyMMdd

  #For our build host workstation / server, we have an external drive whose path is specified below.

  $destinationBasePath = "F:\VirtualMachines\BuildMachineBackups\WeeklyBackups"

  #Test the parameters to ensure valid data

  if (!(Test-Path $destinationBasePath -PathType 'Container')) {

  Write-Host "Destination path, $destinationBasePath, does not exist. Please verify if external hard drive is available"

  }

  if (!(Test-Path $sourcePath -PathType 'Container')) {

  Write-Host "Source path, $sourcePath, does not exist. Please specify a valid source path."

  }

  $fqp_source = (Join-Path $sourcePath $sourceFileName)

  $fqp_destinationPath = Join-Path $destinationBasePath (Join-Path $machineName ("$machineName(backup_$dateStamp)\$machineName.vmx"))

  #ensure that target path does not exist ...

  if (Test-Path ($fqp_destinationPath)--PathType 'Container') {

  rd Join-Path $destinationBasePath (Join-Path $machineName ("$machineName(backup_$dateStamp)"))

  }

#The core of the code is here, path to VMRUN.EXE, Command line arguments, and PowerShell 'Start-Process'

  $vmwareRun = "C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe"

  $cmdArgs = "-T ws clone `"$fqp_source`" `"$fqp_destinationPath`" full -cloneName=`"$machineName(backup)`""

  Start-Process $vmwareRun -ArgumentList $cmdArgs -RedirectStandardError (Join-Path $destinationBasePath ("$machineName_$dateStampErrors.log")) -RedirectStandardOutput (Join-Path $destinationBasePath ("$machineName_$dateStamp.log"))

}

main

0 Kudos
0 Replies