PowerCLI: Set-dvSwitch


image

Last weekend I was playing around with the new dvSwitch feature in vSphere. So I created a dvSwitch and wanted to migrate my VM’s to it. Unfortunately this was not possible with the current version of PowerCLI. Normally you should be able to change the Network switch via:

Get-VM | Get-NetworkAdapter `
| Set-NetworkAdapter -NetworkName "traditional vswitch" -Confirm:$false

There must be a way to do this with PowerCLI but I didn’t know that way. So I asked Luc Dekens and the other PowerCLI guru’s for a solution. A couple of hours later Luc send me a script which was able to do exactly what I wanted to do.

The function / script can be found over here: http://poshcode.org/1373

You can start the function like this: Set-dvSwitch VirtualMachine dvSwitchPortgroup

image

Just wait a couple of seconds till the Reconfigure virtual machine task is ready:

image

You can also run this function against all your VM’s via the following command:

$vms = Get-VM
foreach($vmName in $vms){
    Set-dvSwitch $vmName dvPG_production
}

Just wait a while and all your VM’s are migrated to the new dvSwitch:

image

If you want to start testing with the dvSwitch, keep an eye on http://lucd.info! @LucD22 is going to post an article about what you can do with PowerCLI and the dvSwitch.

Advertisements

7 thoughts on “PowerCLI: Set-dvSwitch

  1. Pingback: Twitter Trackbacks for PowerCLI: Set-dvSwitch « ICT-Freak.nl [ict-freak.nl] on Topsy.com

  2. Pingback: vSphere 4.0 vNetwork Distributed Switch (vDS) - Video Demonstration + Architecture Diagram | The deep core of the phenomena| HyperViZor

  3. Pingback: VMware vSphere - виртуализация ЦОД » Что такое и как работает vSphere 4.0 vSphere 4.0 vNetwork Distributed Switch (vDS).

  4. We are using this to migrate VM’s from standard vSwitches to vDS. The only problem we are having is that the MAC addresses seem to change on the NICs after migration. Is there a way to avoid this?

    • We solved our own issue with the help of Onyx. You need to add some additional device specs to maintain your previous NICS settings:

      Here is the updated code:

      $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
      $devChange = New-Object VMware.Vim.VirtualDeviceConfigSpec
      $devChange.operation = “edit”

      $dev = New-Object (“VMware.Vim.” + $tgtdev.GetType().Name)
      $dev.deviceInfo = New-Object VMware.Vim.Description
      $dev.deviceInfo.label = $tgtdev.DeviceInfo.Label
      $dev.deviceInfo.summary = $tgtdev.DeviceInfo.Summary
      $dev.Backing = New-Object VMware.Vim.VirtualEthernetCardDistributedVirtualPortBackingInfo
      $dev.Backing.Port = New-Object VMware.Vim.DistributedVirtualSwitchPortConnection
      $dev.Backing.Port.PortgroupKey = $dvPGKey
      $dev.Backing.Port.SwitchUuid = $dvSwitchUuid
      $dev.Key = $tgtdev.Key

      $dev.connectable = New-Object VMware.Vim.VirtualDeviceConnectInfo
      $dev.connectable.startConnected = $tgtdev.connectable.startConnected
      $dev.connectable.allowGuestControl = $tgtdev.connectable.allowGuestControl
      $dev.connectable.connected = $tgtdev.connectable.connected
      $dev.connectable.status = $tgtdev.connectable.status
      $dev.controllerKey = $tgtdev.controllerKey
      $dev.unitNumber = $tgtdev.unitNumber
      $dev.addressType = $tgtdev.addressType
      $dev.macAddress = $tgtdev.MacAddress
      $dev.wakeOnLanEnabled = $tgtdev.wakeOnLanEnabled

      $devChange.Device = $dev
      $spec.deviceChange = $devChange

      $taskMoRef = $vm.ReconfigVM_Task($spec)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.