2010-04-07

Deploy-ESXi v1.0 - My ESXi Script-0-Mania entry

As I posted a few weeks ago - I entered the ESXi Script-0-Mania Contest.Unfortunately, my entry was not chosen as one of the top entries, but hey - I don't do this for the money - I enjoy what I do - I feel that the contribution back to the virtualization community is the least I can do - seeing the amount of info and help that I receive from you all.

So - Deploy-ESXi.ps1 v1.0 - My entry. As you can deduct from the script name this script does what it says.

The need for the script? I find that I am deploying more and more systems with ESXi - be it the free version - or a fully-licensed system. Now of course to install ESXi is really, really simple!
I mean F11 -> Enter -> Enter … and Bob's your uncle - or you have an ESXi server deployed - that's it. But then you have the mundane tasks of configuring the installation according to your requirements. Removing Default port groups, changing the Management IP, set NTP settings. I guess you understand what I am talking about. Now of course all of this can be scripted with a Kickstart script - but guess what ?? No Kickstart script for ESXi!! So either you have to do this manually - or if you some of the Enterprise customers - you can utilize Host Profiles to do all of this for you.

Or you can use this script as a base for your environment.

You might say that this will not work with the free version of ESXi because the API is read-only in this version. Well that is true - but by default the new installation is deployed with a fully functional evaluation license which makes the API read-write and allows you to make the changes you need

The script is commented within.

############################################################################
##	ESXi Deployment script                                     			
## 	Author: 	Maish Saidel-Keesing                        				
##  	       	http://technodrone.blogspot.com			
##	Date:	March 15, 2010				
##	Synopsis:	This script will configure an ESXi server			
##			that has been installed with several basic settings			
############################################################################
#
#When an ESXi machine is installed there are basic default settings that we will define
#
#1. Connect to host with default credentials (root,<empty>)
#2. Remove Default VM Network Portgoup
#3. Add VM Portgroup named Virtual Machines and raise the number of port on the virtual Switch
#4. Set NTP Servers
#5. Adding a new root user 
#6. Change Management IP and DNS
#7. Backup configuration to a network share
#8. Change Default password
#9. Reboot the Host after all the changes
#10. Send email to admin of installation particulars

#Set Default variables
$defaultuser = "root"
$defaultpwd = ""
$esxi = Read-Host Please enter the IP of the ESXi server


#connect to ESXi
Write-Host -ForegroundColor Green Connecting to ESXi server
Connect-VIServer $esxi -User $defaultuser -Password $defaultpwd


##2. Remove Default VM Network Portgoup
Write-Host -ForegroundColor Green Remove Default VM Network Portgoup
Get-VirtualPortGroup -Name "VM Network" | Remove-VirtualPortGroup -Confirm:$false

##3. Add VM Portgroup named Virtual Machines and raise the number of port on the virtual Switch
Write-Host -ForegroundColor Green Changing Portgroup and Default vSwitch settings
Get-VirtualSwitch -name vSwitch0 | New-VirtualPortGroup -Name "Virtual Machines" -Confirm:$false
Get-VirtualSwitch -Name vSwitch0 | Set-VirtualSwitch -NumPorts 120 -Confirm:$false

##4. Set NTP Servers
Write-Host -ForegroundColor Green NTP Settings
Add-VmHostNtpServer -NtpServer "pool.ntp.org" -Confirm:$false

##5. Adding a new root user 
Write-Host -ForegroundColor Green Adding new root user
New-VMHostAccount -ID User1 -Password "Qwer$#@1" -UserAccount:$true
Set-VMHostAccount -GroupAccount root -AssignUsers User1
Set-VMHostAccount -GroupAccount localadmin -AssignUsers User1
Set-VMHostAccount -UserAccount User1 -UnassignGroups users

##6. Change Management IP and DNS
Write-Host -ForegroundColor Green Changing Mgmt IP and settings
$mgmtip = Read-Host Please Enter the Management IP address
$mgmsm = Read-Host Please Enter the Management Subnet Mask
$hostname = Read-Host Please Enter the ESXi Hostname
$domainname = Read-Host Please Enter the ESXi Domain Name
$dns1 = Read-Host Please Enter the DNS Server IP

Get-VMHostNetworkAdapter | Where-Object {$_.PortGroupName -eq "Management Network" } | `
	Set-VMHostNetworkAdapter -IP $mgmtip -SubnetMask $mgmsm 
Get-VMHostNetwork | Set-VMHostNetwork -HostName $hostname -DomainName $domainname -DnsFromDhcp:$false -DnsAddress $dns1

##7. Backup configuration to a network share
Write-Host -ForegroundColor Green Backing up configuration
$share = Read-Host Please enter the network share you would like to save the configuration to
Set-VMHostFirmware -BackupConfiguration -DestinationPath $share

##8. Change Default password
Write-Host -ForegroundColor Green Changing Default Password
$newpasswd = "Qwer$#@!"
Set-VMHostAccount root -Password $newpasswd

##9. Reboot the Host after all the changes
Write-Host -ForegroundColor Green Rebooting Host

Set-VMHost -state "Maintenance"
Restart-VMHost -Force:$true -Confirm:$false
sleep 60

##10. Send email to admin of installation particulars

#Wait for the host to come up
Connect-VIServer -Server $mgmtip -User root -Password $newpasswd
	while ($? -ne $true ) {
		sleep 30; write-host -ForegroundColor Red Still Waiting for Host to come back up; Connect-VIServer -Server $mgmtip -User root -Password $newpasswd
	}

Write-Host -ForegroundColor Green Host is available
$body = @"
Management IP address: $mgmtip
Management Subnet Mask: $mgmsm
ESXi Hostname: $hostname 
ESXi Domain Name: $domainname 
DNS Server IP: $dns1 
"@

Send-mailmessage -From "esxideploy@maishsk.local" -To "maish@maishsk.local" -Subject "New ESXi Server installed" -bodyasHTML $body -SmtpServer "smtp.maishsk.local"


Annotations:

23-26. Setting the default variables. All ESXi servers are set with a blank password on first configuration.

47-52. Here I created a another Admin User on the ESXi host - an additional Admin account for troubleshooting - if needed.

66=69. After configuration is completed - configuration is backed up to share - in case it is needed for restore.

71-74. We of course do not want to leave the default blank password

86-89. Check that the server has come up

92-100. Send Email to Administrator with new host details.

Things that will be added in future versions:

  • Logging to file / database
  • Checks after connections - and timeouts
  • Other improvements

I have plans for this script - and the development further so stay tuned for this spot

You can download the script below

Deploy-ESXi v1.0

A demo of how the script works below