VMware Cloud Community
KLankford
Contributor
Contributor

Backup Script Errors

Hi Everyone

I'm in need of a hand, I've tried pulling together a simple backup script from the automation vsphere administration with powerCLI, and keep getting errors:

Function Write-Log ($text) {
$logtime = (Get-date).DateTime
Write-Host "$($Logtime): $Text"
}

Function Backup-VM{
param(
$VM,
$Datastore
)

Process{
$date = get-date -Format "ddMMyyyy"
$time = (get-date -f "HH:MM")

Write-log "$VM Backing Up"
Write-log "$VM Creating Snapshot"

$clonesnap = $VM | New-Snapshot -Name "Snapshot Created on $Date by Backup"

$vmView = $VM | Get-view

$cloneFolder = $vmView.Parent
$CloneVM = "$VM-$Date"

Write-log "$($VM.Name) Cloning from snapshot to $CloneVM"

$CloneSpec = new-object Vmware.Vim.VirtualMachineCloneSpec
$CloneSpec.Snapshot = $vmView.Snapshot.CurrentSnapshot
$CloneSpec.Location = new-object Vmware.VIM.VirtualMachineRelocateSpec
$CloneSpec.Location.Datastore = ($Datastore | get-view).MoRef

$CreateClone = $vmView.CloneVM( $CloneFolder, $CloneVm, $CloneSpec )

Write-Log "$($VM.Name) Removing Snapshot"
Get-Snapshot -VM (Get-VM -Name $VM) -Name $CloneSnap | Remove-Snapshot -confirm:$False
Write-Log "$($VM.Name) Backup Complete"
}
}

Backup-VM -VM (get-vm "VM_Name") -Datastore (Get-Datastore "ESX_Storage")

Now the issue I'm having is that I get the following error message:

[vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> C:\t
emp\Test4.ps1
20 October 2011 13:43:52: VM_Name Backing Up
20 October 2011 13:43:52: VM_Name Creating Snapshot
20 October 2011 13:43:57: VM_Name Cloning from snapshot to VM_Name-20102011
Exception calling "CloneVM" with "3" argument(s): "A specified parameter was not correct .datastore"
At C:\temp\Test4.ps1:33 char:31
+ $CreateClone = $vmView.CloneVM <<<< ( $CloneFolder, $CloneVm, $CloneSpec )
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Any ideas?

I've tried using the VirtualMachineCloneSpec.Location.Datastore without a parameter, but powerCLI doesnt seem to accept it at all-

$CloneSpec = new-object Vmware.Vim.VirtualMachineCloneSpec
$CloneSpec.Snapshot = $vmView.Snapshot.CurrentSnapshot
$CloneSpec.Location = new-object Vmware.VIM.VirtualMachineRelocateSpec
$CloneSpec.Location.Datastore = ($Datastore | get-view).MoRef

Any Ideas would be great.

Karl

0 Kudos
1 Reply
KLankford
Contributor
Contributor

Do you lovely people think I should specify the host parameter of a host that can see this other datastore....?

0 Kudos