VMware Cloud Community
deryambn
Enthusiast
Enthusiast
Jump to solution

Edit multiple ESXi hosts file with powercli by chosing esxi and entry

Hello,

I would like add all ESXis' hosts file the entry and wanna do it to chosing ESXi and an entry. I found some codes but it doesnt work. Could you help me

Thanks.

I found codes from there:  https://github.com/herseyc/PowerCLI-Scripts/blob/master/Update-HostsFile.ps1

This is the code:

$vCenterServer = Read-Host -Prompt 'Enter the FQDN of the vCenter Server you want to connect to (ex. vcenter.domain.com)'
Try {
Connect-VIServer -Server $vCenterServer -ErrorAction Stop | Out-Null
}
Catch {
Write-Host -ForegroundColor Red -BackgroundColor Black "Could not connect to the vCenter Server [$vCenterServer]." `n
Exit
}

while ($True){
### Prompt the user for the cluster name within that vCenter Server
$ClusterName = Read-Host -Prompt 'Enter the full name of the cluster you want to work with (ex. ProdCluster01)'

### Get information about all hosts in the defined cluster
$vmHosts = Get-Cluster -Name $ClusterName | Get-VMHost | Sort-Object

$cont = ""
$contR = ""
Do {
### Ask the user what step to perform
Write-Host `n
Write-Host "Please enter a task to perform on each host in the cluster:"
Write-Host "1.) Add parameter"
Write-Host "2.) Add Another parameter"
Write-Host "3.) Next Cluster"
Write-Host "4.) Exit" `n
$choice = Read-Host

### Perform a particular task based on user input
Switch ($choice) {
### If task #1 is chosen, add parameter to /etc/hosts
1 {
Write-Host -ForegroundColor Yellow `n "Retrieving current /etc/hosts file from $esxihost"
ForEach ($vmHost in $vmHosts) {
#Connectivity Information
$Username = "root" # ESXi Username
$Password = Read-Host -Prompt 'ESXi Password'

#New Host Entry
$addIP = Read-Host -Prompt ' New Entry IP'
$addHostname = Read-Host -Prompt ' New Entry Hostname'
$addAlias= Read-Host -Prompt ' New Entry Alias'

### Create authorization string and store in $head
$auth = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Username + ":" + $Password))
$head = @{"Authorization"="Basic $auth"}

# Request current hosts file
Write-Host "Retrieving current /etc/hosts file from $esxihost" -ForeGroundColor Green
$requesthostsfile = Invoke-WebRequest -Uri https://$esxihost/host/hosts -Method GET -ContentType "text/plain" -Headers $head

if ( $requesthostsfile.StatusCode -ne "200" ) {
Write-Host "Unable to retrieve current /etc/hosts file from $esxihost" -ForeGroundColor Red
Exit
}

# Add new line to hosts file with $addIP and $addHostname and $addAlias
$newhostsfile = $requesthostsfile.Content
$newhostsfile += "`n$addIP`t$addHostname`t$addAlias"

Write-Host "Contents of new /etc/hosts" -ForeGroundColor Green
Write-Host "-------------------------------------------------------"
Write-Host $newhostsfile
Write-Host "-------------------------------------------------------"

# Put the new hosts file on the host
Write-Host "Putting new /etc/hosts file on $esxihost"
$puthostsfile = Invoke-WebRequest -Uri https://$vmHost/host/hosts -Method PUT -ContentType "text/plain" -Headers $head -Body $newhostsfile

if ( $puthostsfile.StatusCode -ne "200" ) {
Write-Host "Unable to put new /etc/hosts file on $esxihost" -ForeGroundColor Red
Exit
}
Write-Host "Done!" -ForeGroundColor Green
}
}
$cont=$True
}
### If task #2 is chosen, Add Another Entry
2 {
Write-Host -ForegroundColor Yellow `n "Retrieving current /etc/hosts file from $esxihost"
ForEach ($vmHost in $vmHosts) {
#New Host Entry
$addanotherIP = Read-Host -Prompt ' New Entry IP'
$addanotherHostname = Read-Host -Prompt ' New Entry Hostname'
$addanotherAlias= Read-Host -Prompt ' New Entry Alias'
}

# Request current hosts file
Write-Host "Retrieving current /etc/hosts file from $esxihost" -ForeGroundColor Green
$requesthostsfile = Invoke-WebRequest -Uri https://$esxihost/host/hosts -Method GET -ContentType "text/plain" -Headers $head

if ( $requesthostsfile.StatusCode -ne "200" ) {
Write-Host "Unable to retrieve current /etc/hosts file from $esxihost" -ForeGroundColor Red
Exit
}

# Add new line to hosts file with $addIP and $addHostname and $addAlias
$newhostsfile = $requesthostsfile.Content
$newhostsfile += "`n$anotheraddIP`t$addanotherHostname`t$addanotherAlias"

Write-Host "Contents of new /etc/hosts" -ForeGroundColor Green
Write-Host "-------------------------------------------------------"
Write-Host $newhostsfile
Write-Host "-------------------------------------------------------"


# Put the new hosts file on the host
Write-Host "Putting new /etc/hosts file on $esxihost"
$puthostsfile = Invoke-WebRequest -Uri https://$vmHost/host/hosts -Method PUT -ContentType "text/plain" -Headers $head -Body $newhostsfile

if ( $puthostsfile.StatusCode -ne "200" ) {
Write-Host "Unable to put new /etc/hosts file on $esxihost" -ForeGroundColor Red
Exit
}
$cont=$True
}
### If task #3 is chosen, next cluster
3 {
Write-Host "Next Cluster..."
$cont = $false
$contR = $false
}
### If task #4 is chosen, exit the script
4 {
Write-Host "Exiting..."
$cont = $false
$contR = $True
}

### If user enters anything other than 1-4, input is invalid and ask question again
default {
Write-Host -ForegroundColor Red ">>> Invalid input. Please select option 1-4."
$cont = $true
$contR = $false
}
### Loop through the script until task #4 (Exit) is chosen
While ($cont)

if ($contR) {
break
}

 

0 Kudos
21 Replies
LucD
Leadership
Leadership
Jump to solution

You can do all escli commands via the Get-EsxCli cmdlet.

$esxcli = Get-esxcli -VMHost <ESXi-name> -V2
$esxcli.network.ip.hosts.remove.Invoke(@{ip='192.168.1.1';hostname='hostname.domain'})


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
deryambn
Enthusiast
Enthusiast
Jump to solution

Thank you so much @LucD . You are the best. 

0 Kudos