View Only
  • 1.  Cloning to a datastore subfolder

    Posted Jun 13, 2011 06:10 PM

    I have a powershell script that creates snapshots daily and clones on Sunday.  I would like to have the clones saved to a subfolder on a NFS Share.  I can't figure out how to set the datastore to a subfolder like "NFS_NAS_SHARED\CLONES".  The following code fails stating "'NFS_NAS_SHARED\CLONES' was not found, using the specified filter(s)."  Is it possible to have the clone destination as a subfolder of a datastore?

    Start-Transcript -Path "C:\VMWare_Scripts\LOGS\SnapshotsTranscript.log"

    #Get the PowerCLI from VMware
    add-pssnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue

    # Import Backup Information
    #File layout: MasterVM(vmname), BackupDS(DS to store clone), BackupFolder

    #$backupinfo =  Import-Csv C:\VMWare_Scripts\Snapshot_backups.csv
    $backupinfo =  Import-Csv C:\VMWare_Scripts\Snapshot_backups_TESTING.csv

    #Set Date format for naming
    $datetime = Get-Date -Format "yyyyMMdd-HH:mm"
    $date = Get-Date -Format "yyyyMMdd"

    #Connect to vCenter
    Connect-VIServer "localhost"

    #Get the day of the week
    switch ((get-date).dayofweek.toString())
    Sunday    { $dow="SUN"; break }
    Monday    { $dow="MON"; break }
    Tuesday   { $dow="TUE"; break }
    Wednesday { $dow="WED"; break }
    Thursday  { $dow="THU"; break }
    Friday    { $dow="FRI"; break }

    foreach ($customer in $backupinfo){

    $vm = Get-VM $customer.MasterVM
    $days = $customer.DaysToKeep

    Write-Host $vm.Name
    #Write-Host $days
       #Delete snaps older than $days
    Get-VM $vm | Get-Snapshot | Where {$_.Created -lt ((Get-Date).AddDays(-$days))} | Remove-Snapshot -Confirm:$false
      if($dow -eq "MON"){
      #Create a clone
      #Create a new snapshot for cloning
      $cloneSnap = $vm | New-Snapshot -Name "clone_snap" -Quiesce:$true
      # Get managed object view
      $vmView = $vm | Get-View

      # Get folder managed object reference
      $cloneFolder = $vmView.parent

      # Build clone specification
      $cloneSpec = new-object Vmware.Vim.VirtualMachineCloneSpec
      $cloneSpec.Snapshot = $vmView.Snapshot.CurrentSnapshot
      $cloneSpec.Location = new-object Vmware.Vim.VirtualMachineRelocateSpec
      #set the clone destination
      $backupDestination = $customer.BackupDS + $customer.BackupFolder
      $cloneSpec.Location.Datastore = (Get-Datastore -Name $backupDestination | Get-View).MoRef
      $cloneSpec.Location.Transform =  [Vmware.Vim.VirtualMachineRelocateTransformation]::sparse

      $cloneName = "$vm-$datetime"

      # Create clone
      $vmView.CloneVM( $cloneFolder, $cloneName, $cloneSpec )

      # Write newly created VM to stdout as confirmation
      Get-VM $cloneName

      # Remove Snapshot created for clone
      Get-Snapshot -VM (Get-VM $customer.MasterVM) -Name $cloneSnap | Remove-Snapshot -confirm:$False

      #Take a Snapshot
      New-Snapshot -Name $datetime -description "Scripted Snap: Snapshots.ps1" -vm $vm -Quiesce:$true


    #Disconnect from vCenter
    Disconnect-VIServer -Confirm:$false


  • 2.  RE: Cloning to a datastore subfolder
    Best Answer

    Posted Jun 13, 2011 06:35 PM

    I'm afraid that the CloneVM_Task method doesn't allow you to specify subfolders on a datastore.

    It wants a datastore and will use the displayname of the VM to create a folder on that datastore.

    If you clone a VM called XYZ to datastore DS1, the clone will be located in the "[DS1] XYZ" folder.

    If you want to move the clone to another folder, you can use the Copy-DatastoreItem cmdlet afterwards.