Creating a VMware Nested Lab with PowerCLI can be a game-changer for lab setups, testing, and development. With PowerCLI, you can automate the deployment of multiple nested ESXi VMs, making the process seamless and efficient. In this guide, weβll walk through how to use a PowerCLI script to automate the creation of a nested environment.
Why Automate Nested ESXi VM Deployment?
Manually setting up multiple ESXi hosts can be time-consuming and prone to errors. By automating this process using PowerCLI, you can:
- Deploy multiple ESXi VMs with predefined configurations.
- Ensure consistency across all deployments.
- Save time by eliminating manual steps.
- Easily scale and modify the setup when needed
Step-by-Step Guide to Automating Nested VM Deployment
Step 1: Install and Import VMware PowerCLI
PowerCLI is a powerful module for managing VMware environments. If you havenβt installed it yet, open PowerShell as an administrator and run:
Install-Module VMware.PowerCLI -Scope CurrentUser -Force
Once installed, import the module:
Import-Module VMware.PowerCLI
Step 2: Connect to vCenter
The script starts by securely connecting to vCenter:
$vcCreds = Get-Credential -Message "Enter vSphere credentials" Connect-VIServer -Server "your-vcenter-ip" -Credential $vcCreds
This prompts you for your vCenter credentials and establishes a connection.
Step 3: Define Configuration Variables
Customize these variables according to your environment:
$esxiOvaPath = "your-ova-file-path" $esxiDatastore = "your-datastore" $esxiHost = "your-esxi-host" $esxiNetwork = "Nested" # Ensure this is a port group with VLAN tagging enabled $esxiRootPassword = "VMware1!" $extraDiskSizeGB = 50 # Additional disk size for vSAN (optional)
These variables define the OVA template path, target datastore, network settings, and root password for the ESXi VMs.
Step 4: Define the List of Nested ESXi VMs
Create an array to store the configuration for each ESXi VM:
$esxiHosts = @( @{ VmName = "vcf-m01-esx01"; Hostname = "vcf-m01-esx01.pexpert.inc"; IpAddress = "172.16.10.11"; Netmask = "255.255.255.0"; Gateway = "172.16.10.1"; Dns = "172.16.10.2" }, @{ VmName = "vcf-m01-esx02"; Hostname = "vcf-m01-esx02.pexpert.inc"; IpAddress = "172.16.10.12"; Netmask = "255.255.255.0"; Gateway = "172.16.10.1"; Dns = "172.16.10.2" }, @{ VmName = "vcf-m01-esx03"; Hostname = "vcf-m01-esx03.pexpert.inc"; IpAddress = "172.16.10.13"; Netmask = "255.255.255.0"; Gateway = "172.16.10.1"; Dns = "172.16.10.2" }, @{ VmName = "vcf-m01-esx04"; Hostname = "vcf-m01-esx04.pexpert.inc"; IpAddress = "172.16.10.14"; Netmask = "255.255.255.0"; Gateway = "172.16.10.1"; Dns = "172.16.10.2" } )
Each entry defines the VM name, hostname, IP settings, and network configurations.
Step 5: Deploy the Nested ESXi VMs
Loop through each configuration and deploy the ESXi VM:
foreach ($esxiHostConfig in $esxiHosts) { Write-Host "π Deploying Nested ESXi Host: $($esxiHostConfig.VmName)..." # Prepare OVF Configuration $ovfConfig = @{ "guestinfo.hostname" = $esxiHostConfig.Hostname "guestinfo.ipaddress" = $esxiHostConfig.IpAddress "guestinfo.netmask" = $esxiHostConfig.Netmask "guestinfo.gateway" = $esxiHostConfig.Gateway "guestinfo.dns" = $esxiHostConfig.Dns "guestinfo.domain" = "pexpert.inc" "guestinfo.root_password" = $esxiRootPassword } # Deploy VM try { $esxiVm = Import-VApp -Source $esxiOvaPath -Name $esxiHostConfig.VmName ` -VMHost (Get-VMHost -Name $esxiHost) -Datastore (Get-Datastore -Name $esxiDatastore) ` -DiskStorageFormat "Thin" -OvfConfiguration $ovfConfig -Confirm:$false -ErrorAction Stop Start-VM -VM $esxiVm -Confirm:$false Write-Host "β Nested ESXi Host '$($esxiHostConfig.VmName)' deployed and powered on." # Configure Network Adapter $networkAdapter = Get-NetworkAdapter -VM $esxiVm Set-NetworkAdapter -NetworkName $esxiNetwork -NetworkAdapter $networkAdapter -Confirm:$false Write-Host "π Network adapter configured for $($esxiHostConfig.VmName)." # Add Extra Disks for vSAN Write-Host "πΎ Adding extra disks to ESXi host: $($esxiHostConfig.VmName)..." for ($i = 1; $i -le 2; $i++) { New-HardDisk -VM $esxiVm -CapacityGB $extraDiskSizeGB -StorageFormat "Thin" -Confirm:$false Write-Host "β Added $extraDiskSizeGB GB disk $i to $($esxiHostConfig.VmName)." } } catch { Write-Host "β Error deploying $($esxiHostConfig.VmName): $_" continue } }
This script:
- Deploys the ESXi VM using the OVA template.
- Configures the network.
- Adds extra disks if needed.
Step 6: Disconnect from vCenter
Once the deployment is complete, safely disconnect from vCenter:
if ($global:DefaultVIServers.Count -gt 0) { Disconnect-VIServer -Confirm:$false Write-Host "π Disconnected from vSphere environment." } else { Write-Host "π No active vSphere connections found." }
Conclusion
With this PowerCLI script, you can automate the creation of a nested ESXi environment in vSphere. This approach is perfect for: β
Lab setups
β
Testing environments
β
vSAN, vSphere, or NSX-T simulations
By using VMware Nested Lab with PowerCLI, you eliminate manual errors, ensure consistency, and deploy multiple VMs quickly. Modify the script based on your environment and start automating your nested VMware deployments today! π