In building out a new VM Datacenter I had the task of adding quite a few VM hosts in short order. To make light work of adding multiple new hosts I decided to script it out and below are the results. It makes it easy:
####################################
#Add New VMware Hosts from TXT file#
# Specifcy Parameters Below #
# http://vm-pro.com #
####################################
$hostfile = get-content "C:\Scripts\new-hosts.txt"
$Location = "VMDatacenterOrClusterName"
$Username = "root"
$Password = ""
################################
$count = 0
foreach ($hostToAdd in $hostfile)
{
Write-Host $hostToAdd "Added to VMware DataCenter " $Location
Add-VMHost -Name $hostname -Location $Location -User $Username -Password $Password -Force
$count = $count + 1
}
Write-Host "Total Hosts Added: " $count
As you can see, I’ve opted to specify all variables, including the location of a txt file containing each of my new vm hosts to add in, at the top of the script. This makes it much easier to customize and reuse in the future.
Ensure that your txt file contains only 1 host per line, and no additional markup.
We then loop through the text document adding hosts individually and displaying a little output to the console window via this command:
[powercli]
#
Write-Host $hostToAdd “Added to VMware DataCenter ” $Location
#
[/powercli]

