def destroy_vm_by_name(service_instance, virtual_machine, name, depth=1): if hasattr(virtual_machine, 'childEntity'): vmList = virtual_machine.childEntity for c in vmList: destroy_vm_by_name(service_instance, c, name, depth + 1) return summary = virtual_machine.summary if summary.config.name == name: sys.stdout.write("name=" + str(summary.config.name)) sys.stdout.write("|uuid=" + str(summary.config.uuid)) print("Found: {0}".format(virtual_machine.name)) print("The current powerState is: {0}".format( virtual_machine.runtime.powerState)) if format(virtual_machine.runtime.powerState) == "poweredOn": print("Attempting to power off {0}".format(virtual_machine.name)) task = virtual_machine.PowerOffVM_Task() libvmtask.WaitForTasks([task], service_instance) print("{0}".format(task.info.state)) print("Destroying VM from vSphere: {0}".format(virtual_machine.name)) task = virtual_machine.Destroy_Task() libvmtask.WaitForTasks([task], service_instance) print("Done by name.")
def main(): parser = argparse.ArgumentParser(description='vCenter login') parser.add_argument('-s', '--host', required=True, action='store', help='vSphere IP') parser.add_argument('-o', '--port', type=int, default=443, action='store', help='vSphere Port') parser.add_argument('-u', '--user', required=True, action='store', help='User name') parser.add_argument('-p', '--password', required=True, action='store', help='Password') parser.add_argument('-n', '--name', required=False, action='store', help='VM name') args = parser.parse_args() try: service_instance = connect.SmartConnect(host=args.host, user=args.user, pwd=args.password, port=int(args.port)) atexit.register(connect.Disconnect, service_instance) if args.name: content = service_instance.content objView = content.viewManager.CreateContainerView(content.rootFolder, [vim.VirtualMachine], True) vmlist = objView.view objView.Destroy() for vm in vmlist: if vm.name == args.name: print "Powering on VM " + vm.name task = vm.PowerOn() libvmtask.WaitForTasks([task], service_instance) print "Powered on VM " + vm.name #libvmtask.list_files(service_instance, '*.vmdk') #libvmtask.list_files(service_instance, '*.iso') #libvmtask.detach_disk(vm, service_instance, '[NAS] hulli/hulli.vmdk') #libvmtask.attach_disk(vm, service_instance, '[NAS] hulli/hulli.vmdk') else: raise SystemExit("Please provide a VM name") return 0 except vmodl.MethodFault as error: print("ERROR: " + error.msg) sys.exit(1)
def main(): parser = argparse.ArgumentParser(description='vCenter login') parser.add_argument('-s', '--host', required=True, action='store', help='vSphere IP') parser.add_argument('-o', '--port', type=int, default=443, action='store', help='vSphere Port') parser.add_argument('-u', '--user', required=True, action='store', help='User name') parser.add_argument('-p', '--password', required=True, action='store', help='Password') parser.add_argument('-n', '--name', required=True, action='store', help='VM name') parser.add_argument('-q', '--resourcepool', required=True, action='store', help='ResourcePool name') parser.add_argument('-d', '--datastore', required=False, action='store', help='Datastore name') args = parser.parse_args() try: service_instance = connect.SmartConnect(host=args.host, user=args.user, pwd=args.password, port=int(args.port)) atexit.register(connect.Disconnect, service_instance) content = service_instance.content objView = content.viewManager.CreateContainerView( content.rootFolder, [vim.VirtualMachine], True) vmlist = objView.view objView.Destroy() for vm in vmlist: if vm.name == args.name: #print vm.name spec = vim.VirtualMachineRelocateSpec() for respool in libvmtask.get_vim_objects( content, vim.ResourcePool): if respool.name == args.resourcepool: resourcepool = respool if not resourcepool: sys.exit(0) spec.pool = resourcepool if args.datastore: for ds in libvmtask.get_vim_objects( content, vim.Datastore): if ds.name == args.datastore: spec.datastore = ds #print spec task = vm.RelocateVM_Task(spec) libvmtask.WaitForTasks([task], service_instance) return 0 except vmodl.MethodFault as error: print("ERROR: " + error.msg) sys.exit(1)
def main(): parser = argparse.ArgumentParser(description='vCenter login') parser.add_argument('-s', '--host', required=True, action='store', help='vSphere IP') parser.add_argument('-o', '--port', type=int, default=443, action='store', help='vSphere Port') parser.add_argument('-u', '--user', required=True, action='store', help='User name') parser.add_argument('-p', '--password', required=True, action='store', help='Password') parser.add_argument('-n', '--name', required=True, action='store', help='VM Name') parser.add_argument('-d', '--datastore', required=False, action='store', help='Datastore Name') parser.add_argument( '-q', '--resourcepool', required=False, action='store', help= 'Resource-Pool name, if empty the first available resource pool will be used.' ) parser.add_argument( '-c', '--datacenter', required=False, action='store', help= 'Datacenter name, if empty the first available resource pool will be used.' ) parser.add_argument('-t', '--template', required=True, action='store', help='VM Name to clone or template name.') args = parser.parse_args() try: service_instance = connect.SmartConnect(host=args.host, user=args.user, pwd=args.password, port=int(args.port)) atexit.register(connect.Disconnect, service_instance) content = service_instance.RetrieveContent() if args.datacenter: print "Using datacenter " + libvmtask.validate_input( args.datacenter) datacenter = libvmtask.get_vim_obj_by_name( content, [vim.Datacenter], libvmtask.validate_input(args.datacenter)) else: datacenter = content.rootFolder.childEntity[0] destfolder = datacenter.vmFolder vmfolder = datacenter.vmFolder hosts = datacenter.hostFolder.childEntity relospec = vim.vm.RelocateSpec() if args.datastore: datastore = libvmtask.get_vim_obj_by_name( content, [vim.Datastore], libvmtask.validate_input(args.datastore)) if datastore: print "Using datastore " + libvmtask.validate_input( args.datastore) relospec.datastore = datastore else: print "Cloud not find datastore, using datastore of origin VM" if args.resourcepool: print("using resource pool " + libvmtask.validate_input(args.resourcepool)) resource_pool = libvmtask.get_vim_obj_by_name( content, [vim.ResourcePool], libvmtask.validate_input(args.resourcepool)) else: print("using first available resource pool") resource_pool = hosts[0].resourcePool #resource_pool = cluster.resourcePool if resource_pool: relospec.pool = resource_pool else: print "Cloud not find resource pool, using resource pool of origin VM" clonespec = vim.vm.CloneSpec() clonespec.location = relospec clonespec.powerOn = False template = libvmtask.get_vim_obj_by_name( content, [vim.VirtualMachine], libvmtask.validate_input(args.template)) if not template: print "VM or template not found" sys.exit(1) print "Cloning VM" task = template.Clone(folder=destfolder, name=libvmtask.validate_input(args.name), spec=clonespec) libvmtask.WaitForTasks([task], service_instance) clonevm = libvmtask.get_vim_obj_by_name( content, [vim.VirtualMachine], libvmtask.validate_input(args.name)) libvmtask.remove_all_nic(clonevm, service_instance) libvmtask.disable_vnc(clonevm, service_instance) return 0 except vmodl.MethodFault as error: print("ERROR: " + error.msg) sys.exit(1)
def main(): parser = argparse.ArgumentParser(description='vCenter login') parser.add_argument('-s', '--host', required=True, action='store', help='vSphere IP') parser.add_argument('-o', '--port', type=int, default=443, action='store', help='vSphere Port') parser.add_argument('-u', '--user', required=True, action='store', help='User name') parser.add_argument('-p', '--password', required=True, action='store', help='Password') parser.add_argument('-n', '--name', required=False, action='store', help='VM name') parser.add_argument('-i', '--uuid', required=False, action='store', help='VM uuid') args = parser.parse_args() try: service_instance = connect.SmartConnect(host=args.host, user=args.user, pwd=args.password, port=int(args.port)) atexit.register(connect.Disconnect, service_instance) if args.uuid: virtual_machine = service_instance.content.searchIndex.FindByUuid( None, args.uuid, True, False) if virtual_machine is None: raise SystemExit("Unable to find VM.") print("Found: {0}".format(virtual_machine.name)) print("The current powerState is: {0}".format( virtual_machine.runtime.powerState)) if format(virtual_machine.runtime.powerState) == "poweredOn": print("Attempting to power off {0}".format( virtual_machine.name)) task = virtual_machine.PowerOffVM_Task() libvmtask.WaitForTasks([task], service_instance) print("{0}".format(task.info.state)) print("Destroying VM from vSphere: {0}".format( virtual_machine.name)) task = virtual_machine.Destroy_Task() libvmtask.WaitForTasks([task], service_instance) print("Done by uuid.") elif args.name: content = service_instance.RetrieveContent() children = content.rootFolder.childEntity for child in children: if hasattr(child, 'vmFolder'): datacenter = child else: continue vm_folder = datacenter.vmFolder vm_list = vm_folder.childEntity for virtual_machine in vm_list: destroy_vm_by_name(service_instance, virtual_machine, args.name, 10) else: raise SystemExit("Please provide a VM name or uuid") return 0 except vmodl.MethodFault as error: print("ERROR: " + error.msg) sys.exit(1)