Wednesday, June 19, 2013

Automatically Check and Upgrade VMware Tools During Power Cycling

After upgrading a cluster of ESXi 5.0 hosts to ESXi 5.1U1, I was once again nagged by the menial tasks of upgrading VMware tools and the VM hardware. Fortunately, there is a setting that you can configure within each VM to automatically check and upgrade VMware tools during a power cycle. This setting can be found under the "options" tab in the Virtual Machine settings:



As an added bonus, this can be scripted to change the setting for all VMs using PowerCLI (thanks to this blog).

Connect-VIServer -Server vcenter

Get-Cluster -Name "Cluster-Name" | Get-VM | Get-View | ForEach-Object{

if ($_.config.tools.toolsUpgradePolicy -ne “upgradeAtPowerCycle”){

$vm = Get-VM -Name $_.name
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.changeVersion = $vm.ExtensionData.Config.ChangeVersion
$spec.tools = New-Object VMware.Vim.ToolsConfigInfo
$spec.tools.toolsUpgradePolicy = “upgradeAtPowerCycle”
$_this = Get-View -Id $vm.Id
$_this.ReconfigVM_Task($spec)

}
}

Disconnect-VIServer vcenter

Monday, June 17, 2013

Backing up ESXi Hosts Configurations using vSphere vCLI

It's always a good practice to back up the ESXi host configs before any updates/patches are rolled out to it. However, every single time I do it I always end up with some syntax/wrong parameter error.

For reference, running the following command will backup the ESXi host configs onto the local desktop. From my experience, this configuration file can only be used to restore the ESXi configs onto an ESXi host of the same version.

"C:\Program Files (x86)\VMware\VMware vSphere CLI\Perl\bin\Perl.exe" "C:\Program Files (x86)\VMware\VMware vSphere CLI\bin\vicfg-cfgbackup.pl" --server esxi-server  --username root -s c:\esxi-server.bak


Tuesday, June 4, 2013

VMware PowerCLI Reference List

Ever since I started my new job, I've had to increasingly use PowerCLI scripts/commands in order to configure and manage the environments. When you have hundreds of ESXi hosts, it becomes impossible to manually make changes on each and every host via the vSphere Client. This post will be used as a place for me to reference the commands that I use fairly often.


Networking

Add virtual port group to every host in a cluster and tag it with the specific VLAN:

Get-Cluster "DRP" | get-vmhost | Get-VirtualSwitch -name vSwitch0 | new-VirtualPortGroup -name "Production" -vlanid 10


Remove specified virtual port group from every host in a cluster:

Get-Cluster "test" | Get-VMHost | Get-VirtualSwitch -Name "vSwitch0" | Get-VirtualPortGroup -Name "Test" | Remove-VirtualPortGroup







Storage

Change PSP to round robin for all LUNs on all hosts:


Get-Cluster "DRP" | Get-VMHost | Get-ScsiLun -CanonicalName "naa.xxx*" | Set-ScsiLun -MultipathPolicy "roundrobin"


Rescan datastores for new storage devices and new VMFS volumes

Get-Cluster "ClusterName" | Get-VMHost | Get-VMHostStorage -RescanAllHba -RescanVmfs


Add NFS datastore to all hosts in a cluster

Get-Cluster "Cluster Name" | Get-VMHost | New-Datastore -Nfs -Name "Datastore Name" -Path "/path" -NfsHost "IP address"