November 21, 2024

ESX Post install config via PowerCLI

Working from home yesterday, I saw a tweet, and linked blog post, by Ivo Beerens, about configuring ESX via PowerCLI after the initial installation. Ivo has covered the bases pretty well for his environment.

I recently wrote a PowerCLI script to do some of the same things, but I did choose a somewhat different approach.  Our scripts differ in the following respects:

  1. My script is executed against the ESX host, and not against vCenter
  2. My script is geared toward a NetApp Filer as my backend storage
  3. My script operates as a menu, giving the flexibility to only configure one or more settings

Now on to my script.  I wrote this script knowing that I would have several hosts I needed to configure in a short amount of time.

# ===============================================================
#    NAME: ConfigMenu.ps1
#  AUTHOR: Jase McCarty
#    DATE: 1/28/2010
#
# ===============================================================

# Explain what the script does
Write-Host "This script will allow for configuration of several "
Write-Host "different components of an ESX Host"

# Ask for the Host name or IP
$ESXHOST = Read-Host "Enter ESX Host Name or IP"
Connect-VIServer $ESXHOST

# Setup Our Menu
do {

# Select environment MENU
Write-Host "*** Config Menu for $ESXHOST ***"
Write-Host "What task would you like to perform?"
Write-Host " 1. Adjust Service Console Memory"
Write-Host " 2. Adjust vSwtich0 Ports to 248"
Write-Host " 3. Add Port Groups to vSwitch0"
Write-Host " 4. Add VMKernel Port to vSwitch0 For VMotion Purpose"
Write-Host " 5. Add vSwitch1 for Storage Network"
Write-Host " 6. Update NFS & TCP settings for NFS Traffic"
Write-Host " 7. Update NTP Settings"
Write-Host " 8. Add Datastores to ESX Host"
Write-Host " 0. Quit"
Write-Host " "
$response = Read-Host "Select 1-8"
Write-Host " "

switch ($response)
{
1 {
"** Adjust the Service Console Memory Setting **";
Write-Host "Setting Service Console Memory to 800MB";
$consoleMemMb = 800;
Get-VMHost | Get-View | %{(Get-View -Id $_.ConfigManager.MemoryManager).ReconfigureServiceConsoleReservation($consoleMemMb*1mb)};
Write-Host "!!! The system will have to be rebooted for these settings to take place !!!";
Read-Host 'Press return to continue...';
Clear-Host
break;
}
2 {
"**Adjust Virtual Switch Ports for vSwitch0 to accomodate Many Virtual Machines **";
Get-VMHost $ESXHOST | Get-VirtualSwitch -Name "vSwitch0" |Set-VirtualSwitch -NumPorts "256";
Read-Host "Press return to continue...";
Clear-Host
break;
}
3 {
"** Add Port Groups to vSwitch0 (Nics must be trunked) **";
Write-Host "Adding Port Groups to vSwitch0";
$NVPG = Get-VMHost $ESXHOST | Get-VirtualSwitch -Name "vSwitch0" | New-VirtualPortGroup -Name "VLAN100" -VLanId "100";
$NVPG = Get-VMHost $ESXHOST | Get-VirtualSwitch -Name "vSwitch0" | New-VirtualPortGroup -Name "VLAN101" -VLanId "101";
Read-Host 'Press return to continue...';
Clear-Host
break;
}
4 {
"** Adding VMKernel Port to vSwitch0 For VMotion **";
Write-Host "Adding VMKernel Port to vSwitch0 For VMotion";
$VMOTIONIP = Read-Host "Enter VMotion IP";
$VMOTIONNM = Read-Host "Enter VMotion Subnet Mask";
$VMOTIONVLAN = Read-Host "Enter VMotion VLAN";
#Get-VMHost $ESXHOST | Get-VirtualSwitch -Name "vSwitch0";
$esxImpl = Get-VMHost $ESXHOST;
$vSw0 = Get-VirtualSwitch -VMHost $esxImpl -Name vSwitch0;
New-VMHostNetworkAdapter -VMHost $esxImpl -PortGroup "VMotion" -VirtualSwitch $vSw0 -IP $VMOTIONIP -SubnetMask $VMOTIONNM -VMotionEnabled $true;
Set-VirtualPortGroup -VirtualPortGroup "VMotion" -VlanId $VMOTIONVLAN
Read-Host 'Press return to continue...';
Clear-Host
break;
}
5 {
"**Add Second Virtual Switch to host for Storage Network **";
$STORAGESCIP = Read-Host "Enter Storage Service Console IP: ";
$STORAGESCNM = Read-Host "Enter Storage Service Console NetMask: ";
$STORAGEVKIP = Read-Host "Enter Storage VMKernel IP: ";
$STORAGEVKNM = Read-Host "Enter Storage VMKernel NetMask: ";

Write-Host "Adding vSwitch1 to Host";
Get-VMHost $ESXHOST | New-VirtualSwitch -Name "vSwitch1";
$esxImpl = Get-VMHost $ESXHOST;
$vSw = Get-VirtualSwitch -VMHost $esxImpl -Name vSwitch1;
Set-VirtualSwitch -VirtualSwitch $vsW -Nic vmnic1,vmnic2
New-VMHostNetworkAdapter -VMHost $esxImpl -PortGroup "StorageSC" -VirtualSwitch $vSw -IP $STORAGESCIP -SubnetMask $STORAGESCNM -ConsoleNic;
New-VMHostNetworkAdapter -VMHost $esxImpl -PortGroup "StorageVMKernel" -VirtualSwitch $vSw -IP $STORAGEVKIP -SubnetMask $STORAGEVKNM;

Read-Host 'Press return to continue...';
Clear-Host
break;          }
6 {
"**Update NFS/TCP Settings for NFS Traffic **";
Write-Host "Updating TCP and NFS Advanced Configuration Settings";
Set-VMHostAdvancedConfiguration -VMHost $ESXHOST -Name Net.TcpipHeapSize -Value 30;
Set-VMHostAdvancedConfiguration -VMHost $ESXHOST -Name Net.TcpipHeapMax -Value 120;
Set-VMHostAdvancedConfiguration -VMHost $ESXHOST -Name NFS.MaxVolumes -Value 64;
Set-VMHostAdvancedConfiguration -VMHost $ESXHOST -Name NFS.HeartbeatMaxFailures -Value 10;
Set-VMHostAdvancedConfiguration -VMHost $ESXHOST -Name NFS.HeartbeatTimeout -Value 5;
Set-VMHostAdvancedConfiguration -VMHost $ESXHOST -Name NFS.HeartbeatFrequency -Value 12;
Read-Host 'Press return to continue...';
Clear-Host
break;
}
7 {
"**Update NTP Settings **";
Write-Host "Updating NTP Server Values";
Add-VMHostNtpServer -NtpServer "ntp1.domain.com";
Add-VMHostNtpServer -NtpServer "ntp2.domain.com";
Get-VmhostFirewallException -Name “NTP Client“  |Set-VMHostFirewallException -enabled:$true;
Read-Host 'Press return to continue...';
Clear-Host
break;
}

8 {
"**Add Datastores to ESX Host **";
Write-Host "Adding Datastores to ESX Host";
Get-VMHost | New-Datastore -Nfs -NAME nfs_datastore -Path /vol/nfs_datastore -NfsHost x.x.x.x;
Read-Host 'Press return to continue...';
Clear-Host
break;
}
0 {
"** Exiting Script **";
Write-Host "Quit";
break;
}
default {
"** The selection could not be determined **";
break;
}
}

}
while ($response -ne "0")

As can be seen, I am performing some of the same tasks as Ivo’s script.  The script can be easily modified to fit just about any environment.  I have not yet added the snippets of code to update vmnic load balancing, failover, etc…  But it is on the way.

Again, use this script only after sufficient testing.  And as always, use of this script does not make me liable for any modifications to your environment.

$NVPG = Get-VMHost $ESXHOST | Get-VirtualSwitch -Name “vSwitch0” | New-VirtualPortGroup -Name “VLAN100” -VLanId “100”;

One thought on “ESX Post install config via PowerCLI

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.