Reply to Message

View discussion in a popup

Replying to:
BenLiebowitz
Expert
Expert

I've had a similar issue to this when migrating VMs from 4.1 to 5.5 with a new vCenter.  I ended up writing a PowerCLI script that exports a list of all the VMs in the PROD VC to CSV.  It then connects to the other VC, in your case the DR VC, browses the datastore, adds the VM to inventory, sets the network, places it back into the correct folder, etc.   If you don't want to power them on, you can remove the PowerON VMs section.  In my case, we were upgrading so the script also upgrades the virtual hardware and the tools.

Hope it helps.

-----------------------

#Build the BlueFolderPath function

New-VIProperty -Name 'FullPath' -ObjectType 'VirtualMachine' -Value {

    param($vm)

  $current = Get-View $vm.ExtensionData.Parent

  $path = ""

  do {

  $parent = $current

  if($parent.Name -ne "vm"){$path =  $parent.Name + "/" + $path}

  $current = Get-View $current.Parent

  } while ($current.Parent -ne $null)

  $path.TrimEnd('/')

} -Force | Out-Null

#Build the Get-folderbypath function

function Get-FolderByPath {

  <#

.SYNOPSIS  Retrieve folders by giving a path

.DESCRIPTION The function will retrieve a folder by it's

  path. The path can contain any type of leave (folder or

  datacenter).

.NOTES  Author:  Luc Dekens

.PARAMETER Path

  The path to the folder.

  This is a required parameter.

.PARAMETER Separator

  The character that is used to separate the leaves in the

  path. The default is '/'

.EXAMPLE

  PS> Get-FolderByPath -Path "Folder1/Datacenter/Folder2"

.EXAMPLE

  PS> Get-FolderByPath -Path "Folder1>Folder2" -Separator '>'

#>

  param(

  [CmdletBinding()]

  [parameter(Mandatory = $true)]

  [System.String[]]${Path},

  [char]${Separator} = '/'

  )

  process{

    if((Get-PowerCLIConfiguration).DefaultVIServerMode -eq "Multiple"){

      $vcs = $defaultVIServers

    }

    else{

      $vcs = $defaultVIServers[0]

    }

    foreach($vc in $vcs){

      foreach($strPath in $Path){

        $root = Get-Folder -Name Datacenters -Server $vc

        $strPath.TrimStart($Separator).Split($Separator) | %{

          $root = Get-Inventory -Name $_ -Location $root -Server $vc -NoRecursion

          if((Get-Inventory -Location $root -NoRecursion | Select -ExpandProperty Name) -contains "vm"){

            $root = Get-Inventory -Name "vm" -Location $root -Server $vc -NoRecursion

          }

        }

        $root | where {$_ -is [VMware.VimAutomation.ViCore.Impl.V1.Inventory.FolderImpl]}|%{

          Get-Folder -Name $_.Name -Location $root.Parent -Server $vc

        }

      }

    }

  }

}

# Enable connections to multiple vCenters

Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope AllUsers -Confirm:$false

# Connect to Prod VC

connect-viserver PRODVC

# export list of VMs to Migrate to CSV

Get-Datacenter -name "DataCenter" | Get-VM |

Select Name, @{N="Datastore";E={Get-Datastore -VM $_ | Select -ExpandProperty Name}},

  @{N="Network";E={Get-VirtualPortgroup -VM $_ | Select -ExpandProperty Name}},

  @{N="Parent";E={$_.FullPath}} |

Export-Csv "c:\migrate.csv" -NoTypeinformation -UseCulture

# Connect to DR VC

connect-viserver DRVC

# Set a host in DR Cluster to add them to.

$esxhost = "DRVSPHERE01"

## Add backto inventory & Change network for VMs

foreach ($row in (Import-Csv c:\Migrate.csv)) {

    $vmName = $row.Name

    $vmxfile = "[$($row.Datastore)] $($vmName)/$($vmName).vmx"

#    $vmlocation = $row.Parent.Split('/')

    $vmNewVM = New-VM -VMFilePath $vmxfile -VMhost $esxhost

    # Move VM to existing folder locaiton

     Move-VM -VM $row.Name -Destination (get-folderbypath $row.Parent)

    # Change Network Portgroup

    $vm = get-vm -Name $row.Name

        Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -NetworkName $row.Network -Confirm:$false

    # Upgrade Virtual Hardware

    Set-VM -VM $vmName -Version v10

    # Update VMware Tools

    update-tools $vmName -Confirm:$false

    start-sleep -s 300

    # Poweron VMs

    Start-VM -VM $vmName -Confirm:$false | wait-tools

    Get-VMQuestion -VM $vmName | Set-VMQuestion –Option "I moved it" -confirm:$false

} ## end foreach

Ben Liebowitz, VCP vExpert 2015, 2016, & 2017 If you found my post helpful, please mark it as helpful or answered to award points.
Reply
0 Kudos