Learning Objectives
By the end of this article, you will:
Automate common VM lifecycle actions such as power on, power off, and restart.
Create and manage VM snapshots through scripts.
Build reusable Python-driven PowerCLI automation for these tasks.
Visualize the process with a step-by-step diagram.
My Personal Repository on GitHub
VMware Repository on GitHub
Prerequisites
Completed Articles 1–3 (PowerCLI and Python are installed, and you have scripts working).
vCenter or ESXi access with permission to modify VMs and create snapshots.
Windows system with PowerCLI.
1. Introduction to VM Lifecycle Automation
Managing VMs at scale is much easier when you automate frequent actions like starting, stopping, restarting, and snapshotting. PowerCLI makes these tasks scriptable, while Python lets you orchestrate and schedule them as part of broader automation workflows.
2. PowerShell Script: Power On, Power Off, Restart, and Snapshot VMs
Below is a PowerShell script that demonstrates how to:
Connect to vCenter
Power on, power off, and restart a VM
Take a snapshot of a VM
Log results to a file
Save as vm_lifecycle.ps1:
# Import PowerCLIImport-Module VMware.PowerCLI# Connect to vCenter (update details)Connect-VIServer -Server <vcenter-address> -User <username> -Password <password># Set your VM name$vmName = “<your-vm-name>”# Power on VMStart-VM -VM $vmName -Confirm:$false | Out-File -FilePath C:Templifecycle_log.txt -Append# Take snapshotNew-Snapshot -VM $vmName -Name “AutoSnapshot_$(Get-Date -Format ‘yyyyMMdd_HHmmss’)” -Description “Automated snapshot via script” | Out-File -FilePath C:Templifecycle_log.txt -Append# Restart VMRestart-VMGuest -VM $vmName -Confirm:$false | Out-File -FilePath C:Templifecycle_log.txt -Append# Power off VMStop-VM -VM $vmName -Confirm:$false | Out-File -FilePath C:Templifecycle_log.txt -Append# DisconnectDisconnect-VIServer -Server * -Confirm:$false
Replace <vcenter-address>, <username>, <password>, and <your-vm-name> as needed.
3. Calling the Lifecycle Script from Python
Here is the Python code to execute the above PowerShell script and print the output:
import subprocessps_script = r”C:Tempvm_lifecycle.ps1″completed_process = subprocess.run([ “powershell.exe”, “-ExecutionPolicy”, “Bypass”, “-File”, ps_script], capture_output=True, text=True)print(“Script Output:”, completed_process.stdout)if completed_process.stderr: print(“Errors:”, completed_process.stderr)
You can schedule or trigger this script from other Python programs as needed.
4. Diagram: VM Lifecycle Automation Flow
5. Best Practices and Safety Tips
Snapshot Management:Do not keep snapshots for long periods. Regularly remove old snapshots to avoid storage and performance issues.
Permissions:Scripts that modify VM states should be run only by trusted admins.
Testing:Always test scripts in a lab before using in production.
Logging:Use log files to keep a history of automated actions for auditing and troubleshooting.
6. Further Reading
VMware PowerCLI Documentation
PowerCLI Cmdlet Reference
Python subprocess docs
Best Practices for VMware Snapshots
7. Conclusion and Next Steps
You have learned how to automate VM lifecycle management and snapshotting using PowerCLI and Python.These scripts enable faster recovery, testing, and bulk management for your vSphere environment.
Next up: In Article 5, you will learn how to export and visualize your VM inventory and script outputs using Python for reporting and analytics.
Next PostVM Conversion in Windows Admin Center (Public Preview): A Field Guide for VMware to Hyper-VTL;DR: Microsoft’s new VM Conversion extension in Windows Admin Center (WAC) v2 provides a no-cost, agentless way to convert VMware VMs (vCenter 6.x and 7.x) to Hyper-V with minimal downtime…
The post Automating VM Lifecycle Actions and Snapshots with PowerCLI and Python appeared first on Digital Thought Disruption.

