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.
Techie solutionVMware SpecialistAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jitu SSystem EngineerCommented:
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.
0
Techie solutionVMware SpecialistAuthor Commented:
I have to move vm across the cluster.
0
Jitu SSystem EngineerCommented:
With the above code, you can gracefully shutdown the virtual machines and you can move across datacenters.
0
Discover the Answer to Productive IT

Discover app within WatchGuard's Wi-Fi Cloud helps you optimize W-Fi user experience with the most complete set of visibility, troubleshooting, and network health features. Quickly pinpointing network problems will lead to more happy users and most importantly, productive IT.

Techie solutionVMware SpecialistAuthor 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

0
Techie solutionVMware SpecialistAuthor Commented:
I have Tried above script but network script is not working. it is throwing error network is not accessible.
0
Techie solutionVMware SpecialistAuthor Commented:
please help me with this.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Did you check that the assigned network of the VM on the source host is also defined for the destination host?
0
Techie solutionVMware SpecialistAuthor Commented:
No, what would be the format to change the network during migration
0
Jitu SSystem EngineerCommented:
Vmotion VMKernel Port Group should have same (label) name at both source and destination network.
0
Techie solutionVMware SpecialistAuthor Commented:
It is different on different dvswitch
0
Jitu SSystem EngineerCommented:
Is there a separate Distributed switch for each cluster?
0
Techie solutionVMware SpecialistAuthor Commented:
The new infra on same vcenter has different DVSwith
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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.
0
Techie solutionVMware SpecialistAuthor 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
 }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.