Add multiple ESXi Hosts to vCenter with PowerCLI

When you want to add many VMware ESXi Hypervisors to your vCenter inventory, a small one-liner might save you a lot of time. You can add hosts based on consistent host numbering, IP-Addresses or use a textfile for input.

Snippet 1: Add multiple Hosts to vCenter by Hostname Range
This solution requires consistent hostnames. The snippet adds 20 Hosts named esx1.virten.lab - esx20.virten.lab to the datacenter Lab in your vCenter. You have to be connected to the vCenter where you want to add the hosts (Connect-VIServer).

1..20 | Foreach-Object { Add-VMHost esx$_.virten.lab -Location (Get-Datacenter Lab) -User root -Password <Password> -RunAsync -force:$true}

Parameters:

  • 1..20: Host Range. In this example, ESXi hosts numbered 1 - 20 in their name
  • esx$_.virten.lab: Hostname Template. Adds esx1.virten.lab - esx20.virten.lab
  • Lab: Datacenter Name (Must exist)
  • <Password>: Replace with your password
  • -RunAsync: Optional parameter. Much faster, runs all tasks parallel.

 

Snippet 2: Add multiple Hosts to vCenter by IP-Address Range
You can also add hosts based on it's IP-Address. As you want to use hostnames rather than IP-Addresses to manage the hosts in your vCenter you have to do a reverse lookup to get the DNS name. This snippet adds 10 Hosts with the IP-Address range 192.168.0.10 - 192.168.10.20 with its hostname to the datacenter Lab in your vCenter.

10..20 | ForEach-Object { [System.Net.Dns]::GetHostbyAddress("192.168.0.$_")  } | 
select-object HostName | ForEach-Object { Add-VMHost $_.HostName -Location (Get-Datacenter Lab) -User root -Password <Password> -RunAsync -force:$true }

Parameters:

  • 10..20: Host Range. In this example, ESXi hosts numbered 1 - 20 in their name
  • 192.168.0.$_: IP-Address range. Adds 192.168.0.10 - 192.168.0.20
  • Lab: Datacenter Name (Must exist)
  • <Password>: Replace with your password
  • -RunAsync: Optional parameter. Much faster, runs all tasks parallel.

 

Snippet 3: Add multiple Hosts to vCenter by Input Textfile
You can use a textfile to add multiple ESX hosts to your vCenter. This could be useful if you do not have consistent IP-Addresses or Hostnames. Just create a textfile with one host per line:
hosts

Get-Content hosts.txt | Foreach-Object { Add-VMHost $_ -Location (Get-Datacenter Lab) -User root -Password <Password> -RunAsync -force:$true}

Parameters:

  • hosts.txt: Input textfile
  • Lab: Datacenter Name (Must exist)
  • <Password>: Replace with your password
  • -RunAsync: Optional parameter. Much faster, runs all tasks parallel.

 

Snippet 4: Add multiple Hosts to vCenter with CSV file
You can also use a CSV file to add multiple ESX hosts to your vCenter. This helps you to further specify the process of adding ESXi Hosts (eg. ESXi hosts with different passwords):
hosts-csv

Import-Csv hosts.csv | Foreach-Object { Add-VMHost $_.name -Location (Get-Datacenter Lab) -User root -Password $_.password -RunAsync -force:$true}

Parameters:

  • hosts.csv: Input textfile
  • Lab: Datacenter Name (Must exist)
  • -RunAsync: Optional parameter. Much faster, runs all tasks parallel.
  1. hi, thank you for the topic, helped me .
    any chance we can automate it with python sdk?
    if yes, could you please share me the exact method name ?

  2. Hi

    how to make this work if I use different password on each server?
    Thanks
    J

  3. I'm new to powercli so bear with me.

    In snippet 2, Add multiple Hosts to vCenter by IP-Address Range, is there a way to make the 10..20 and the 192.168.0. a variable?

    I tried the following but it caused the script to bomb out and gave an error saying they weren't valid IP addresses starting at character 1 with the $host_range. I changed that back to not being a variable and reran but got the same error but at character 35 this time. I tinkered with the variables and the quote placement and stuff for a while but eventually accepted defeat.

    $host_range = "10.20"
    $host_address = "192.168.0"

    $host_range | ForEach-Object { [System.Net.Dns]::GetHostbyAddress("$host_address.$_") } |
    select-object HostName | ForEach-Object { Add-VMHost $_.HostName -Location (Get-Datacenter Lab) -User root -Password -RunAsync -force:$true }

    Thanks!

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>