...hopefully some useful VMware related stuff

ESXi 5 - PowerShell Host Setup Script

23/08/2012 17:00

Use this script to configure a freshly installed ESXi 5 host

Requires PowerCLI (tested / developed on PowerCLI 5 against ESXi 5 and 5 update 1 Hosts)

A series of parameters like iSCSI Target addresses are manually entered to the script, the rest, including host name and specific vmkernel port IP's are captured by a Read-Host when the script is run.

 

  • Enter Maintenance Mode
  • vMotion - Portgroup and vmkernel
  • VM Network - removes from vSwitch0
  • iSCSI Storage - Portgroups, Active / Unused NIC best practice, Software iSCSI Enable, Target Configuration, vmkernel binding, Multipath to Round Robin
  • NFS Mounts - Portgroup and Mounting of storage
  • NTP Settings - NTP Server, Firewall Opening, Service Start
  • vSwitch Configuration - vmnic assignments, VM Network portgroups with VLANs
  • Local Storage - Renamed to host_local
  • Optionally Enables SSH and suppresses warning (for Test hosts only)
  • Prompts for Reboot

 

Download the script  HERE 

Hopefully someone else can make use of this too or offer advice to expand / develop it further!

 

### The Script ###

 

# Initial Setup Script for ESXi Hosts
# 20120820
# https://www.vdan.co.uk

#####################################################
# Set Credentials, Host and IPs and other Variables #
#####################################################

Clear
""
"Initial Setup Script for ESXi Hosts"
"20120820"
"https://www.vdan.co.uk"
""
# Host and Credentials
$ESXiHost = Read-Host "Enter Host Name or IP"
$Username = "root"
$Password = Read-Host "Enter root Password"

# vMotion Settings
$vMoIP = Read-Host "Enter vMotion IP"
$vMoIPSM = Read-Host "Enter vMotion IP Subnet Mask"

# iSCSI Settings
$iSCSI1 = Read-Host "Enter iSCSI_1 IP"
$iSCSI1SM = Read-Host "Enter iSCSI_1 IP Subnet Mask"
$iSCSI2 = Read-Host "Enter iSCSI_1 IP"
$iSCSI2SM = Read-Host "Enter iSCSI_1 IP Subnet Mask"
$iscsivmk2 = "vmk2"
$iscsivmk3 = "vmk3"

# iSCSI Storage Targets
$itarget1 = "10.0.0.1"
$itarget2 = "10.0.0.2"
$itarget3 = "10.0.0.3"
$itarget4 = "10.0.0.4"

# NFS Storage Settings
$NFSDSName1 = "NFS_ISO"
$NFSDSPath1 = "/VmISO"
$NFSDSHost1 = "10.0.0.10"

# Name for Local Datastore
$VMHostName = Read-Host "Enter the host name of the server (ESX01)"

# NTP Server Name
$ntp = "pool.ntp.org"

# Optional Configuration - Enable SSH and Suppress Warning
$sshenable = $true
$sshnowarn = $true

# Summary of Proposed Changes
""
$ctime = get-date -uformat "%H:%M:%S"
"$ctime........Collection of Information Complete"
""
"...About to configure the host $ESXiHost with the following settings..."
""
"01 - Connect to ESXi Host $ESXiHost with Username $Username and provided password"
"02 - Enters Maintenance Mode"
"03 - Configures NTP to point to $ntp"
"04 - Renames local datastore to HOST_local"
"05 - Add vmnic5 to vSwitch0"
"06 - Remove VM Network Portgroup from vSwitch0"
"07 - Add vMotion vmk1 port to vSwitch0 and assign $vMoIP and $vMoIPSM"
"08 - Set vmnic0 Active for Mgmt and Standby for vMotion"
"09 - Set vmnic5 Active for vMotion and Standby for Mgmt"
"10 - Create vSwitch1"
"11 - Add vmnic3 and vmnic7 to vSwitch1"
"12 - Create iSCSI_1 vmk2 with $iSCSI1 and $iSCSI1SM"
"13 - Create iSCSI_2 vmk3 with $iSCSI1 and $iSCSI1SM"
"14 - Create VLAN2_NFS on vSwitch1"
"15 - Set vmnic3 Active for iSCSI_1 and unused for iSCSI_2"
"16 - Set vmnic7 Active for iSCSI_2 and unused for iSCSI_1"
"17 - Create vSwitch2 and assign vmnic1, vmnic2, vmnic4, vmnic6"
"18 - Create VLAN1_LAN, VLAN4_LINUX, VLAN249_VDI and VLAN255_DMZ on vSwitch2"
"19 - Enables Software iSCSI"
"20 - Binds vmk2 and vmk3 to the Software iSCSI adapter"
"21 - Configures iSCSI Targets"
"22 - Configures NFS Storage"
"23 - Scans for Datastores"
"24 - Optionally enables SSH and removes warning"
"25 - Reboots the host - Prompts for Confirmation!!"

###################
# Connect to host #
###################

# Connects to the ESXi Host using the parameters retrieved above
Connect-VIServer -Server $ESXiHost -User $Username -Password $Password

$ctime = get-date -uformat "%H:%M:%S"
"$ctime........Entering Maintenance Mode"

# Puts the host into Maintenance Mode
Set-VMHost -State maintenance

$ctime = get-date -uformat "%H:%M:%S"
"$ctime........Configuring NTP Servers and Opening Ports"

# Configures NTP server setting, opens the firewall and enables the service
Add-VmHostNtpServer -NtpServer $ntp
Get-VMHostFirewallException | where {$_.Name -eq "NTP client"} | Set-VMHostFirewallException -Enabled:$true
Get-VMHostService | where {$_.Key -eq "ntpd"} | Set-VMHostService -Policy "on"

$ctime = get-date -uformat "%H:%M:%S"
"$ctime........Renaming Local Datastore"

# Renames the local datastore to HOSTNAME_local
$VMHostName = $VMHostName.ToUpper()
$LocalDSName = $VMHostName+"_local"
Get-DataStore | Where-Object { $_.Name -like "datastore*" } | %{
     $_ | Set-Datastore -Name ($_.Name -replace "datastore",$LocalDSName)
}

#####################################
# vSwitch0 - Management and vMotion #
#####################################

$ctime = get-date -uformat "%H:%M:%S"
"$ctime........Editing Virtual Switch 0"

# Adds vmnics to the default Virtual Switch (vSwitch0)
$vmnics = New-Object System.Collections.ArrayList
$vmnics.Add("vmnic0")
$vmnics.Add("vmnic5")
Set-VirtualSwitch -VirtualSwitch "vSwitch0" -Nic $vmnics -Confirm:$false

# Removes VM Network Portgroup
Get-VirtualPortGroup -Name "VM Network" | Remove-VirtualPortGroup -Confirm:$false

# Creates the Portgroup for vMotion and configures the vMotion vmkernel
New-VirtualPortGroup -Name "vMotion" -VirtualSwitch "vSwitch0" -VLANID 10
New-VMHostNetworkAdapter -vmhost (get-vmhost) -portgroup "vMotion" -virtualswitch "vSwitch0" -IP $vMoIP -Subnet $vMoIPSM -vMotionEnabled $true

# Renames the default Management Network vmkernel port to MGMT
Get-VirtualPortGroup -Name "Management Network" | Set-VirtualPortGroup -Name "MGMT"

# Configures the MGMT portgroup to use the first vmnic and standby's the second
Get-VirtualPortGroup -Name "MGMT" | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive "vmnic0" -MakeNicStandby "vmnic5"

# Configures the vMotion portgroup to use the second vmnic and standby's the first
Get-VirtualPortGroup -Name "vMotion" | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive "vmnic5" -MakeNicStandby "vmnic0"

#############################
# Create vSwitch1 - Storage #
#############################

$ctime = get-date -uformat "%H:%M:%S"
"$ctime........Creating Virtual Switch 1"

# Creates vSwitch1
New-VirtualSwitch -Name "vSwitch1" -VMHost (Get-VMHost) -NumPorts "64"

# Adds vmnics to vSwitch1
$vmnics = New-Object System.Collections.ArrayList
$vmnics.Add("vmnic3")
$vmnics.Add("vmnic7")
Set-VirtualSwitch -VirtualSwitch "vSwitch1" -Nic $vmnics -Confirm:$false

# Creates Virtual Machine Portgroup for Storage Network Access
New-VirtualPortGroup -Name "VLAN2_STORAGE" -VirtualSwitch "vSwitch1"

# Creates the first iSCSI vmkernel Portgroup and configures the vmkernel port
New-VirtualPortGroup -Name "iSCSI_1" -VirtualSwitch "vSwitch1"
New-VMHostNetworkAdapter -vmhost (get-vmhost) -portgroup "iSCSI_1" -virtualswitch "vSwitch1" -IP $iSCSI1 -Subnet $iSCSI1SM

# Creates the second iSCSI vmkernel Portgroup and configures the vmkernel port
New-VirtualPortGroup -Name "iSCSI_2" -VirtualSwitch "vSwitch1"
New-VMHostNetworkAdapter -vmhost (get-vmhost) -portgroup "iSCSI_2" -virtualswitch "vSwitch1" -IP $iSCSI2 -Subnet $iSCSI2SM

# Configures the first iSCSI vmkernel to use the first vmnic and unuses the second
Get-VirtualPortGroup -Name "iSCSI_1" | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive "vmnic3" -MakeNicUnused "vmnic7"

# Configures the second iSCSI vmkernel to use the second vmnic and unuses the first
Get-VirtualPortGroup -Name "iSCSI_2" | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive "vmnic7" -MakeNicUnused "vmnic3"

################################
# vSwitch2 - VM Networks #
################################

$ctime = get-date -uformat "%H:%M:%S"
"$ctime........Creating Virtual Switch 2"

# Creates vSwitch2
New-VirtualSwitch -Name "vSwitch2" -VMHost (Get-VMHost) -NumPorts "64"

# Adds vmnics to vSwitch2
$vmnics = New-Object System.Collections.ArrayList
$vmnics.Add("vmnic1")
$vmnics.Add("vmnic2")
$vmnics.Add("vmnic4")
$vmnics.Add("vmnic6")
Set-VirtualSwitch -VirtualSwitch "vSwitch2" -Nic $vmnics -Confirm:$false

# Adds the Virtual Machine Portgroups to vSwitch2
New-VirtualPortGroup -Name "VLAN1_LAN" -VirtualSwitch "vSwitch2"
New-VirtualPortGroup -Name "VLAN4_LINUX" -VirtualSwitch "vSwitch2" -VLANID 4
New-VirtualPortGroup -Name "VLAN249_VDI" -VirtualSwitch "vSwitch2" -VLANID 249
New-VirtualPortGroup -Name "VLAN255_DMZ" -VirtualSwitch "vSwitch2" -VLANID 255

#############################
# iSCSI / NFS Configuration #
#############################

$ctime = get-date -uformat "%H:%M:%S"
"$ctime........Enabling Software iSCSI Adapter"

# Enable the Software iSCSI Initiator
Get-VMHostStorage -VMHost $HostName | Set-VMHostStorage -SoftwareIScsiEnabled $True
 
# Retrieves HBA number for the iSCSI Software Adapter
$HBANumber = Get-VMHostHba -VMHost $HostName -Type iSCSI | %{$_.Device}

# Gets an ESXCli Session
$ESXCli = get-esxcli

$ctime = get-date -uformat "%H:%M:%S"
"$ctime........Binding vmkernel ports to iSCSI Adapter"

# Binds VMKernel ports to the iSCSI Software Adapter HBA

$EsxCli.iscsi.networkportal.add($HBANumber, $null, $iscsivmk2)
$EsxCli.iscsi.networkportal.add($HBANumber, $null, $iscsivmk3)

$ctime = get-date -uformat "%H:%M:%S"
"$ctime........Configuring iSCSI Targets"
 
# Configure iSCSI Targets
Get-VMHostHba -VMHost $HostName -Type iScsi | New-IScsiHbaTarget -Address $iTarget1,$iTarget2,$iTarget3,$iTarget4

$ctime = get-date -uformat "%H:%M:%S"
"$ctime........Configuring NFS Datastores"

# Mount NFS Datastore (ISO Location)
Get-VMHost | New-Datastore -Nfs -NAME $NFSDSName1 -Path $NFSDSPath1 -NfsHost $NFSDSHost1

$ctime = get-date -uformat "%H:%M:%S"
"$ctime........Rescanning HBAs"
 
# Rescan on all HBAs and for VMFS volumes
Get-VMHostStorage -VMHost $HostName -RescanAllHba
Get-VMHostStorage -VMHost $HostName -RescanVmfs

# Set Multipath policy on all datastores to RoundRobin
Get-VMHost | Get-ScsiLun -LunType disk | Where-Object {$_.MultipathPolicy -ne “RoundRobin”} | Set-ScsiLun -MultipathPolicy “RoundRobin”

############
# Optional #
############

If($sshenable -eq $true)
{
    $ctime = get-date -uformat "%H:%M:%S"
    "$ctime........Setting Optional Config - Enable SSH"

    Get-VMHost | Foreach {
    Start-VMHostService -HostService ($_ | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"} )
    }
}
If($sshnowarn -eq $true)
{
    $ctime = get-date -uformat "%H:%M:%S"
    "$ctime........Setting Optional Config - Suppress Shell Warning for SSH"
    Set-VMHostAdvancedConfiguration -VMHost * -Name UserVars.SuppressShellWarning -Value 1
}

###########
# Cleanup #
###########

$ctime = get-date -uformat "%H:%M:%S"
"$ctime........Restarting Host"
""
Restart-VMHost -server $vmhost -confirm:$true

#################
# End of Script #
#################

Comments - ESXi 5 - PowerShell Host Setup Script

No comments found.

Search site