This repository contains a collection of scripts for managing VMware vSphere environments, including tasks such as:
- Listing and managing VM snapshots.
- Managing VM tags.
- Fixing Platform Services Controller (PSC) replication issues.
- Backing up ESXi configurations.
These scripts use the pyvmomi library to interact with the vSphere API. They require Python 3 and the dependencies listed in requirements.txt. They are designed to connect to multiple vCenter servers, using credentials stored in a .env file.
vcenter_snapshot_cleanup.py: Connects to one or more vCenter servers, retrieves all VMs, displays their snapshots, and deletes snapshots older than a specified age (default: 30 days).- Usage:
- Set environment variables
VCENTER1_HOST,VCENTER1_USER,VCENTER1_PASSWORD,VCENTER2_HOST, etc., with the credentials for each vCenter server. - Run the script:
python vcenter_snapshot_cleanup.py - Optionally, specify the age of snapshots to delete using the
-aor--ageargument:python vcenter_snapshot_cleanup.py -a 7(deletes snapshots older than 7 days). - The script will prompt for confirmation before deleting snapshots.
- Logs are written to
snapshot_cleanup.log.
- Set environment variables
- Usage:
vcenter_snapshot_lister.py: Connects to one or more vCenter servers, retrieves all VMs, and lists their snapshots in a tabular format.- Usage:
- Set environment variables
VCENTER1_HOST,VCENTER1_USER,VCENTER1_PASSWORD,VCENTER2_HOST, etc., with the credentials for each vCenter server. - Run the script:
python vcenter_snapshot_lister.py - The script will output a table with vCenter, VM name, snapshot name, and creation date.
- Set environment variables
- Usage:
These scripts use PowerCLI to interact with the vSphere environment. They require a PowerCLI environment to be set up.
getVMtags.ps1: Retrieves all VMs and prints the names of VMs that don't have any tags assigned.- Usage: Run in a PowerCLI environment:
.\getVMtags.ps1
- Usage: Run in a PowerCLI environment:
setVMtags.ps1: Assigns the tag "Bronze" to any VM that doesn't already have a tag, excluding VMs with names starting with "vCLS-".- Usage: Run in a PowerCLI environment:
.\setVMtags.ps1
- Usage: Run in a PowerCLI environment:
fixpsc.sh: This script is used to fix replication issues with Platform Services Controllers (PSCs) in a vSphere environment. It involves changing the domain state, decommissioning the broken PSC, rejoining the domain, and testing replication.- Usage:
- This script must be run from the affected PSC.
- Prerequisites:
- Offline snapshots of VCs/PSCs.
- SSO Admin Password.
- FQDN of a healthy PSC.
- Root password of the healthy PSC.
- Bash shell enabled on the healthy PSC.
- Run the script:
./fixpsc.sh - The script will prompt for the healthy PSC FQDN and passwords.
- A log file
fix_psc_state.logwill be created.
- Usage:
This section provides detailed instructions on how to fix broken PSC replication, including steps, commands, and visual aids.
References:
- Resolving stale PSC entries from your vSphere environment
- vCenter error 400 Failed to connect to VMware Lookup Service
Steps:
-
Identify the Issue: The following images illustrate the problem, showing a transition from a broken state to a fixed state:
Before (Broken):
After (Fixed):
-
Create Replication Agreements: Use the following command to create a new replication agreement between PSCs (replace
vc01,vc02,vc03,vc04, andAdministratorwith your actual values):cd /usr/lib/vmware-vmdir/bin ./vdcrepadmin -f createagreement -2 -h vc01 -H vc02 -u Administrator ./vdcrepadmin -f createagreement -2 -h vc01 -H vc03 -u Administrator ./vdcrepadmin -f createagreement -2 -h vc01 -H vc04 -u Administrator ./vdcrepadmin -f createagreement -2 -h vc02 -H vc03 -u Administrator ./vdcrepadmin -f createagreement -2 -h vc02 -H vc04 -u Administrator ./vdcrepadmin -f createagreement -2 -h vc03 -H vc04 -u Administrator -
Show All PSCs: Run this command to show all PSCs in the vSphere domain (replace
PSC_FQDNandadministratorwith your actual values):vdcrepadmin -f showservers -h PSC_FQDN -u administrator
-
Show Partners: Use this command to display the partner PSC (replace
PSC_FQDNandadministratorwith your actual values):vdcrepadmin -f showpartners -h PSC_FQDN -u administrator
- For more detailed instructions and troubleshooting, refer to the
fixpsc.shscript and the linked articles.
- For more detailed instructions and troubleshooting, refer to the
- Python Environment:
- Install Python 3.
- Create a virtual environment (recommended):
python3 -m venv venv - Activate the virtual environment:
- On Linux/macOS:
source venv/bin/activate - On Windows:
venv\Scripts\activate
- On Linux/macOS:
- Install the required Python packages:
pip install -r requirements.txt
- PowerCLI Environment:
- Install PowerCLI.
- Connect to your vCenter server:
Connect-VIServer <vcenter_server>
- Environment Variables:
- The python scripts use environment variables for vCenter credentials. Create a
.envfile in the project root directory and define the following variables:
VCENTER1_HOST=your_vcenter_host VCENTER1_USER=your_vcenter_username VCENTER1_PASSWORD=your_vcenter_password VCENTER2_HOST=...- Replace
your_vcenter_host,your_vcenter_username, andyour_vcenter_passwordwith your actual vCenter credentials. Add moreVCENTERn_...variables for additional vCenter servers.
- The python scripts use environment variables for vCenter credentials. Create a
- The original README.md contained information about backing up ESXi. This information has been retained below.
- The python scripts disable SSL verification, which is not recommended for production.
To backup ESXi, use the following steps:
-
Navigate to the backup directory:
cd /vmfs/volumes/KCP-SDX1-AO-DATASTORE01/esxi_backup -
Edit the
esxi_backup.shscript:vi esxi_backup.sh
Script Content:
#!/bin/sh vim-cmd hostsvc/firmware/sync_config vim-cmd hostsvc/firmware/backup_config find /scratch/downloads/ -name \*.tgz -exec cp {} /vmfs/volumes/KCP-SDX1-AO-DATASTORE01/ESXi_config_backup_$(hostname)_$(date +’%Y%m%d_%H%M%S’).tgz \; find /vmfs/volumes/KCP-SDX1-AO-DATASTORE01/ -type f -name '*.tgz' -mtime +20 -exec rm {} \;
-
Change permissions:
chmod +x esxi_backup.sh
-
Create Cron Job: Edit
/var/spool/cron/crontabs/rootand add the following line (adjust the path toesxi_backup.shif necessary):1 1 * * * /vmfs/volumes/KCP-SDX1-AO-DATASTORE01/esxi_backup.sh