PowerShell/PowerCLI – vMotion VMs between clusters based on Name and IP

A quick post where I threw together a script to vMotion all VMs of a particular wildcard name and a certain IP range between two vSphere clusters (5.0 and 5.5) under the same vCenter Server.

The script assumes your source and destination clusters are using Standard vSwitches (VSS). with identical port group names on each.

In a migration scenario, perhaps your source cluster is using a Standard vSwitch, but the new destination cluster has a vSphere Distributed Switch (VDS).

In this case, I would implement a temporary standard vSwitch with a single NIC on the new cluster with identical port group names as the source cluster.

This makes vMotion migration simple. Once VMs are on the new cluster, vNICs can be bounced from standard to VDS port groups.

Anyway – on with the script. PowerCLI version used was 6.0 Release 1

#vMotion VMs based on name and IP address between clusters under one vCenter
#Assumes standard vSwitch networking and consistent port group names between clusters
#Source cluster (Std vSwitch), destination Cluster (temp migration Std vSwitch) with same port group names. Maker sure Log folder is created ahead of time

Import-Module VMware.VimAutomation.Core

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Scope Session -Confirm:$false

#Change your variables here:
$vcenter = "MYvCenterServer"
$sourcecluster = "MyOldCluster"
$destcluster = "MyNewCluster"
$VMnameFilter = "PROD*"
$IPFilter = "10.1.3.*"
$logfolder = "c:\MigrationLogs"

Connect-VIServer $vcenter

$getvms = Get-Cluster $sourcecluster | Get-VM | where name -Like $VMnameFilter | Select Name, @{ L = "IP Address"; E = { ($_.guest.IPAddress[0]) } } | where "IP Address" -Like $IPFilter | select -ExpandProperty name

foreach ($vm in $getvms)
{
Move-VM -vm $vm -Destination $destcluster -VMotionPriority High -Confirm: $false -ErrorAction SilentlyContinue

If (!$error)
{ Write-Output $vm, $error[0] | out-file -append -filepath "$logfolder\success.log" }
Else
{ Write-Output $vm, $error[0] | out-file -append -filepath "$logfolder\failed.log" }

}
Advertisements

USB passthrough in vSphere 4.1

One of the new features of vSphere/ESX 4.1 is the ability to pass-through up to 20 USB devices from the ESX host to a VM or VMs.

It is really simple to setup and test.

I am using a Server 2008 R2 VM as a test in this case.

Right click on the VM and select edit settings.

Add a USB Controller and then click OK and exit the edit settings screen.

Edit settings on the VM again and add a USB device.

At this point the wizard will show you any visible/compatible device you have plugged into the underlying ESX 4.1 host

A device HCL is here on VMwares support site: http://kb.vmware.com/kb/1021345

You cannot multi-select devices at this stage – add them one by one.

There is an option to allow vMotion of the VM while the USB device is connected.

VMware documentation states:¬† “You can migrate a virtual machine to another ESX/ESXi host in the same datacenter and maintain the USB
passthrough device connections to the original host.”

I tested vMotion with a USB mass storage device attached and it does indeed work across ESX hosts as promised.

In the screen below I have now added two pass-through USB devices to my VM (A Kingston USB drive and a Safenet/Raindow dongle)

Inside the Windows VM – looking at device manager – the devices have appeared.

Both devices work correctly as intended.

I have tested numerous brands of USB mass storage devices (Kingston, Sandisk, Lexar, Imation) as well a couple of of security dongles and they all work well.

Also, please check out the USB pass-through section in the Virtual Machine Administration Guide PDF that is part of the vSphere 4.1 documentation.

http://www.vmware.com/support/pubs/vs_pubs.html