PowerCLI script to prepare ESXi hosts for VCF

With VMware Cloud Foundation, there is a number of prerequisites needed to be configured on an ESXi host before it can be commissioned or used for a management domain bringup.

When dealing with multiple ESXi hosts it is often quicker to script the configuration needed.

Based on the official documentation, the following needs to be configured before we can use the ESXi host with VCF:
1. DNS configuration (DNS servers, domain name, search domain)
2. NTP and SSH started and policy set to Start and stop with host
3. VM Network portgroup tagged with the same VLAN ID as Management Network

In addition to this, I often add the license key and rename the local datastore as part of the preparation.

Attached is a PowerCLI script that can be used to speed up the preparation.

<# Written by Martin Gustafsson, mgustafsson (at) vmware.com
This is a script used to prepare ESXi hosts for VMware Cloud Foundation Deployments. 
It can be used for both the management domain and VI workload domains. 
It uses the ESXi hosts defined in C:\temp\esxlist.txt, one host per line.
The following is being configured:
Add the ESXi license
Start SSH and configure the policy to Start and Stop with the Host
Start NTP, configure the NTP source and set the policy to Start and stop with the Host
Configuring DNS servers, domain name and search domains.
Tag the portgroup "VM Network" with the same VLAN ID as the Management Network
Rename the local datastore to <short name>.local

This is based on the requirements as per our official documentation 

$user = "root" 
$Passwd = "VMware123!"
$esxlist = get-content C:\temp\esxlist.txt
$ntpserver = "se.pool.ntp.org"
$domainname = "eng.vmware.com"
$searchdomain = "eng.vmware.com"
$dns1 = ""
$dns2 = ""

foreach ($esx in $esxlist) {
Connect-VIServer $esx -User $user -Password $Passwd

Write-Host -Object "Adding license key on $esx" -ForegroundColor Green
Set-VMHost -VMHost $esx -LicenseKey $license

Write-Host -Object "Starting SSH on $esx" -ForegroundColor Green
$sshstatus= Get-VMHostService -VMHost $esx| where {$psitem.key -eq "tsm-ssh"}
if ($sshstatus.Running -eq $False) {
Get-VMHostService | where {$psitem.key -eq "tsm-ssh"} | Start-VMHostService }
Get-VMHostservice | Where-object {$_.key -eq "tsm-ssh" } | Set-VMHostService -policy "On"

Write-Host -Object "Configuring NTP to $ntpserver on $esx" -ForegroundColor Green
Add-VMHostNtpServer $ntpserver
Get-VMHostFirewallException | where {$_.Name -eq "NTP client"} | Set-VMHostFirewallException -Enabled:$true
Get-VMHostService | Where-Object {$_.key -eq "ntpd"} | Start-VMHostService
Get-VMHostService | Where-Object {$_.key -eq "ntpd"} | Set-VMHostService -policy "On"

Write-Host "Configuring DNS and Domain Name on $esx" -ForegroundColor Green
Get-VMHostNetwork -VMHost $esx | Set-VMHostNetwork -DomainName $domainname -DNSAddress $dns1 , $dns2 -SearchDomain $searchdomain -Confirm:$false

Write-Host "Getting VLAN ID from Management Network on $esx" -ForegroundColor Green
$vlanid = Get-VirtualPortgroup -name "Management Network" | % { $pg=$_ ; get-vmhostnetworkadapter -portgroup $pg | % { "$($pg.vlanid)" }}

Write-Host -Object "Tagging VM Network with vlan $vlanid on vSwitch0 on $esx" -ForegroundColor Green
Get-VirtualSwitch -name vSwitch0 | Get-VirtualPortgroup -name "VM Network" | Set-VirtualPortgroup -vlanid $vlanid

Write-Host -Object "Renaming local datastore to .local on $esx" -ForegroundColor Green
Get-Datastore -Name datastore1* | %{ $n = '' + (Get-VMHost -Id $_.ExtensionData.Host[0].Key[0]).Name.Split('.')[0] + '.local';Set-Datastore -Datastore $_ -Name $n }

Disconnect-VIserver * -confirm:$false

1 Comment

Add yours →

  1. Thank you Martin for this Amazing script,

    Just a quick tip for anyone using Powershell from Linux. You can directly store your hosts in the variable.
    $esxlist = ‘’, ‘’, ‘’

Leave a Reply

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

© 2024 mgustafsson – yet another VMware blog

Theme by Anders NorénUp ↑