PowerShell Script to automate ESXI host add to vCenter | NTP configure | DNS configure | Place host into maintenance mode

#PowerShell script to automate ESXI configuration part post new ESXI deployment


$vCenter= Read-Host "Enter vCenter Name"

$VCCred = Get-Credential

#Add ESXI Host

$ESXI1= Read-Host "Enter ESXI host Name"

$ESXICred= Get-Credential


$String= $ESXI1

$String.Substring(0,$String.Length-12) | Out-file 'C:\temp\output.txt'

$Prop= Get-Content -Path 'C:\temp\output.txt'


#Connect vCenter 

Connect-VIServer -Server $vCenter -Credential $VCCred


#Collect Datacenter details 

$Datacent= Get-Datacenter


#Prompt for to enter Datacenter Name, enter here short name inside *.........*, 

$DCM= read-Host "*Enter DC shortname Name inside both*"


# String to remove extra character from DC that we are typing manually


$NTP.subString(1,$DCM.Length-2) |Out-file 'C:\temp\DCName.txt'

$DCName= Get-Content -Path 'C:\temp\DCName.txt'


Add-VMHost -Server $vCenter -Name $ESXI1 -Location $DCM -Credential $ESXICred -Force



Get NTP Server details




# PowerCli command to COllect NTP server details from existing Datacenter >host

Get-Datacenter | Where-Object {$_.Name -imatch "$DCName"} |Get-VMHost |Where-Object {$_.Name -imatch "$Prop"} |Get-VMhostNTPServer |Out-file 'C:\temp\NTPServer.txt'

$NTPServer= Get-Content -Path 'C:\temp\NTPServer.txt'



Get-Datacenter |Where-Object {$_.Name -imatch "$DCName"} |Get-VMHost |Out-File 'c:\temp\VMhost.txt'

$NTPHost= Get-Content -Path 'C:\temp\vmhost.txt'



#PowerCli command to add existing NTP server details into new ESXI host

Add-VMHostNtpServer -NtpServer $NTPServer -VMHost $ESXI1 | Where-Object {$_.Name -imatch "$ESXI1"}


# PowerCli command to Collect DNS Name, DNS IP, SearchDomain

Get-Datacenter | Where-Object {$_.Name -imatch "$DCName"} |Get-VMHost |Where-Object {$_.Name -imatch "$ESXI1"} | Get-VMHostNetwork | Select-Object DomainName, SearchDomain, DnsAddress |Out-file 'C:\temp\NetInfo.txt'


$NetInfo= Get-Content -Path 'C:\temp\NetInfo.txt'



# PowerCli Command to add DNS Address, SearchDomain

$vmHostNetworkInfo = Get-VmHostNetwork -Host $ESXI1


Set-VmHostNetwork -Network $vmHostNetworkInfo -DnsAddress '','' -SearchDomain temp.ads.com -DnsFromDhcp $false



#Put ESXI host into maintenance mode


Set-VMHost -VMHost $vmhost -State "Maintenance" 

Featured Post

HPE MSA 2040 configuration step by step

HPE MSA 2040 configuration Default IP range for HP SAN storage MSA 2040 is You need to connect your laptop and storage with...