$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.domain.bankanet.com.tr/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.domain.bankanet.com.tr/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 } }