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.

8 thoughts on “PowerCLI: Set-dvSwitch

  1. 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?

    1. 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)

  2. great script. do you have one that works the other way around?
    from dvs to std switch ?

    thanks

Leave a comment

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