Home > Blogs > VMware PowerCLI Blog

Spotlight on the Move-VM Cmdlet including PowerCLI 6.5 Enhancements

VMware PowerCLI 6.5’s release introduced a lot of new cmdlets and improvements to existing cmdlets. One of my favorite improved cmdlets has to be Move-VM. Move-VM was already a very versatile cmdlet before this new release. It could be used to move a VM between hosts, datastores, resource pools, clusters, to new folders, to a vApp, and so forth. Now, with PowerCLI 6.5 R1, Move-VM can move VMs between vCenters! We can even take that a step further, Move-VM can move VMs to vCenters which are not linked together by SSO domain. That’s something that cannot be done by the web client!

Let’s start by taking a look at the newest addition to the cmdlet, migrating VMs between vCenters.

Cross vCenter vMotion

Cross vCenter vMotion was introduced with vSphere 6.0. It’s proved to be a great feature that opens up new options in flexibly managing a vSphere environment. One of the limitations in using cross vCenter vMotion by way of the Web Client is that it can’t migrate VMs between SSO domains. This is where the usage of PowerCLI is hugely beneficial because PowerCLI allows users to fill that gap. The key to PowerCLI being able to overcome that limitation is due to its ability to connect to multiple vCenter servers at the same time.

In order to perform a cross vCenter vMotion, there are a couple parameters that are needed:

  • Active Connection to both Source and Destination vCenters
  • Source vCenter
    • VM
    • VM’s Network Adapter/s
  • Destination vCenter
    • Destination ESXi Host
    • Destination Datastore
    • Destination PortGroup/s

Here’s some example code on how that can be accomplished:

$sourceVCConn = Connect-VIServer -Server $sourceVC -user $sourceVCUsername -password $sourceVCPassword
$destVCConn = Connect-VIServer -Server $destVC -user $destVCUsername -password $destVCpassword

$vm = Get-VM $vmname -Server $sourceVCConn
$networkAdapter = Get-NetworkAdapter -VM $vm -Server $sourceVCConn

$destination = Get-VMHost -Server $destVCConn
$destinationPortGroup = Get-VDPortgroup -VDSwitch $switchname -Name VM-Network1 -Server $destVCConn
$destinationDatastore = Get-Datastore $datastorename -Server $destVCConn

Move-VM -VM $vm -Destination $destination -NetworkAdapter $networkAdapter -PortGroup $destinationPortGroup -Datastore $destinationDatastore

I also want to filling in a couple informative gaps in the above code:

  • Due to the cross vCenter vMotion functionality being added in as part of vSphere 6.0, it can only be performed against vCenter servers of 6.0 and newer.
  • The destination parameter only accepts an individual ESXi host. A workaround could look like the following code:
    $destination = Get-Cluster –Name Cluster01 –Server $destVCConn | Get-VMHost | Select-Object –First 1
  • The datastore parameter only accepts an individual datastore. A workaround could look like the following code:
    $destinationDatastore = Get-DatastoreCluster –Name DSCluster01 –Server $destVCConn | Get-Datastore | Sort-Object -Property FreeSpaceGB -Descending | Select-Object -First 1

Multiple NIC VM vMotion

If you look at the example code above, specifically at the network pieces, you’ll notice that it only focuses on moving a VM with a single NIC. However, both the NetworkAdapter and PortGroup parameters accept arrays. This means that you can pass multiple objects to those parameters.

One key thing to note is that the first item in each array will be referenced together, then the second items will be referenced together, and so on and so forth. If there are multiple network adapters and only one portgroup specified, the network adapters will all be assigned to the same portgroup. If there is only a single network adapter and multiple portgroups specified, the command will error out.

Here’s an example of doing a Cross vCenter vMotion on a VM that has multiple NICs:
Move-VM with Multiple Network Adapters

Conclusion

The above examples are just the tip of showing just how versatile the Move-VM cmdlet really is. Let us know how you’re using Move-VM in your environment in the comments below!

For more information on its use in your environment, see the PowerCLI cmdlet reference on Move-VM.

This entry was posted in General and tagged , , on by .
Kyle Ruddy

About Kyle Ruddy

Kyle Ruddy is a Senior Technical Marketing Engineer at VMware in the Cloud Platform Business Unit. Kyle currently focuses on vSphere with Operations Management as well as all things API, SDK, and CLI. Kyle can be found blogging on VMware blogs, http://blogs.vmware.com/vSphere and http://blogs.vmware.com/PowerCLI, and his personal blog, http://www.thatcouldbeaproblem.com. You can follow Kyle on twitter as @kmruddy.



Spotlight on the Move-VM Cmdlet including PowerCLI 6.5 Enhancements

VMware PowerCLI 6.5’s release introduced a lot of new cmdlets and improvements to existing cmdlets. One of my favorite improved cmdlets has to be Move-VM. Move-VM was already a very versatile cmdlet before this new release. It could be used to move a VM between hosts, datastores, resource pools, clusters, to new folders, to a vApp, and so forth. Now, with PowerCLI 6.5 R1, Move-VM can move VMs between vCenters! We can even take that a step further, Move-VM can move VMs to vCenters which are not linked together by SSO domain. That’s something that cannot be done by the web client!

Let’s start by taking a look at the newest addition to the cmdlet, migrating VMs between vCenters.

Cross vCenter vMotion

Cross vCenter vMotion was introduced with vSphere 6.0. It’s proved to be a great feature that opens up new options in flexibly managing a vSphere environment. One of the limitations in using cross vCenter vMotion by way of the Web Client is that it can’t migrate VMs between SSO domains. This is where the usage of PowerCLI is hugely beneficial because PowerCLI allows users to fill that gap. The key to PowerCLI being able to overcome that limitation is due to its ability to connect to multiple vCenter servers at the same time.

In order to perform a cross vCenter vMotion, there are a couple parameters that are needed:

  • Active Connection to both Source and Destination vCenters
  • Source vCenter
    • VM
    • VM’s Network Adapter/s
  • Destination vCenter
    • Destination ESXi Host
    • Destination Datastore
    • Destination PortGroup/s

Here’s some example code on how that can be accomplished:

$sourceVCConn = Connect-VIServer -Server $sourceVC -user $sourceVCUsername -password $sourceVCPassword
$destVCConn = Connect-VIServer -Server $destVC -user $destVCUsername -password $destVCpassword

$vm = Get-VM $vmname -Server $sourceVCConn
$networkAdapter = Get-NetworkAdapter -VM $vm -Server $sourceVCConn

$destination = Get-VMHost -Server $destVCConn
$destinationPortGroup = Get-VDPortgroup -VDSwitch $switchname -Name VM-Network1 -Server $destVCConn
$destinationDatastore = Get-Datastore $datastorename -Server $destVCConn

Move-VM -VM $vm -Destination $destination -NetworkAdapter $networkAdapter -PortGroup $destinationPortGroup -Datastore $destinationDatastore

I also want to filling in a couple informative gaps in the above code:

  • Due to the cross vCenter vMotion functionality being added in as part of vSphere 6.0, it can only be performed against vCenter servers of 6.0 and newer.
  • The destination parameter only accepts an individual ESXi host. A workaround could look like the following code:
    $destination = Get-Cluster –Name Cluster01 –Server $destVCConn | Get-VMHost | Select-Object –First 1
  • The datastore parameter only accepts an individual datastore. A workaround could look like the following code:
    $destinationDatastore = Get-DatastoreCluster –Name DSCluster01 –Server $destVCConn | Get-Datastore | Sort-Object -Property FreeSpaceGB -Descending | Select-Object -First 1

Multiple NIC VM vMotion

If you look at the example code above, specifically at the network pieces, you’ll notice that it only focuses on moving a VM with a single NIC. However, both the NetworkAdapter and PortGroup parameters accept arrays. This means that you can pass multiple objects to those parameters.

One key thing to note is that the first item in each array will be referenced together, then the second items will be referenced together, and so on and so forth. If there are multiple network adapters and only one portgroup specified, the network adapters will all be assigned to the same portgroup. If there is only a single network adapter and multiple portgroups specified, the command will error out.

Here’s an example of doing a Cross vCenter vMotion on a VM that has multiple NICs:
Move-VM with Multiple Network Adapters

Conclusion

The above examples are just the tip of showing just how versatile the Move-VM cmdlet really is. Let us know how you’re using Move-VM in your environment in the comments below!

For more information on its use in your environment, see the PowerCLI cmdlet reference on Move-VM.

This entry was posted in General and tagged , , on by .
Kyle Ruddy

About Kyle Ruddy

Kyle Ruddy is a Senior Technical Marketing Engineer at VMware in the Cloud Platform Business Unit. Kyle currently focuses on vSphere with Operations Management as well as all things API, SDK, and CLI. Kyle can be found blogging on VMware blogs, http://blogs.vmware.com/vSphere and http://blogs.vmware.com/PowerCLI, and his personal blog, http://www.thatcouldbeaproblem.com. You can follow Kyle on twitter as @kmruddy.

9 thoughts on “Spotlight on the Move-VM Cmdlet including PowerCLI 6.5 Enhancements

  1. David Warner

    Great stuff. 2 questions.

    1.) Moving VMs across vcenters, does the source and destination ESX hosts have to be able to communicate? Or does only vCenters need to be able to communicate over network to move a VM.

    2.) Is it possible to copy/clone a VM to a different vcenter with PowerCLI?

    Thanks.

    Reply
  2. Josh (Windos) King

    From everything I’m reading this should allow me to move VMs between any vCenter running 6.0 or above, however it errors out for me when I try to move a VM from vCenter 6.5 yo vCenter 6 (Update 2). Is this a bug or intended behavior?

    Reply
    1. Kyle RuddyKyle Ruddy Post author

      That is not as intended. However there are several reasons that may occur due to some items not being backwards compatible (such as hardware versions, distributed port group versions, etc.).

      I’ll follow up via email to see what the error is.

      Reply
  3. Pingback: Using PowerCLI to vMotion VM between different SSO domains

  4. Christian

    I got this error message:

    The operation for the entity “VM-Name failed with the following message: “vMotion is not licensed for the Source host

    Both Source and Dest. vCenter are latest 6.0
    Both Source and Dest ESXi Host are 5.5 with E+ license

    MFG
    Christian

    Reply
  5. shreya

    Great article, i understood each and everything about what you said in this article but Is it possible to copy/clone a VM to a different vcenter with PowerCLI?

    Reply
  6. Chris Childerhose

    How do you keep the same datastore that the VM is on when doing cross vCenter migrations? I have VMs that won’t move in the Web UI due to having multiple VMDKs on multiple datastores due to performance policies like SQL. How do you get around this to keep them on the same shared datastore? I already have moved some hosts to the new VC so each host has the same datastore connections that the ones in the old VC have. I need to get my last 2 hosts moved over to the new VC.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*