Search code examples
powershellvmwareesxipowercli

VM Clone to the Subdirectory of a Datastore


I have a Datastore in vCenter that I'm trying to clone to using a PowerCLI script I run weekly. I can clone virtual machines to it, but I have absolutely no control over the directory structure. It's throwing every virtual machine in a folder named after the virtual machine, on the root of the datastore.

My Ideal directory structure is with the Root only being utilized for active virtual machines. a "BACKUPS" folder exists. Inside that folders are created weekly to store the weekly clones. Example "20111004" for today. Inside that folders exist for each virtual machine.

$vm = <name of the virtual machine obtained programmatically>
$date = Get-Date -Format "YYYYMMDD"
$h = <ESXi host where the VM is located, obtained programatically>
$dsName = "DATASTORE1" #<Name of the datastore I'm targeting>
$dirPath = "BACKUPS/" + $date + "/" + $vm

New-VM -Name $vm-$date -VM $vm -VMHost -$h -Datstore $dsName -Location $dirPath

Example of a "full" path
[DATASTORE1] BACKUPS/20111004/VIRTUALMACHINE

This is the only piece of the script that doesn't work. When the command is given this way in PowerShell it completely ignores the -Location parameter and throws the clone on the root of the datastore. I haven't found any documentation (that I understand) about how its supposed to be formatted yet. Something to do with a VIContainer ??

Script Source I'm customizing:
http://www.mbnbusiness.co.uk/blog/2010/08/updated-powercli-vmware-vm-backup-script-v2-0/


Solution

  • The -Location defines the vCenter folder into which a VM is placed, not a folder within a datastore within which to place the VMs files.

    As far as I am aware, a VMs files must be placed on the root of the datastore.

    I considered trying to do a similar thing when I first created the script as it made the archiving of old backups easier, but I could not find a solution at the time.

    Sorry I can’t help more…