Skip to content Skip to sidebar Skip to footer

Need Python Interface For Moving A Machine To Another Folder

I am trying to find a code support in python for moving a machine between Datacenter's folders without success, I saw in pysphere that you can define the folder just in the clone s

Solution 1:

You can do this with pyVmomi. I would avoid pysphere because pyVmomi is maintained by VMWare and pysphere hasnt been updated in 4 years or more.

That said here is some sample code that uses pyVmomi

service_instance = connect.SmartConnect(host=args.host,
                                        user=args.user,
                                        pwd=args.password,
                                        port=int(args.port))

search_index = service_instance.content.searchIndex
folder = search_index.FindByInventoryPath("LivingRoom/vm/new_folder")
vm_to_move = search_index.FindByInventoryPath("LivingRoom/vm/test-vm")
move_task = folder.MoveInto([vm_to_move])

In this example I create a ServiceInstance by connecting to a vCenter, next I grab an instance of the SearchIndex. The SearchIndex has several methods that can be used to locate your managed objects. In this example I decided to use the FindByInventoryPath method, but you could use any that will work for you. First I find the instance of the Folder named new_folder that I want to move my VirtualMachine into. Next I find the VirtualMachine I want to move. Finally I execute the Task that will move the vm for me. That task takes a param of the list of objects to be moved into the folder, and in this case its a single item list containing only the one vm I want to move. From here you can monitor the task if you want.

Keep in mind that if you use the FindByInventoryPath there are many hidden folders that are not visible from the GUI. I find that using the ManagedObjectBrowser is very helpful at times.

Helpful doc links:

Post a Comment for "Need Python Interface For Moving A Machine To Another Folder"