BjornJohansson
Enthusiast
Enthusiast

Storage vMotion between clusters from CSV mapping file

Hi,

We need to move quite a few VM's off old hardware. The plan is to use a PowerCLI script that reads from a CSV to serialize move the VM's to the new cluster. The move is done online storage vMotion for all Windows VM's. All VM's must end up on correct site, datastore, vlan and DRS group. So I'm thinking using the csv as a mapping file to specify correct destination for each VM.

The Plan in overview

  1. Get all VM's that will migrated into a csv (done)
  2. Add new columns i csv where we manually map following information (done)
    1. Destination Portgroup (we are migrating from Standard to Distributed vSwitch)
    2. Destination VMhost (we are running a Metro configuration and VM's must end up at correct site)
    3. Destination Datastore (same reason as above, some VM's are replicated, some resides on local datastores)
    4. Destination DRS group (pin VM to site because of Metro)
  3. Import-csv and loop move-vm foreach VM

So even if we only move one VM at the time, it will work 24/7 serialized and minimizing overloading production hardware. 

Any hole in the plan so far? Please let me know!

The csv file

Some manual stuff, but CSV is not too time consuming to map site, portgroups etc. in Excel. VMhost is only included to help site mapping.

Name, PortGroup, DestinationPG,VMhost, DestinationVMhost, Datastore, DestinationDS, DrsGroup

migrationVM1,vlan_214, dpg_214 esxi-02, esxi-prod-01,nfs_03, nfs_prod_01, vm-affinity-site1

migrationVM2,vlan_210, dpg_210 esxi-14, esxi-prod-06,nfs_02, nfs_prod_11, vm-affinity-site2

The script

Here I need some help. Think the plan is OK, but my script is not approaching it correctly. Got it to work as a one-liner for one VM.

$csvinput = Import-Csv -Path "C:\Powershell\CSV\Migrate.csv" -Header Name,PortGroup,DestinationPG,VMhost,DestinationVMhost,Datastore,DestinationDS,DrsGroup

$vm = Get-VM -Name $csvinput.Name

$destinationEsx = Get-VMHost $csvinput.DestinationVMhost

$networkAdapter = Get-NetworkAdapter -VM $vm

$destinationPortGroup = Get-VDPortgroup -VDSwitch 'vDS-Prod-01' -Name $csvinput.DestinationPG

$destinationDatastore = Get-Datastore $csvinput.Datastore

Foreach ($VM in $csvinput)

$vm | Move-VM -Destination $_.$destinationEsx -NetworkAdapter $networkAdapter -PortGroup $destinationPortGroup -Datastore $destinationDatastore -DiskStorageFormat Thin

I can see that this approach is very faulty, but not sure how to fix it?

Any tips?

Thank you!

Tags (1)
Reply
0 Kudos