Site icon Digital Thought Disruption

Automating Host Lifecycle Management with PowerCLI: From Discovery to Decommission

Introduction

Adding and removing ESXi hosts should be repeatable, reliable, and policy-driven. Whether you’re expanding a vSphere cluster or retiring old hardware, PowerCLI allows you to automate every phase of the host lifecycle. This article provides a modular workflow to manage the lifecycle of ESXi hosts, ensuring they meet compliance and operational standards from day one to retirement.


My Personal Repository on GitHub

VMware Repository on GitHub


Workflow Overview

We will automate the following:

  1. Discover and validate new ESXi hosts
  2. Configure networking, DNS, NTP, and services
  3. Add hosts to vCenter and clusters
  4. Assign licenses
  5. Drain, remove, and decommission aging hosts

Step 1: Discover and Validate New ESXi Hosts

# Example list of new host IPs
$newHosts = @("192.168.1.101", "192.168.1.102")

foreach ($host in $newHosts) {
Test-Connection -ComputerName $host -Count 2 | Select Address, StatusCode
}

Validate access and DNS resolution:

Resolve-DnsName esxi01.lab.local

Step 2: Add Hosts to vCenter

Add-VMHost -Name "esxi01.lab.local" `
-User "root" -Password "YourRootPass" `
-Location "LabCluster" -Force

Step 3: Configure Host Networking

Get-VMHost -Name "esxi01.lab.local" | Set-VMHostNetwork -DomainName "lab.local" -DnsAddress "192.168.1.1","192.168.1.2"

New-VirtualSwitch -VMHost "esxi01.lab.local" -Name "vSwitch1" -Nic vmnic1
New-VirtualPortGroup -VirtualSwitch "vSwitch1" -Name "Prod_Network"

Step 4: Configure NTP and Services

$ntp = "0.pool.ntp.org","1.pool.ntp.org"

$host = Get-VMHost -Name "esxi01.lab.local"
$ntp | ForEach-Object { Add-VMHostNtpServer -VMHost $host -NtpServer $_ }

$svc = $host | Get-VMHostService | Where-Object {$_.Key -eq "ntpd"}
Start-VMHostService -HostService $svc
Set-VMHostService -HostService $svc -Policy "on"

Step 5: Assign Host License

Get-VMHost -Name "esxi01.lab.local" | Set-VMHost -LicenseKey "AAAAA-BBBBB-CCCCC-DDDDD-EEEEE"

Check license assignment:

Get-VMHost | Select Name, LicenseKey

Step 6: Validate and Audit Configuration

Get-VMHost | Select Name, ConnectionState, Version, Build, @{N="DNS";E={$_.ExtensionData.Config.Network.DnsConfig.Address}}, @{N="NTP";E={($_ | Get-VMHostNtpServer).NtpServer}}

Export configuration to CSV for recordkeeping:

Get-VMHost | Export-Csv "C:\Reports\Host_Inventory_After_Onboarding.csv" -NoTypeInformation

Step 7: Drain and Remove Decommissioned Hosts

# Step 1: Migrate VMs
Get-VM -Location "esxi04.lab.local" | Move-VM -Destination (Get-Cluster -Name "LabCluster")

# Step 2: Put host into maintenance mode
Set-VMHost -VMHost "esxi04.lab.local" -State Maintenance

# Step 3: Remove host from cluster and vCenter
Remove-VMHost -VMHost "esxi04.lab.local" -Confirm:$false -Force

Diagram: Host Lifecycle Automation Flow


Use Case: Rack-and-Ready Auto-Onboarding for New Clusters

PowerCLI can be paired with PXE/AutoDeploy to:

This forms the foundation for zero-touch host provisioning in enterprise-scale data centers.


Troubleshooting Tips

ProblemFix
Host fails to add to vCenterValidate FQDN, firewall, and root credentials
DNS not updatingUse Set-VMHostNetwork and verify DNS servers
NTP sync not workingEnsure ntpd is enabled and has internet or upstream access
Host removal blocked by resourcesEnsure no VMs or ISO mounts remain on host

What’s Next

The next article will focus on vSAN automation using PowerCLI:

Exit mobile version