# SAMPLE - Change variables for your Site
#
# VMware PowerCLI Script to configure a VMware ESXi Host
# Designed for VMware ESXi on HP 460c G6 Blades
# But should work for most hosts with some variable changes
# Will work with an IP address but the storage will be renamed to XX.XX.XX.X-LOCAL instead of HOSTNAME-LOCAL
# Host should be deployed from ISO
# Assumes root password and IP Details configured (IP, DNS and search suffix)
# At that point when the host is on the network this script can be run
# Once this script is complete, add the host to vCenter and reboot the host and
# then validate the host is configured correctly.
#
# John Gibson - March 2010
# version 1.0 - Initial script creation
# version 1.1 - Updated script to set DNS Settings on Hosts
# version 1.2 - Updated script and structure of script
#
# Set Variables to be used in the ESXi Host configuration
#
# Modify these settings for regional requirements
#
# ESXi Host Patch repository where the offline bundles have been extracted to
$patchURL = "http://servername.fqdn"
# NTP Time Servers to use
$ntp1 = "your.NTP.Server1.fqdn"
$ntp2 = "your.NTP.Server1.fqdn"
# DNS Search Details to use
$DomainName = "host.dns.doman.name"
$DNSSearch = "host.dns.search.name"
$PreferredDNS = "x.x.x.x"
$AltDNS = "x.x.x.x"
# These NICs are connected to the management network vSwitch0
$esxnics = "vmnic0","vmnic1"
# This/these NICs are connected to the VM network vSwitch1
$vmnics = "vmnic2","vmnic3"
# VMotion Subnet Mask
$VMotionSubnet = "255.255.255.0"
# Local Accounts to be created with VMware Admin Rights
# Note: Complex passwords are needed or ESXi will reject them
$vmadminaccount = "vmadmin"
$vmadminpassword = "C0mp1exPassw0rd"
$vmopsaccount = "vmops"
$vmopspassword = "C0mp1exPassw0rd"
$eccuserpassword = "C0mp1exPassw0rd"
Write-Host "ESXi Configuration script for VMware ESXi Hosts for HP Servers"
# Capture unique variables for the ESXi Host by user input
$vmhost=(Read-Host "Enter just the name of the ESXi Host (e.g. hostname)").ToLower()
$HostPassword=Read-Host "Enter the password to the root account on the ESXi Host"
$VMotionIP=Read-Host "Enter the VMotion IP address for this ESX Host (x.x.x.x)"
$vSwitch1Name=Read-Host "Enter the name of the Virtual Machine network (e.g. 192.168.10.1 Subnet)"
$vSwitch1VLAN=Read-Host "Enter the number of the Virtual Machine VLAN (0 if not trunked)"
if ("0" -ne $vSwitch1VLAN){
$vSwitch1Name2=Read-Host "Enter the name of the Second Virtual Machine network (Press 0 to skip)"
if ("0" -ne $vSwitch1Name2){
$vSwitch1VLAN2=Read-Host "Enter the number of the Second Virtual Machine VLAN"
}
}
# Authenticate to ESX Host...
write-host "Connecting to " $vmhost
$esxhost = Connect-VIServer $vmhost -User root -Password $HostPassword
# First puts the ESX host into maintenance mode...
write-host "Entering Maintenance Mode"
Set-VMHost -State maintenance
# Configure vSwitch0
write-host "Configuring vSwitch0"
$vs0 = Get-VirtualSwitch -Name vSwitch0
Set-VirtualSwitch -VirtualSwitch $vs0 -Nic $esxnics
New-VMHostNetworkAdapter -PortGroup VMkernel -VirtualSwitch $vs0 -IP $VMotionIP -SubnetMask $VMotionSubnet -VMotionEnabled: $true
# Removes "VM Network" from the vSwitch0
get
-VirtualPortGroup
| where { $_.Name
-like "VM Network"} | Remove
-VirtualPortGroup
-Confirm:
$false
# Configure vSwitch1
write-host "Configuring vSwitch1"
$vs1 = New-VirtualSwitch -Name vSwitch1 -nic $vmnics
write-host "Configuring " $vSwitch1Name
New-VirtualPortGroup -VirtualSwitch $vs1 -Name $vSwitch1Name -VLanId $vSwitch1VLAN
if ("0" -ne $vSwitch1VLAN){
if ("0" -ne $vSwitch1Name2){
write-host "Configuring " $vSwitch1Name2
New-VirtualPortGroup -VirtualSwitch $vs1 -Name $vSwitch1Name2 -VLanId $vSwitch1VLAN2
}
}
# Configure vSwitch Security for all vSwitches
write-host "Configuring vSwitch Security settings and enabling Beacon Probing for all vSwitches"
foreach ($vswitchName in Get-VirtualSwitch $vmhost){
$hostview = get-vmhost $vmhost | Get-View
$ns = Get-View -Id $hostview.ConfigManager.NetworkSystem
$vsConfig = $hostview.Config.Network.Vswitch
| Where-Object { $_.Name
-eq $vswitchName } $vsSpec = $vsConfig.Spec
$vsSpec.Policy.NicTeaming.FailureCriteria.checkBeacon = $true
$vsSpec.Policy.Security.AllowPromiscuous = $False
$vsSpec.Policy.Security.forgedTransmits = $False
$vsSpec.Policy.Security.macChanges = $False
$ns.UpdateVirtualSwitch( $VSwitchName, $vsSpec)
}
# Set-up the NTP Configuration
write-host "Adding NTP Servers"
Add-VmHostNtpServer -NtpServer $ntp1,$ntp2
# Set DNS Details to ensure they have been set
write-host "Resetting DNS Details"
$vmHostNetworkInfo = Get-VmHostNetwork -VMHost $vmhost
Set-VmHostNetwork -Network $vmHostNetworkInfo -DomainName $DomainName -SearchDomain $DNSSearch
Set-VmHostNetwork -Network $vmHostNetworkInfo -DnsAddress $PreferredDNS, $AltDNS
# Rename Local Datastore
$LocalName = $vmhost.ToUpper()
Get-Datastore -Name "datastore1" | Set-Datastore -Name $LocalName"-LOCAL"
# Create Local Accounts
write-host "Configuring local ESXi account for " $vmadminaccount
New-VMHostAccount -Group $vmadminaccount
New-VMHostAccount -User -Id $vmadminaccount -Password $vmadminpassword -Description "VMware Local ESXi Administrator" -AssignGroups $vmadminaccount
$sgAuthMgr = Get-View (Get-View ServiceInstance).Content.AuthorizationManager
$sgEntity = Get-Folder ha-folder-root | Get-View
$sgPerm = New-Object VMware.Vim.Permission
$sgPerm.entity = $sgEntity.MoRef
$sgPerm.group = $true
$sgPerm.principal = $vmadminaccount
$sgPerm.propagate = $true
$sgPerm.roleId
= ($sgAuthMgr.RoleList
| where {$_.Name
-eq "Admin"}).RoleId
$sgAuthMgr.SetEntityPermissions($sgEntity.MoRef,$sgPerm)
write-host "Configuring local ESXi account for " $vmopsaccount
New-VMHostAccount -Group $vmopsaccount
New-VMHostAccount -User -Id $vmopsaccount -Password $vmopspassword -Description "VMware Local ESXi Operations Administrator" -AssignGroups $vmopsaccount
$sgAuthMgr = Get-View (Get-View ServiceInstance).Content.AuthorizationManager
$sgEntity = Get-Folder ha-folder-root | Get-View
$sgPerm = New-Object VMware.Vim.Permission
$sgPerm.entity = $sgEntity.MoRef
$sgPerm.group = $true
$sgPerm.principal = $vmopsaccount
$sgPerm.propagate = $true
$sgPerm.roleId
= ($sgAuthMgr.RoleList
| where {$_.Name
-eq "Admin"}).RoleId
$sgAuthMgr.SetEntityPermissions($sgEntity.MoRef,$sgPerm)
# Create Account for EMC ECC Control Centre
write-host "Configuring account for EMC ECC ControlCenter"
$sgRole = New-VIRole -Name ControlCenter -Privilege ( Get-VIPrivilege -PrivilegeItem "Browse datastore" )
New-VMHostAccount -Group ControlCenter
New-VMHostAccount -User -Id eccuser -Password $eccuserpassword -Description "EMC ControlCenter discovery" -AssignGroups "ControlCenter"
$sgAuthMgr = Get-View (Get-View ServiceInstance).Content.AuthorizationManager
$sgEntity = Get-Folder ha-folder-root | Get-View
$sgPerm = New-Object VMware.Vim.Permission
$sgPerm.entity = $sgEntity.MoRef
$sgPerm.group = $true
$sgPerm.principal = "ControlCenter"
$sgPerm.propagate = $true
$sgPerm.roleId
= ($sgAuthMgr.RoleList
| where {$_.Name
-eq "ControlCenter"}).RoleId
$sgAuthMgr.SetEntityPermissions($sgEntity.MoRef,$sgPerm)
# Patch ESXi Host
write-host "Patching ESXi Host"
Install-VMHostPatch -VMhost $vmhost -HostUsername root -HostPassword $HostPassword -WebPath $patchURL/patch/ESXi400-201002001/metadata.zip
Install-VMHostPatch -VMhost $vmhost -HostUsername root -HostPassword $HostPassword -WebPath $patchURL/patch/BCM-bnx2x-1.52.12.v40.3-offline_bundle-223054/metadata.zip
Install-VMHostPatch -VMhost $vmhost -HostUsername root -HostPassword $HostPassword -WebPath $patchURL/patch/hp-esxi4.0uX-bundle-1.2/metadata.zip
# Restart the ESXi Host
write-host "Rebooting ESXi Host"
Restart-VMHost -server $vmhost -confirm:$false
# Disconnect from ESXi Host
Disconnect-VIServer -Confirm:$False
# Provide Post config Instructions
write-host "The basic ESXi Host configuration is completed, please:"
write-host "1. Wait for the host to reboot."
write-host "2. Connect the Host to vCenter and assign a license"
write-host "3. Verify the Host Configuration is correct"
write-host "4. Confirm all patches have been applied (scan for updates)"
Write-Host "5. Once complete take the ESXi host out of Maintenance Mode"