A. Mikkelsen

VMware ESX scripts, commands, tools and other nice to know things that will make your virtualization days easier!!!!

Last year I was asked to reduce the time spend on installing and configuring our ESX hosts.  Because we weren’t using Enterprise Plus licenses, we didn’t have Host Profiles.
I came up with a simple two-step process based on the EDA appliance and a custom PowerShell script.

  • Install the host from PXE.
    Only setting the minimum configuration, so it’s as versatile as possible.

    • Disk layout
    • Network teaming
    • FQDN and IP
  • Configure the host using a custom PowerShell script.
    Based on the Datacenter and Cluster the host is to be added to.

    • Add to vCenter
    • Set COS memory
    • Enable VMotion
    • License host
    • And much more…

A lot of blog posts are available on the net, on how to setup and install an ESX host using the EDA appliance so I won’t trouble you with this.

The PowerShell script is divided into several sections, I won’t explain everyone, only the most relevant, the rest is documented in the full script.
All steps in the script are logged to a host specific log file.

  • User input – Only 3 things are asked for when running the script, the rest is stored in the scripts Static section.
    • FQDN of the host (must be configured in DNS to work)
    • The environment – what environment is the host to be placed in, like PROD, DMZ, etc.
    • The hosts VMotion IP.
  • Add the host to the right cluster.
    You must have get the Datacenter/Cluster variable prior to adding the host, else it will just be placed in the first datacenter (See script for more info).

     add-vmhost $strHost -location $strvCenterDatacenter -user $strHostUser -password $strHostUserPWD -force: $true
     
  • Set the host in maintenance mode
     Get-VMHost -Name $strHost | Set-VMHost -State maintenance
     
  • License host.
    As you can see below, you have to supply the full name of the license you are adding, not just the license key.

     $targethostMoRef = (get-VMHost $strHost | get-view).MoRef
     $si = Get-View ServiceInstance
     $LicManRef=$si.Content.LicenseManager
     $LicManView=Get-View $LicManRef
     $licassman = (Get-View $LicManView.LicenseAssignmentManager)
    
     #$licassman.UpdateAssignedLicense($targethostMoRef.value,”YOUR LIC KEY”,”vSphere4 Enterprise Plus (1-12 cores per CPU”)
     $licassman.UpdateAssignedLicense($targethostMoRef.value,”YOUR LIC KEY”,”vSphere4 Enterprise (1-6 cores per CPU”)
     
  • Set the correct time zone.
     $strTimeZone = "Europe/Copenhagen"
     $tmpHost = Get-VMHost $strHost | get-view
     $tmpDTSystem =  $tmpHost.ConfigManager.DateTimeSystem
     $tmpMoRef = Get-View  $tmpDTSystem
     $tmpDateConfig = New-Object      Vmware.Vim.HostDateTimeConfig
     $tmpDateConfig.timeZone = $strTimeZone
     $tmpMoRef.updateDateTimeConfig($tmpDateConfig)
     
  • Add NTP servers.
    Use an array of NTP servers like
    $arrNTPServer = @(“dk.pool.ntp.org”,”de.pool.ntp.org”,”us.pool.ntp.org”,”clock.cimat.ues.edu.sv”,”ntp1.gbg.netnod.se”,”ntp1.theremailer.net”)

     Add-VMHostNtpServer -VMHost $strHost -NtpServer $arrNTPServer -Confirm:$false
     

    If you want you can restart the NTP service – only do it if you don’t plan on restarting the host after configuring.

     Restart-VMHostService $ntpd -Confirm:$false
     
  • Open firewall rules
    I always open the following firewall rules so that NTP and the SSH is working

     Get-VmhostFirewallException -VMHost $strHost -Name "NTP Client" | Set-VMHostFirewallException -enabled:$true
     Get-VmhostFirewallException -VMHost $strHost -Name "SSH Client" | Set-VMHostFirewallException -enabled:$true
     [/powershel]</li>
    	<li>Set the correct DNS servers
     Use an array of DNS servers, this way you can add multiple DNS servers at      once.
     @("10.10.10.40","10.10.10.30") or @("10.10.10.40")
     
     $strHost = @("10.10.10.40","10.10.10.30")
     Get-VMHost -Name $strHost | Get-View | %{$tmpNS = Get-View -Id $_.configManager.networkSystem
     $tmpDNS = $tmpNS.NetworkConfig.DnsConfig
     $tmpDNS.domainName = $strHostDomain
     $tmpDNS.address = $arrDNSsrv
     $tmpDNS.searchDomain = $strSearchDomain
     $tmpNS.updateDnsConfig($tmpDNS)}
     
  • Configuring network, adding network including VMotion.
    See the script for the full script.

     $vSwitch = Get-VirtualSwitch $strHost -Name vSwitch0
     #Set-VirtualSwitch -VirtualSwitch $vSwitch -Nic vmnic1
     New-VirtualPortGroup -VirtualSwitch $vSwitch -Name "VMOTION" -VLanId 0
     New-VMHostNetworkAdapter -VMHost $strHost -PortGroup "VMOTION" -VirtualSwitch $vSwitch -IP $strVMotion -SubnetMask $strSubnetMask -VMotionEnabled $true
     $strNetConfig = Get-View (Get-VMHost $strHost | Get-View).ConfigManager.NetworkSystem
     $strIPRoute = New-Object VMware.Vim.HostIpRouteConfig
     $strIPRoute.defaultGateway = $strGateway
     $strNetConfig.UpdateIpRouteConfig($strIPRoute)
     New-VirtualPortGroup -VirtualSwitch $vSwitch -Name "PROD" -VLanId 200
     New-VirtualPortGroup -VirtualSwitch $vSwitch -Name "PXE" -VLanId 0
     

    If needed you can remove the default VM network

     $vSwitch = Get-VirtualSwitch $strHost -Name vSwitch0
     $vmnetwork = Get-VirtualPortGroup -VirtualSwitch $vSwitch -Name "VM Network"
     Remove-VirtualPortGroup -VirtualPortGroup $vmnetwork -Confirm:$false}
     
  • Reboot the host J
     Get-VMHost -Name $strHost | %{Get-View $_.ID} | %{$_.RebootHost_Task($TRUE)}
     
  • If you want your script to wait for the host to be rebooted before continuing, you can do it with these two loops.
     do{
     $result = Get-VMHost $strHost
     Start-Sleep 10 # Wait 10 sec
     }
     Until($result.State -ne "Maintenance")
     # Maintenance - because host entered Maintenence mode earlier
    
     do{
     $result =  Get-VMHost $strHost
     Start-Sleep 10 # Wait 10 sec
     }
     Until($result.State -eq "Maintenance")
     
  • Finally exit the host from maintenance mode.
     Get-VMHost -Name $strHost | Set-VMHost -State connected
     

If everything is configured correctly, DRS should start migrating VM’s to the newly added host.

This script helped reducing the time spend on installing and configuring a host from 90 minutes to about 18 minutes. That’s a reduction of more than 500%, time you can use on other cool PowerShell tasks. J

By using a script to customize our hosts, we also gained two other benefits.

  1. Compliance – a host is always installed and configured the exact same way each time.
  2. Each step is documented in a host specific log file.

The full script can be downloaded here.



Some time ago I was asked to create a script that could list each VM’s videocard settings.

$VMs = Get-View -ViewType VirtualMachine | sort Name 
foreach($VM in $VMs){
	write "$($VM.Name) $($VM.Guest.Screen.Width)/$($VM.Guest.Screen.Height)"
}

As you could see that was quite simple.
I using the Get-View -ViewType to speed up the query – See LucD’s post on http://communities.vmware.com/message/1511671#1511671 for more info.

Then I was asked to set all VM’s videocard setting to Auto Detect (Default setting).

$vms = Get-View -ViewType VirtualMachine | sort Name
foreach ($vm in $vms){
	if ($vm.Runtime.PowerState -eq "PoweredOff") {
		if ($vm.Config.Version -eq "vmx-07"){ 
			foreach($dev in $vm.Config.Hardware.Device){
				if ($dev.DeviceInfo.Label -like "Video*"){
					if ($dev.UseAutoDetect -match "False" ) {
						$VMName = $vm.Name
						$VMcidcrd = New-Object VMware.Vim.VirtualMachineVideoCard 
						$VMcidcrd.UseAutoDetect = 1
						$VMcidcrd.Key = 500
						$spec = new-object VMware.Vim.VirtualDeviceConfigSpec
						$spec.Device = $VMcidcrd
						$spec.Operation = "edit"
						$VMSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
						$VMSpec.DeviceChange = $spec
						$vm.ReconfigVM_Task($VMSpec)
						write "$($vm.Name) - Video Settings Changed"
						
					}
				}
			}
		}
	}
}

As you can see this requires a bit more.
The VM has to be
• Powered Off
• Running vHW version 7
Download the full script here.

If you want to set the videocard setting to a specific MB, then just change the line from the above script

$VMcidcrd.UseAutoDetect = 0

To

$VMcidcrd.UseAutoDetect = 0

And add the line

$VMcidcrd.VideoRamSizeInKB = 131072

Where the 131072 us the amount of MB you want multiplied with 1024
Ex. 128 x 1024 = 131072

September 15, 2010 VMware released version 2.0 of its Project Onya Alpha.

You can download and read more about Project Onyx on the projects home page.
http://communities.vmware.com/community/vmtn/vsphere/automationtools/onyx?view=overview

This project is a must have for all PowerCLI geeks 🙂

When you upgrade your vSphere environment you normally also upgrade the VM’s virtual hardware to version 7, to take advantage of the new features. This is pretty normal procedure for all VMware admins.

But in some very very rare cases you might need to move a VM upgraded to hardware version 7, to a host that doesn’t support VM’s running hardware version 7.
From a host running ESX 4.x to a host running ESX 3.x

So what to do.
There is two ways you can accomplice this task.

The first way is to use the free VMware Converter tool.
Some great guides have been created by others so I don’t want to do it all over again.
The only thing is that it can take some time to convert the VM, but it is a proven and stable method.
http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1028019
Or
http://www.techhead.co.uk/vmware-esx-how-to-downgrade-a-vms-vm-versionhw-level-from-7-4-0-to-4-3-x
Or
http://blog.vmpros.nl/2010/04/09/vmware-how-to-downgrade-vm-hardware-level-7-to-4/

The other way is to do it manually, this way is a lot faster, but there is a risk that it will corrupt the VM, so make sure you have a working backup.
Use this guide on your own risk

  • Powered off the VM
  • Make sure the VM doesn’t have any snapshots before proceeding
  • From the ESX console or from a Putty session, edit the VMs VMX file, using your favorite editor
    vi /vmfs/volume/DS1/WIN2008-001/WIN2008-001.vmx
  • Change the virtual hardware version from:
    virtualHW.version = “7”

    To

    virtualHW.version = “4”
  • You don’t need to change config.version = “8”, since ESX 3.x already uses this version
  • Change the virtual controller, because virtual hardware version 4 doesn’t understand the version 7 virtual controller, from:
    scsi0.virtualDev = “lsisas1068”

    To

    scsi0.virtualDev = “lsilogic”
  • From the ESX console or from a Putty session, edit the VMs VMDK pointer file/files (if more than one virtual disk), using your favorite editor
    vi /vmfs/volume/DS1/WIN2008-001/WIN2008-001.vmdk
  • Change the virtual hardware version from:
    ddb.virtualHWVersion = "7"

    To

    ddb.virtualHWVersion = "4"
  • You should now be able to power on the VM as virtual hardware version 4.

Using IBM Blades and Cisco switches to run your ESX enviroment?

If yes, have you tested what happens if you unplug the network cables going into one switch?

If you like me have bundled 2 or more cables going from one switch, to one backbone switch and done the same for the other switch, then your VM’s using that switch will loose network connection (from outside the host).
This is not the way I wanted the setup to work.

After a bit of googling i found a blog from Scott Lowe (http://blog.scottlowe.org/2007/06/22/link-state-tracking-in-blade-deployments/) about the problem and also a solution.
The solution is called Link State Tracking. Many users have tried the solution and have got it to work, so I had to test it…..

I added the following lines to each of the Blade Switches (Port-Channel, group and interfaces may be different on your system).

----------UPLINK to CORE switch------------
interface Port-Channel1
link state group 1 upstream

----------LINK to Blade server------------
interface range GigabitEthernet0/1 - 14
link state group 1 downstream

----------Global command------------
link state track 1

conf t
interface Port-Channel1
link state group 1 upstream
interface range GigabitEthernet0/1 - 14
link state group 1 downstream
link state track 1

Remember to write the changes to memory using

wri

After this was done on both Blade switches, i just had to test it.
I started a ping to a VM that I knew was using Switch1 to communicate with external network traffic.
Then I unplugged the to 2 network cables going into Switch1 and waited to see if the ping command would loose the communication with the VM….
It didn’t loose connection. So the the VM must have switched to Switch2.

So configuring the Blade Switches for Link State Tracking is to proper way to configure the switches.
A big thanks goes to Scott Lowe for the blog on Link State Tracking.

After we upgraded to vSphere 4.1, the SCSI adaptor of all our WINXP guests changed from LSI Logic Parallel  to BusLogic Parallel. This change should normally not create any warnings or problems it VMware Tools are up to date.

But after the upgrade all our WINXP guests got the following warning when powered on in the event log.

Message from esxhost01.labt.local: The guest operating system is Windows XP and you have one or more virtual SCSI devices installed in your virtual machine. Windows XP does not support the BusLogic SCSI adapter that VMware ESX currently uses for its virtual SCSI devices. Select OK to continue or Cancel to cancel. info 22-09-2010 15:55:25 VM name vpxuser
I then changed the SCSI adaptor back to LSI Logic Parallel and I now got almost the same warning.

Message from esxhost01.labt.local: The guest operating system is Windows XP and you have one or more virtual SCSI devices installed in your virtual machine. Windows XP does not support the LSI Logic SCSI adapter that VMware ESX currently uses for its virtual SCSI devices. Select OK to continue or Cancel to cancel. info 22-09-2010 15:55:25 VM name vpxuser

I contacted VMware support and they told me that it was a known “feature”/”bug” and send me the following link to suppress the waring in the Event log.
http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1007122

There is two ways to suppress the warning.

Through vSphere Client

  • Using the vSpher Client logon to your vCenter server.
  • Poweroff the VM.
  • Edit the VM’s settings
  • Navigate to Options > Advanced > General
  • Click Configuration Parameters
  • Add the following rows, by using Add Row
    • If you have a BusLogic Parallel SCSI adaptor
      buslogic.noDriver = "FALSE"
    • If you have a LSI Logic Parallel SCSI adaptor
    • lsilogic.noDriver = "FALSE"
  • Click OK twice to close the dialogs and save the changes.
  • PowerOn the VM

Editing the VMX file

  • Open a SSH to the host ex. using Putty
  • Navigate to the VM’s files (relpace with your own path)
    cd /vmfs/volumes/lun01/winxp01/
  • Open the config file  in your favorit editor .
    vi winxp01.vmx
  • Add the following line to the file
    • If you have a BusLogic Parallel SCSI adaptor
    • buslogic.noDriver = "FALSE"
    • If you have a LSI Logic Parallel SCSI adaptor
    • lsilogic.noDriver = "FALSE"
  • Save and close the config file.
  • PowerOn the VM

I haven’t tested if it possible to add the lines to the global config file on each host (/etc/vmware/config) for all VM’s..

I will test this next week.

After we upgraded to vSphere 4.1 we have been expirence problems with VM’s entering FullScreen mode.

The issue is that 9/10 times a VM enter FullScreen mode, you are not able to move the mouse to the bottom part of the screen and select a program or the Start option.

VMware is aware of the bug.
They say that it is resolved  in ESX 5 (next year)  – FullScreen mode should be completely redesigned.

Until then a patch is requested from PR, but no timeframe is released.
So if you have the same problem, please file a support request with VMware to speed up the process.

Yesterday we upgraded one of our vSphere Clusters to 4.1 – it went smoothly 🙂

But today the users reported that they weren’t able to use cut % paste between the guest and their computer using the vSphere Client (Console).

After a quick google we found that VMware has tightened the vSphere security by disabling this feature. See VMware KB 1026437.

If you need the cut & paste functionality you can enable it again on the guest or host level.

For a single VM:

  • Using the vSpher Client logon to your vCenter server.
  • Poweroff the VM.
  • Edit the VM’s settings
  • Navigate to Options > Advanced > General
  • Click Configuration Parameters
  • Add the following rows, by using Add Row
    isolation.tools.copy.disable –  false
    isolation.tools.paste.disable  – false
    
  • Click OK twice to close the dialogs and save the changes.
  • PowerOn the VM

For all VM’s on a host ESX/ESXi

Must be done on all hosts, so you don’t loose the functionality when the VM is migrated to another host.

  • Open a SSH to the host ex. using Putty
  • Open /etc/vmware/config in your favorit editor .
  • Add these lines to the file

    isolation.tools.copy.disable="FALSE"
    isolation.tools.paste.disable="FALSE"
    
  • Save and close the config file. Cut & Paste will work after a VM powerson, reboots or resume.

Just released the complete powershell script to control vRanger backup from vCenter.

Read about the script and it’s features here.

Download it here.