Link to home
Start Free TrialLog in
Avatar of Techie solution
Techie solution

asked on

How to automate Powershell scripts for Vmware migration?

hi i want to automate my vm migration from one cluster to another via powershell.  please help me throgh . Also please guide me how can i include vmware cli in powershell.
Avatar of Jitu S
Jitu S
Flag of India image

Hi,

You can move multiple virtual machines to an ESX server via below code. If you have shared storage between the source and destination host you can comment out the shutdown line.

You can add a snap-in to Powershell to load the PowerCLi commands i.e Add-PSSnapin Vmware.Vimautomation.core -- type this in Powershell.

Code:
$Servers = Get-Content "C:\temp\Servers.txt"
Foreach ($server in $servers)
{
Shutdown-VMguest $server -Confirm:$false
Write-Host -Foregroundcolorcolor Green "Virtual Machine Powered off"
Move-VM $server -Destination "Destination ESX IP or FQDN"
Write-Host -Foregroudcolorcolor Green "Virtual Machine Migrated to Destination"
}

Open in new window


Please let me know if this is helpful.
Avatar of Techie solution
Techie solution

ASKER

I have to move vm across the cluster.
With the above code, you can gracefully shutdown the virtual machines and you can move across datacenters.
#Put the path of the csv file
 $vms = Import-CSV "C:\test\3.csv"
 foreach ($vm in $vms)
 {
 #Assign Variables
 $VMName = ($vm.Name1,$vm.Name2,$vm.Name3,$vm.Name4,$vm.Name5, $vm.Name6,$vm.Name7,$vm.Name8, $vm.Name9,$vm.Name10)
 $vHost = $vm.Hos
 $vDatastore = $vm.Datastore
 $vNetwork = $vm.Network
 #Power off vms
 Get-VM -Name $VMName | Shutdown-VMGuest
 #Where the VM gets Migrated (Datastore:VV02-FC-D01;VV02-FC-D02;VV02-FC-D03;VV02-FC-D04,VV02-FC-D01 to D04)
 #Host is XXXXXXXXX017 to XXXXXXXX0024, XXXXXXXX0025 to XXXXXXX0032)
 Move-VM -VM $VMName -Destination $vHost -Datastore $vDatastore
 #Set Network Adapter (NET_10.xx.xx.0_24 to NET_NET_10.xx.xx.0_24,NET_10.xx.xx.0_24)
 Get-VM -Name $VMName|Get-NetworkAdapter| Set-NetworkAdapter -NetworkName $vNetwork -Confirm:$false
 #Power On VMs
 Start-VM $VMName -Confirm -RunAsync
 }

Open in new window

I have Tried above script but network script is not working. it is throwing error network is not accessible.
please help me with this.
Avatar of Qlemo
Did you check that the assigned network of the VM on the source host is also defined for the destination host?
No, what would be the format to change the network during migration
Vmotion VMKernel Port Group should have same (label) name at both source and destination network.
It is different on different dvswitch
Is there a separate Distributed switch for each cluster?
The new infra on same vcenter has different DVSwith
I guess you will have to set up a "fake" DV switch on the new infrastructure just for the migration, and then change the DV switch in the migrated VMs.
ASKER CERTIFIED SOLUTION
Avatar of Techie solution
Techie solution

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial