This code will create menus for you to update Advanced Settings (or edit it to do other things… it’s the menus that are most important) for your ESXi hosts. It connects to vCenter, creates a menu of clusters to select from, creates a menu of hosts to select from and then executes the change. It puts the host in maintenance mode, updates the value and reboots the host.
The code runs past the edge of the page, but there are controls on the below widget to expand it so you can see all of it. 🙂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
$WarningPreference = 'SilentlyContinue' Disconnect-VIServer * -Force -Confirm:$false -ErrorAction SilentlyContinue function Create-ClusterMenu ($objClusters) { $intCount = 0 foreach ($objCluster in $objClusters) { $strClusterName = $objCluster.Name Write-Host "[$intCount] $strClusterName" $intCount++ } } function Create-HostMenu ($objVMHosts) { $intCount = 0 foreach ($objVMHost in $objVMHosts) { $strHostName = $objVMHost.Name Write-Host "[$intCount] $strHostName" $intCount++ } } Connect-VIServer vcenter.vmware.com $intCount = 0 $strAdvancedOption = "Numa.LocalityWeightActionAffinity" $intAdvancedOptionValue = 0 $objClusters = Get-Cluster | Sort-Object Name do { Create-ClusterMenu $objClusters $intClusterSelection = Read-Host "Enter a selection" $objCluster = $objClusters[$intClusterSelection] } while (-not $objCluster) $strClusterName = $objCluster.Name $objVMHosts = $objCluster | Get-VMHost | Where-Object { $_.ConnectionState -eq "Connected" -or $_.ConnectionState -eq "Maintenance" } | Sort-Object Name do { do { Create-HostMenu $objVMHosts $intHostSelection = Read-Host "Enter a selection" $objVMHost = $objVMHosts[$intHostSelection] } while (-not $objVMHost) $strVMHostName = $objVMHost.Name Write-Host -ForegroundColor Green "PROCESSING: $strVMHostName [$strClusterName]" $strVMHostConnectionState = $objVMHost.ConnectionState If ($strVMHostConnectionState -ne "Maintenance") { Write-Host " - Putting host into Maintenance Mode... " -NoNewline $objVMHost | Set-VMHost -State "Maintenance" | Out-Null Write-Host -ForegroundColor Green "[COMPLETE]" } else { Write-Host -ForegroundColor Yellow " - Host is already in Mainteance Mode... [SKIPPED]" } Write-Host " - Configuring NUMA Setting on host... " -NoNewline Get-AdvancedSetting -Entity $objVMHost -Name $strAdvancedOption | Set-AdvancedSetting -Value $intAdvancedOptionValue -Confirm:$false | Out-Null [string]$strAdvancedSettingValue = Get-AdvancedSetting -Entity $objVMHost -Name $strAdvancedOption if ($strAdvancedSettingValue -eq "$strAdvancedOption:0") { Write-Host -ForegroundColor Green "[COMPLETE]" } else { Write-Host -ForegroundColor Red "[ERROR]" exit } Write-Host " - Restarting host..." -NoNewline $objVMHost | Restart-VMHost -Confirm:$false | Out-Null while ($strConnectionState -ne "NotResponding") { Write-Host "." -NoNewline Start-Sleep -Seconds 5 $objVMHost = Get-VMHost $strVMHostName $strConnectionState = $objVMHost.ConnectionState } Write-Host -ForegroundColor Green " [COMPLETE]" Write-Host " - Waiting for host to come back online..." -NoNewline while ($strConnectionState -ne "Maintenance") { $objVMHost = Get-VMHost $strVMHostName $strConnectionState = $objVMHost.ConnectionState Write-Host "." -NoNewline Start-Sleep -Seconds 10 } Write-Host -ForegroundColor Green " [COMPLETE]" if ($strConnectionState -eq "Maintenance" -and $strVMHostConnectionState -eq "Connected") { Write-Host " - Taking host out of Maintenance Mode... " -NoNewline $objVMHost | Set-VMHost -State "Connected" | Out-Null Write-Host -ForegroundColor Green "[COMPLETE]" } else { Write-Host -ForegroundColor Yellow " - Host was previously in Maintenance Mode and will not be removed... [SKIPPED]" } } while ($intHostSelection -ne 99) Disconnect-VIServer * -Force -Confirm:$false |
Â