Using PowerCLI to Build Out vCenter

Share on:

I wanted to share this handy script i created when you have a need to build out a new vCenter.

This script can be extremely handy as it will take care of creating the datacenter, cluster, adding hosts, creating datastores and then applying the appropriate host profile. It is all done via CSV files so it is extremely customizable.

Files to modify to customize your configuration

  • createdatacenter.csv - Contains the name of the datacenter to create.
  • createcluster.csv - Contains the name of the cluster to create and the datacenter to put it in.
  • createhosts.csv - Contains the Name of the host and which cluster to place them in.
  • createdatastores.csv - Contains the name of the datastore, naaid of the lun and the datastore cluster to put it in.
  • applyhostprofile.csv - Contains the name of the host and hostprofile to apply.

The code and Github Link can be found below.

ConfigvCenter.ps1

 1# Variables
 2$vcenter = "vc.lab.local"
 3$esxusername = "root"
 4$esxpassword = "password"
 5$DSC = "DSC01"
 6$Datacenter = "Lab"
 7
 8#Connect to vCenter
 9#Connect-VIServer -Server $vCenter
10
11# Create Datacenters
12Write-Host "Creating Datacenter" -ForegroundColor "Green"
13$csv = Import-CSV .\createdatacenter.csv
14foreach ($datacenter in $csv) {
15  New-Datacenter -Location (Get-Folder -NoRecursion) -Name $datacenter.Name
16}
17
18# Create Clusters
19Write-Host "Creating Clusters" -ForegroundColor "Green"
20$csv = Import-CSV .\createcluster.csv
21foreach ($cluster in $csv) {
22  New-Cluster -Name $cluster.Name -Location $cluster.Datacenter -DRSEnabled -DrsAutomationLevel FullyAutomated -HAEnabled
23}
24
25# Create Clusters
26Write-Host "Adding Hosts" -ForegroundColor "Green"
27$csv = Import-CSV .\createhosts.csv
28foreach ($vmhost in $csv) {
29  Add-VMHost -Name $vmhost.Name -Location $vmhost.Cluster -User $esxusername -Password $esxpassword
30  Set-VMHost -VMHost $vmhost.Name -State Maintenance
31}
32
33# Create Datastores
34Write-Host "Creating Datastores" -ForegroundColor "Green"
35$csv = Import-CSV .\createdatastores.csv
36foreach ($datastore in $csv) {
37  New-Datastore -VMHost $datastore.host -Name $datastore.Name -Path $datastore.NAAID -VMFS
38}
39
40# Create Datastore Cluster and Move Datastores In
41Write-Host "Creating Datastore Cluster and Adding Datastores" -ForegroundColor "Green"
42$csv = Import-CSV .\createdatastores.csv
43New-DatastoreCluster -Name $DSC -Location $Datacenter
44foreach ($datastore in $csv) {
45  Move-Datastore $datastore.Name -Destination $datastore.DatastoreCluster
46}
47
48# Apply HostProfile
49Write-Host "Applying HostProfiles" -ForegroundColor "Green"
50$csv = Import-CSV .\applyhostprofile.csv
51foreach ($hostprofile in $csv) {
52  Apply-VMHostProfile -Entity $hostprofile.Entity -Profile $hostprofile.Profile -Confirm:$false
53}
54
55Write-Host "Complete!" -ForegroundColor "Green"
comments powered by Disqus

See Also