Ok, so I rushed a bit into getting the last post about this online as I hadn’t blogged in a while. However nearly as soon as I finished I started getting more ideas about how I could improve the script. The next day I started improving the script and done some testing and now It allows you to use 4 CSV Files to add Datacenters, Folders, Clusters and Hosts intou your VC Environment.
I am already having ideas about the next version, I am thinking about the possibilities of having one script that exports Inventory information using export-csv and then another script that uses them same exported CSVs. This will be really useful for rebuilding a VC, and could possibly be done as a scheduled job on a daily basis so that you always have the files there incase it crashes. Anyways That probably won’t be here anytime soon unfortunately (very busy next few months) but it is on my list of things to work on.
This Script works for environments that need to be populated using the Datacenter > Folder > Cluster > Hosts Heirachy in the inventory.
The Four CSV Files
This script uses 4 CSV Files to populate the inventory from. One for each inventory item. The CSV Files are:
data.csv – A List of Datacenters that Need Adding.
Datacenter, Location
DC01, Datacenters
DC02, Datacenters
Note: Leave the location field as “Datacenters” if you add your Datacenters into the root Folder Like we do. This is the default root folder for new Datacenters.
folder.csv – Folders to be added into Datacenters.
Folder, Location
Development, DC01
Production, DC01
Staging, DC02
clu.csv – Clusters to be added into Folders
Cluster, Location, DRS, DRSMode, HA
clu01, Production, Yes, FullyAutomated, Yes
clu02, Production, Yes, FullyAutomated, No
clu03, Production, No, n/a, Yes
clu04, Staging, No, n/a, No
Note: DRS and HA Values must be “Yes” or “No”. If DRS is “No” then DRSMode must hold the “n/a” Value.
hosts.csv – List of Hosts to be added into Clusters
Hostname, Password, User, Folder, Cluster
host1.domain.com, MyPass, root, Development, n/a
host2.domain.com, MyPass, root, Development, n/a
host3.domain.com, MyPass, root, Development, n/a
host4.domain.com, MyPass, root, Production, clu01
host5.domain.com, MyPass, root, Production, clu01
Note: Hosts that are not in clusters and just sit under folders must have the “n/a” value in the Cluster column.
The PowerCLI Script – Broken Down.
First things First – Set Variables
## —-Set Variables—-
$vcenter = 192.168.1.5
$hostscsv = Import-Csv “C:\PowerCLI\hosts.csv”
$datacsv = Import-Csv “C:\PowerCLI\data.csv”
$foldercsv = Import-Csv “C:\PowerCLI\folder.csv”
$clucsv = Import-Csv “C:\PowerCLI\clu.csv”
Here we add the paths to the CSV Files and the vCenter IP Address/Hostname etc.
Keeping it Simple – Connect to vCenter
## —-Connect to VirtualCenter—-
Connect-VIserver $vcenter
Add Datacenters from data.csv
## —-Add Datacenters From CSV—-
$datacsv | % {
$Name = $_.Datacenter
$Location = Get-Folder -Name $_.Location
New-Datacenter -Location $Location -Name $Name
}
Add Folders from folder.csv
## —-Add Folders From CSV—-
$foldercsv | % {
$Name = $_.Folder
$Location = Get-Datacenter -Name $_.Location
New-Folder -Location $Location -Name $Name
}
Add Clusters from clu.csv
## —-Add Clusters From CSV—-
$clucsv | % {
$Name = $_.Cluster
$Location = Get-Folder -Name $_.Location
$DRS = $_.DRS
$DRSMode = $_.DRSMode
$HA = $_.HA
if ($DRS -eq “No” -AND $HA -eq “No”)
{
New-Cluster -Location $Location -Name $Name
}
elseif ($DRS -eq “No” -AND $HA -eq “Yes”)
{
New-Cluster -Location $Location -Name $Name -HAenabled:$true
}
elseif ($DRS -eq “Yes” -AND $HA -eq “Yes”)
{
New-Cluster -Location $Location -Name $Name -DRSEnabled -DRSMode $DRSMode -HAenabled:$true
}
elseif ($DRS -eq “Yes” -AND $HA -eq “No”)
{
New-Cluster -Location $Location -Name $Name -DRSEnabled -DRSMode $DRSMode
}
}
You can see this uses various If and Elseif statements to decide how to configure HA and DRS. The example CSV above has one of each possible selection.
Nearly there – Add Hosts from host.csv
## —-Add Hosts From CSV—-
$hostscsv | % {
$PW = $_.Password
$HN = $_.Hostname
$Cluster = $_.Cluster
$User = $_.User
$Folder = $_.Folder
$getfolder = Get-Folder -Name $_.Folder
if ($Cluster -ne “n/a”)
{
$getcluster = Get-Cluster -Name $_.Cluster
}
if ($Cluster -eq “n/a”)
{
Add-VMhost -Name $HN -User $User -Password $PW -Location $getfolder -Force
}
else
{
Add-VMhost -Name $HN -User $User -Password $PW -Location $getcluster -Force
}
}
This is one of the main improvements from my first post about this script. The original was kicking out errors, now I have added in If statements to make sure that this was stopped.
And Finally – Disconnect from vCenter
## —-Disconnect From VirtualCenter—-
Disconnect-VIServer -Server * -Confirm:$false -Force:$true
All of the files that I used for this script can be downloaded below:
data.csv
folder.csv
clu.csv
hosts.csv
populatevc.ps1
Zip Bundle
I hope that this helped
Steve
