«

»

Add Multiple ESXi Hosts To vCenter Server Using PowerCLi

A few days back I created a vSphere cluster with 32 ESXi hosts and I really didn’t want to add all these hosts using the UI so I decided to put together a short powercli script to manage this.

The script that was tested using the following software versions:

  • VMware vCenter Server 6.5
  • VMware ESXi 6.5 build 5146846
  • VMware PowerCLI 6.5 Release 1 build 4624819
# PowerCLI script to add ESXi hosts to vCenter Server
# Version 1.0
# Magnus Andersson – Sr Staff Solution Architect @Nutanix
#
#
# ---------------------------------------------------
# Edit the information below to match your environment
#
# Specify vCenter Server, vCenter Server username, vCenter Server user password, vCenter Server location which can be the Datacenter, a Folder or a Cluster (which I used).
$vCenter="vcenter01.vcdx56.local"
$vCenterUser="mandersson@vcdx56.local"
$vCenterUserPassword="not-secret"
$vcenterlocation="npx005-01"
#
# Specify the ESXi host you want to add to vCenter Server and the user name and password to be used.
$esxihosts=("esxinut001.vcdx56.local","esxinut002.vcdx56.local","esxinut003.vcdx56.local","esxinut004.vcdx56.local")
$esxihostuser="root"
$esxihostpasswd="not-secret"
#
# You don't have to change anything below this line
# ---------------------------------------------------
#
#Connect to vCenter Server
write-host Connecting to vCenter Server $vcenter -foreground green
Connect-viserver $vCenter -user $vCenterUser -password $vCenterUserPassword -WarningAction 0 | out-null
#
write-host --------
write-host Start adding ESXi hosts to the vCenter Server $vCenter
write-host --------
#
# Add ESXi hosts
foreach ($esxihost in $esxihosts) {
Add-VMHost $esxihost -Location $vcenterlocation -User $esxihostuser -Password $esxihostpasswd
}
#
# Disconnect from vCenter Server
write-host "Disconnecting to vCenter Server $vcenter" -foreground green
disconnect-viserver -confirm:$false | out-null

 

Below is the script output when running in my lab environment.

If you want to limit the script output you can add “| out-null” to the add-vmhost section so the new line would be:

  • Add-VMHost $esxihost -Location $vcenterlocation -User $esxihostuser -Password $esxihostpasswd” | out-null

The new output will then be according to the below output:

Happy PowerCLI scripting