Bulk import VMs while changing the virtual switch name

Q. How can I bulk import VMs into a Hyper-V server while changing the network switch it is connected to?

A. I recently had to move a large number of VMs to a new cluster. The new cluster used a new name for the virtual switch which would block a normal import of the VM (remember that in 2012 you do not have to first export VMs to be able to import them). Fortunately it is simple to find a list of compatibility problems (which would be the bad switch name) and then correct. Below is the script.

$XMLFiles = Get-ChildItem C:\ClusterStorage\VMs\*.XML -Recurse
foreach ($File in $XMLFiles)
{
    $File.FullName
    $Report = Compare-VM -Path $File.FullName
    $Report.Incompatibilities[0].Source | Connect-VMNetworkAdapter -SwitchName 'Datacenter Switch No Team'
    Import-VM -CompatibilityReport $Report
}

Note that the script only works if the VM has a single NIC that needs to be updated and there are no other problems. If there are other issues the import will fail.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish