VMware Cloud Community
Ilithya
Contributor
Contributor

CloneVM Folder & remove-vm problem

Hello,

I'm working on an Backup script that should be sceduled once a week and clone several VMs and unregister the clone so we can move the Files to a Backup Server.

The first test runs seem to work but with some errors and confusion for me.

1. The CloneVM function requires 3 Arguments: TargetFolder, TargetName & CloneSpecs.

When I set the Location through the CloneSpecs ( Location.Datastore ) the TargetFolder seems to be ignored.

But when I try to change the Folder to a MoRef of Datastore it throws an error, but it works with MoRef of the VM folder.

I'd really like to Clone the VMs to a NFS Datastore in a Subfolder called Backup.

2. remove-vm throws me an "no permission" Exception due to missing System.Read permission.

When I look in the "recent tasks" window in vSphere it shows Unregistering VM but never changes status from "progressing" to "completed".

When I look in the Tasks & Events tab of the Datacenter it shows the task as completed and the Cloned VM isn't showing in the Inventory anymore so seems successfully unregistered.

Is the remove-vm cmdlet problematic or is the exception thrown while trying to check the status of the "to-be-unregistered-VM"?

I'd be thankfull for answers since I really don't understand these problems.

Ilithya

If needed I could post the code I've written yet.

0 Kudos
20 Replies
DSTAVERT
Immortal
Immortal

The free version of ESXi is very restricted for script access. You will have read access but no writes. There is a script that can work but you will need to enable SSH access. Have a look at William Lam's http://communities.vmware.com/docs/DOC-8760

-- David -- VMware Communities Moderator
Ilithya
Contributor
Contributor

Sorry for misunderstanding,

We are running several ESX 4.? Server and a vCenter onto which I loggon, I allready have read that the CloneVM function only works when connected to VC.

0 Kudos
LucD
Leadership
Leadership

The folder parameter on the CloneVM_Task method should point to a "blue" folder. In other words a folder under the VMs and Templates tab.

Do you use the -DeleteFromDisk parameter on the Remove-Vm cmdlet ?

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
DSTAVERT
Immortal
Immortal

Sorry I didn't read your post closer.

-- David -- VMware Communities Moderator
0 Kudos
Ilithya
Contributor
Contributor

1. So would it be fine to create a Backup folder in the VM folder? how would I get the Folder MoRef, the get-folder only lists the usual Folders (Datacenter,Datastore,VM,..)

2. No I don't use this param since I don't want to delete the files I just copied

Thank you for fast replies and ideas!

0 Kudos
LucD
Leadership
Leadership

The MoRef is a property of a SDK object and Get-Folder returns PowerCLI objects.

So you would have to use Get-View to switch between the 2 objects.

Suppose you have the "blue" folder called Backup, then you could do

$fldMoRef = (Get-Folder -Name "Backup" | Get-View).MoRef

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Ilithya
Contributor
Contributor

That's what I've tried with the Datastore Folder but it threw an exception, I think I need to read a little more about these Folders.

0 Kudos
LucD
Leadership
Leadership

The Datastores folder is a hidden pre-configured folder that is only used to store the datastore entries.

You can't create a VM in that folder.

Neither can you in the Networking folder.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Ilithya
Contributor
Contributor

Thank you, it seems that I've understood the different Location / Folder organization,

the permission error is still a bit unsadisfying but I could live with it or try to catch it. If you still got an idea, here is the Error:

Remove-VM : 16.07.2010 09:31:50 Remove-VM Unsufficient permissions. You need 'Syste

m.Read' privileges to complete the requested operation.

At C:\temp\tmp.ps1:23 char:10

+ remove-vm <<<< -VM $clonedVM -confirm:$FALSE

+ CategoryInfo : InvalidArgument: (Task-task-1712:ManagedObjectReference) [Remo

ve-VM], NoPermission

+ FullyQualifiedErrorId : Client20_QueryServiceImpl_RetrieveProperties_NotPermission,VMw

are.VimAutomation.ViCore.Cmdlets.Commands.RemoveVM

0 Kudos
LucD
Leadership
Leadership

It looks as if the account you used to connect doesn't have the required privileges.

Can you try with another account ?

Can you do the removal of the guest from the vSphere client with that account ?

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Ilithya
Contributor
Contributor

Yes I can remove it from vSphere without any Problems.

( It's getting removed by the script too, but not without error - even through the machine isn't in the inventory afterwards)

0 Kudos
Ilithya
Contributor
Contributor

I just checked Privileges and my Role is "Admin" which includes the System.Read permission...

0 Kudos
LucD
Leadership
Leadership

Could you perhaps attach the script you are using ?

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Ilithya
Contributor
Contributor

function BackupVM

{

$vm = get-vm $args

$vmview = get-view $vm

$newName = "Backup2."+$vm.Name

$destStore = get-datastore $BackupDataStoreName

$destStoreView = get-view $destStore

$vmCloneFolder = (get-view (get-folder -Name Backup)).MoRef

$vmCloneSpec = new-object Vmware.Vim.VirtualMachineCloneSpec

$vmCloneSpec.Location = new-object Vmware.Vim.VirtualMachineRelocateSpec

$vmCloneSpec.Location.Datastore = $destStoreView.MoRef

$vmCloneSpec.Location.DiskMoveType = http://Vmware.Vim.VirtualMachineRelocateDiskMoveOptions::moveAllDiskBackingsAndDisAllowSharing

$vmCloneSpec.Location.Transform = http://Vmware.Vim.VirtualMachineRelocateTransformation::sparse

$vmview.CloneVM($vmCloneFolder, $newName, $vmCloneSpec)

$clonedVM = get-vm -Name $newName

remove-vm -VM $clonedVM -confirm:$FALSE

}

function BackupVMS

{

$starttime = get-date -f "HH:MM:S"

connect-viserver 172.16.0.3

foreach ($arg in $args)

{

Get-VM -Name $arg | foreach {

write-host "$_ is to be cloned next."

BackupVM $_

}

}

disconnect-viserver -confirm:$false

}

It's not complete yet but works for now

0 Kudos
LucD
Leadership
Leadership

Made a few minor changes and it works without a problem for me.

What role does this account you use have ?

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Ilithya
Contributor
Contributor

Datacenter-datace... Lesen und Stores sehen ILITHYA False True

Datastore-datasto... Admin ILITHYA False True

Datastore-datasto... Admin ILITHYA False True

Datastore-datasto... Admin ILITHYA False True

HostSystem-host-1858 Admin ILITHYA False True

It looks pretty much like this, I ran the script with ours SUPERADMIN's ( my Chef ) account and it's not giving me any errors.

Thanks for all the help !

0 Kudos
LucD
Leadership
Leadership

That first role "Lesen und Stores sehen" is obviously a custom role.

Can you check if that role contains the system.read privilege

Get-VIRole -Name "Lesen und Stores sehen"  | Get-VIPrivilege

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Ilithya
Contributor
Contributor

I'm not at work anymore so I can't say it for sure, but I'm very sure that it contains System.Read since we have been trying to adjust my permissions today in effort to get it "work".

Since I'm only an apprentice which would like to become a trainee I'm pretty sure I can consider myself lucky with the permissions I allready got and since the script is should be run by an yet non existing service account, I'd rather know which permissions we need for this account Smiley Happy

Mfg

Ilithya

0 Kudos
IanGibbs
Enthusiast
Enthusiast

I have this permissions issue on removing a VM as well. I have role Administrator (which does contain System.Read) on the Datacentre that contains the VM I am removing. When I elevate my rights to Administrator on the whole VC (the parent of the Datacentre), the problem goes away. Seems like a bug to me.

0 Kudos