How to automate Powershell scripts for Vmware migration?

Techie solution
Techie solution used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jitu SSystem Engineer

Commented:
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.
Techie solutionVMware Specialist

Author

Commented:
I have to move vm across the cluster.
Jitu SSystem Engineer

Commented:
With the above code, you can gracefully shutdown the virtual machines and you can move across datacenters.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Techie solutionVMware Specialist

Author

Commented:
#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

Techie solutionVMware Specialist

Author

Commented:
I have Tried above script but network script is not working. it is throwing error network is not accessible.
Techie solutionVMware Specialist

Author

Commented:
please help me with this.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Did you check that the assigned network of the VM on the source host is also defined for the destination host?
Techie solutionVMware Specialist

Author

Commented:
No, what would be the format to change the network during migration
Jitu SSystem Engineer

Commented:
Vmotion VMKernel Port Group should have same (label) name at both source and destination network.
Techie solutionVMware Specialist

Author

Commented:
It is different on different dvswitch
Jitu SSystem Engineer

Commented:
Is there a separate Distributed switch for each cluster?
Techie solutionVMware Specialist

Author

Commented:
The new infra on same vcenter has different DVSwith
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
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.
VMware Specialist
Commented:
#Put the path of the csv file
 $vms = Import-CSV "C:\test\Testback.csv"
 foreach ($vm in $vms)
 {
 #Assign Variables
 $VMName = ($vm.Name1,$vm.Name2)
 $VMHost = ($vm.Host)
 $DD = ($vm.Datastore)
 $OldNetwork = ($vm.OldNetwork)
 $Net = ($vm.Network)
 Write-Host $VMName
 Write-Host $vm.Host
 Write-Host $vm.Datastore
 Write-Host $vm.Network
 Write-Host $vm.OldNetwork
 #Power off vms
 Get-VM -Name $VMName | Shutdown-VMGuest
 #Where the VM gets Migrated
 Move-VM -VM $VMName -Destination $VMHost -Datastore$DD
 Get-VM $VMName | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $Net -Confirm:$false
 #Power On VMs
 #Start-VM $VMName -Confirm
 }

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial