In the ever-evolving landscape of virtualization management, efficiency is key. VMware's vCenter Server is the heart of many virtual environments, orchestrating a symphony of VMs with precision. But what if you could take vCenter's capabilities to the next level? Enter Python – the versatile scripting language that speaks directly to vCenter, unlocking a new realm of automation and control.
Python is more than just a programming language; it's a gateway to automation. With the Python SDK for VMware, known as PyVmomi, you can script complex operations that manage your virtual machines, all within the comfort of Python’s syntax. Whether you’re looking to power on a fleet of VMs, take snapshots before a big update, or gracefully shut down systems for maintenance, Python and PyVmomi make it possible with minimal effort.
Imagine a script that not only lists all VMs registered in your vCenter but also offers you the control to power them on, suspend, resume, or even take a snapshot. This is not a mere imagination anymore; it's a reality that I've crafted into a Python script. This script is your remote control to vCenter, putting the power of virtual machine management at your fingertips.
The script starts by connecting to your vCenter server using your credentials. Once authenticated, it fetches a list of all VMs and displays them neatly. Here's where the interactivity comes in – you choose a VM and decide what to do with it. Want to power it on or shut it down? Just a command away. Need to suspend or resume it? A simple input does the job. Looking to create a snapshot? It’s just as easy.
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim
import ssl
import atexit
# vCenter Server details
vc_server = 'vc.ntpro.local'
username = 'administrator@ntpro.local'
password = 'VMware1!'
# Disable SSL certificate verification (for demo purposes only, not recommended for production)
context = ssl._create_unverified_context()
# Function to connect to vCenter
def connect_to_vcenter(server, user, password):
si = SmartConnect(host=server, user=user, pwd=password, sslContext=context)
atexit.register(Disconnect, si)
return si
# Function to get all VMs
def get_all_vms(si):
content = si.RetrieveContent()
container = content.viewManager.CreateContainerView(content.rootFolder, [vim.VirtualMachine], True)
return container.view
# Power operations for the VM
def power_on_vm(vm):
if vm.runtime.powerState != vim.VirtualMachinePowerState.poweredOn:
task = vm.PowerOnVM_Task()
wait_for_task(task)
print(f"VM '{vm.name}' is powered on.")
else:
print(f"VM '{vm.name}' is already powered on.")
def power_off_vm(vm):
if vm.runtime.powerState != vim.VirtualMachinePowerState.poweredOff:
task = vm.PowerOffVM_Task()
wait_for_task(task)
print(f"VM '{vm.name}' is powered off.")
else:
print(f"VM '{vm.name}' is already powered off.")
def suspend_vm(vm):
if vm.runtime.powerState == vim.VirtualMachinePowerState.poweredOn:
task = vm.SuspendVM_Task()
wait_for_task(task)
print(f"VM '{vm.name}' is suspended.")
else:
print(f"VM '{vm.name}' cannot be suspended because it is not powered on.")
def resume_vm(vm):
if vm.runtime.powerState == vim.VirtualMachinePowerState.suspended:
task = vm.PowerOnVM_Task()
wait_for_task(task)
print(f"VM '{vm.name}' is resumed.")
else:
print(f"VM '{vm.name}' is not suspended.")
def create_snapshot(vm):
task = vm.CreateSnapshot_Task(name='Snapshot', description='Created by script', memory=False, quiesce=False)
wait_for_task(task)
print(f"Snapshot for VM '{vm.name}' created.")
# Wait for vCenter task to complete
def wait_for_task(task):
task_done = False
while not task_done:
if task.info.state == vim.TaskInfo.State.success:
return
if task.info.state == vim.TaskInfo.State.error:
print(f"Task failed: {task.info.error}")
raise Exception("Task failed")
# Main script logic
if __name__ == "__main__":
si = connect_to_vcenter(vc_server, username, password)
vms = get_all_vms(si)
vms_dict = {vm.name: vm for vm in vms}
print("List of VMs:")
for vm_name in vms_dict.keys():
print(vm_name)
selected_vm_name = input("Enter the name of the VM you wish to manage: ")
vm = vms_dict.get(selected_vm_name)
if vm:
print(f"Selected VM: {selected_vm_name}")
action = input("Choose an action: (on) Power On, (off) Power Off, (suspend) Suspend, (resume) Resume, (snapshot) Create Snapshot: ").lower().strip()
if action == 'on':
power_on_vm(vm)
elif action == 'off':
power_off_vm(vm)
elif action == 'suspend':
suspend_vm(vm)
elif action == 'resume':
resume_vm(vm)
elif action == 'snapshot':
create_snapshot(vm)
else:
print("Invalid action selected.")
else:
print(f"VM '{selected_vm_name}' not found.")
The script is designed with safety and ease of use in mind, leveraging Python’s clear syntax and PyVmomi’s powerful bindings to the vSphere API. It’s a tool that can be expanded, customized, and integrated into larger workflows or dashboards. I'm hosting the source code, along with some screenshots on my GitHub page.