How to Add Multiple ESXi Hosts to vCenter Using PowerCLI – Automation – VirtuallyThatGuy

Ever wondered how to add a bunch of ESXi Hosts to a newly or existing vCenter using powershell or automate with powercli? Well below script can help you achieve just that:

Join me on this journey of automating this process using the below. You can save as .ps1 and call the script in PowerCLI or PowerShell.​​ 

#

# 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="UK3P-vc01.lab.local"

$vCenterUser="rboadi@lab.local"

$vCenterUserPassword="xxxxxx"

$vcenterlocation="DTCP-Cluster"

#

# Specify the ESXi host you want to add to vCenter Server and the user name and password to be used.

$esxihosts=("DTCP-ESXi01.lab.local","DTCP-ESXi02.lab.local","DTCP-ESXi03.lab.local")​​ #(Get-VMHost -Name "UK3P-*")

$esxihostuser="root"

$esxihostpasswd="xxxxxxx"

#

# The variables specified above will be used to add hosts to vCenter

# ------------------------------------------------------------------

#

#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​​ Starting to​​ add​​ 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​​ -Force

}

#

# Disconnect from vCenter Server

write-host​​ "Disconnecting to vCenter Server​​ $vcenter"​​ -foreground​​ green

disconnect-viserver​​ *​​ -confirm:$false​​ |​​ out-null

 

​​ 

You can change​​ the​​ $esxihosts​​ to​​ a wildcard and save you some typing replace with something​​ like​​ #(Get-VMHost -Name "DTCP-*").​​ 

#
# 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="UK3P-vc01.lab.local"
$vCenterUser="rboadi@lab.local"
$vCenterUserPassword="xxxxxx"
$vcenterlocation="DTCP-Cluster"
#
# Specify the ESXi host you want to add to vCenter Server and the user name and password to be used.
$esxihosts=("DTCP-ESXi01.lab.local","DTCP-ESXi02.lab.local","DTCP-ESXi03.lab.local") #(Get-VMHost -Name "UK3P-*")
$esxihostuser="root"
$esxihostpasswd="xxxxxxx"
#
# The variables specified above will be used to add hosts to vCenter
# ------------------------------------------------------------------
#
#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 Starting to add 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 -Force
}
#
# Disconnect from vCenter Server
write-host "Disconnecting to vCenter Server $vcenter" -foreground green
disconnect-viserver * -confirm:$false | out-null


You May Also Like

About the Author: VirtuallyThatGuy

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *