Goal : We need to migrate 200 Windows XP/Windows server 2003 VM’s from one vCenter (say vCenterA vSphere 4.0) to another vCenter (vCenterB vSphere 5.0)
Part 1 ( one Script):
We are in a situation to migrate around 200 XP/windows 2003 VM’s from one vCenter (say vCenterA vSphere 4.0) to another vCenter (vCenterB vSphere 5.0) in another geographical location. Our game plan of Part 1would be moving all the VMs to the transport LUN (shared datastore between source & destination vCenter hosts (DatastoreB)) (Say RPA lun which is enables access to the source & the destination vCenter hosts ). Once confirmed all the VM’s are moved to the transport LUN we will be power off the VM & unregistering all the 200 VM’s from the source vCenter (vCenterA vSphere 4.0).
Part 2 (another Script):
All the unregistered VM’s in the RPA LUN (say (DatastoreB))) We will be registering it to the hosts in the specific cluster (say Cluster_1) in the destination vCenter (vCenterB vSphere 5.0). Once confirmed all the 200 VM’s are registered in the destination vCenter, We will be changing the VM network for all the VM’s to different DHCP VLAN (VLAN say VLAN 15).
Once confirmed all the VMs are assigned with the new VM network (VLAN 15), We will be powering on all the VM’s in the specific interval (say sleep time 10 Seconds).
Once all the VM’s are powered on we need to upgrade the VMware tools to the current version (Tools Version 8389) and reboot all the VM’s. Once the VM’s are powered on we will be shutting down all the VM’s to upgrade the VM hardware version (say 4VM 4.0 or 7.0) to the current version (VM hardware version 8.0).
Once confirmed all the VM hardware’s are upgraded to the current version we will be powering on all the 200 VM’s sequentially (say in sleep interval 10 sec) & reboot it if the hardware upgrade requires reboot.
Finally we have to ipconfig/release & renew at the guest OS level.
Hello suresh,
we know the methodology u are saying. I think we cannot achieve this using the script because it is not tied up with one component say we have to tie our vcenter 4.0 script with RPA and the from RPA to vcenter 5.0. As of my knowledge there are no scripts that can integrate all these steps.Without registering the vm's to the vcenter the script cannot do anything bcoz the 90% of the script depends on the vcenter Database.
Thanks
VK
It is possible. We have the cmdlet to register the VM from specific datastore. If required you can ignore the VM registration part which we can do it manually also. Give me the script for the rest.
SureshD
FYI I am just trying to save 173 hours.
Sl.No | Tasks | Time Required Per VM (Minutes) | Time required for total 200 VMs (Minutes) | Time required for total 200 VMs Per Tasks (Hours) |
1 | Moving The VMs to the transport Datastore | 10 | 2000 | 33.33 |
2 | Power off the VM | 5 | 1000 | 16.67 |
3 | Unregister the VM from Source vCenter 4.0 | 2 | 400 | 6.67 |
4 | Reregister the VM in the destination vCenter | 2 | 400 | 6.67 |
5 | Changing the VM Network to DHCP VLAN (Say VLAN 15) | 2 | 400 | 6.67 |
6 | Powering on the VM | 3 | 600 | 10.00 |
7 | Upgrade the VMware tools | 5 | 1000 | 16.67 |
8 | Reboot the VM | 2 | 400 | 6.67 |
9 | shutting down the VM’s to upgrade the VM hardware version | 2 | 400 | 6.67 |
10 | Upgrade the VM hardware version | 2 | 400 | 6.67 |
11 | Powering on the VM | 3 | 600 | 10.00 |
12 | Reboot again it if the hardware upgrade requires reboot | 2 | 400 | 6.67 |
13 | ipconfig/release and Ipconfig/renew | 2 | 400 | 6.67 |
14 | Move the VM from Transport LUN to the Normal VMFS datastore | 10 | 2000 | 33.33 |
16 | Total Time | 52 | 10400 | 173.33 |
Hi LucD & Team,
Could you please help me providing the 2 scripts for the above requirements?
Regards
Suresh Dhanaraj
Hi
Welcome to the communities.
But I think its not safe or recommended procedure .
If its production environment then you should not follow this .
I would gladly help you with any difficulties you encounter in writing those scripts.
But did you already try to come up with some kind of script yourself ?
I think you split up the process in practical steps, and most of these steps are in fact just 1 cmdlet.
As a guideline I listed for each of your steps the cmdlet to use.
Let me know if this helps you on your way ?
Sl.No | Task | |
1 | Moving The VMs to the transport Datastore | Move-VM |
2 | Power off the VM | Shutdown-VMGuest |
3 | Unregister the VM from Source vCenter 4.0 | Remove-VM -DeletePermanently:$false |
4 | Reregister the VM in the destination vCenter | New-VM -VMFilePath |
5 | Changing the VM Network to DHCP VLAN (Say VLAN 15) | Set-NetworkAdapter |
6 | Powering on the VM | Start-VM |
7 | Upgrade the VMware tools | Update-Tools |
8 | Reboot the VM | Restart-VMGuest |
9 | shutting down the VM’s to upgrade the VM hardware version | Shutdown-VMGuest + |
10 | Upgrade the VM hardware version | Set-VM -Version
|
11 | Powering on the VM | Start-VM |
12 | Reboot again it if the hardware upgrade requires reboot | Restart-VMGuest |
13 | ipconfig/release and Ipconfig/renew | Invoke-VMScript |
14 | Move the VM from Transport LUN to the Normal VMFS datastore | Move-VM |
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I would be curious to know why this procedure should not be considered "safe" ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
HI LucD & Team,
Thanks a lot for your replies. The thing is I am a beginner for the PowerCLI & I am not from the scripting background. But I already read most of the concepts in PowerShell & PowerCLI & I have idea how to individually run the cmdlet or simple scripting for 1 or 2 cmdlet using foreach, invoke, import etc. But I need your help for running this in a workflow like checking with ‘If‘ statement , it’s passed that stage then it has to cross the next stage & providing the reasonable interval between all the tasks & running it in safe way.
Please help may be going forward I can come up with my script. I am running out of time. I am in a situation to get this task done.
Regards
Suresh Dhanaraj
Ok, without real testing the 2 scripts as I would write them.
The first script
$tgtDs = Get-Datastore -Name DatastoreB
&{foreach($vm in Get-VM)){ Move-VM -VM $vm -Datastore $tgtDs -Confirm:$false
Shutdown-VMGuest -VM $vm -Confirm:$false
Remove-VM -VM $vm -Confirm:$false -DeletePermanently:$false
$vm | Select Name,@{N="VMX";E={$vm.ExtensionData.Config.Files.VmPathName} }} | Export-Csv C:\vm-vmx.csv -NoTypeInformation -UseCulture
I use a CSV file to transfer the information from vCenter 1 to vCenter 2.
The 2nd script, to be run on vCenter 2 could look like this
$portgroup = Get-VirtualPortGroup -Name VLAN15
$tgtDs = Get-Datastore -Name NormalDS
Import-Csv C:\vm-vmx.csv -UseCulture | %{ $vm = New-VM -Name $_.Name -VMFilePath $_.VMX -VMHost (Get-Cluster -Name Cluster_1 | Get-VMHost | Get-Random) -Confirm:$false
Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -NetworkName $portgroup.Name -Confirm:$false
Start-VM -VM $vm -Confirm:$false
Update-Tools -VM $vm
Shutdown-VMGuest -VM $vm -Confirm:$false
Set-VM -VM $vm -Version v8 -Confirm:$false
Start-VM -VM $vm -Confirm:$false
Invoke-VMScript -VM $vm -ScriptText "ipconfig /release; ipconfig /renew" -ScriptType Bat -GuestUser $user -GuestPassword $pswd
Move-VM -VM $vm -Datastore $tgtDs -Confirm:$false
}
You will have to update the datastore names and the portgroup name.
If you don't run this with an account that has access to the guest OS, you will have to give correct values to the $user and $pswd variables.
A word of advise, try out this procedure first with 1 test VM !
Just change the Get-VM in the 1st script somehtin like
&{foreach($vm in Get-VM -Name TestVM)){
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks a lot LucD. I will test & reply you. I really liked it they way you asked me to build the script like “Give a man a fish; you have fed him for today. Teach a man to fish; and you will not have to listen to his incessant whining about how hungry he is.”
Thanks again. Surely I will start building my own script now onwards. If there are some challenges we will check with you for tweaking it.
Regards
Suresh Dhanaraj
Hi LucD,
I just did little tweaking on both the script 1 & script 2 with do-until loop to validate before/after each execution. Please find the attached for the modified scripts. Please go though the script & suggest me (provide me the modified script) solution for the below challenges.
Script1 Challenges:
Script2 Challenges:
Regards
Suresh Dhanaraj
Do you see the VXM path for all the VMs ? Do a
Get-VM | Select Name,@{N="VMX";E={$vm.ExtensionData.Config.Files.VmPathName}
To answer the question (script 2) try changing the beginning of script2 as follows
...
$vm = New-VM -Name $_.Name -VMFilePath $_.VMX -VMHost (Get-Cluster -Name $Clusters | Get-VMHost | Get-Random) -Confirm:$false
Get-VM $vm | Get-NetworkAdapter |Set-NetworkAdapter -NetworkName $NewPortGroup -Confirm:$false do { write-Host "Validating the $vm PortGroup"
sleep 10
} until (Get-VM $vm |Get-NetworkAdapter |Where {$_.NetworkName –eq $NewPortGroup }) write-Host "$vm has been mapped to $NewPortGroup"
$question = Get-VMQuestion -VM $vm
if($question){ Set-VMQuestion -VM $vm -DefaultOption -Confirm:$false
} Start-VM -VM $vm -Confirm:$false
....
When the Start-VM succeeds, the Update -Tools should run.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD & Team,
I have modified the script given by you depending upon my requirement. It’s 99% completed except the above challenges/issues.
Could you please help me ?
Regards
Suresh Dhanaraj
Sorry just now i received your mail. Let me take look.
Thanks again.
SureshD
Hi LucD,
Thanks again. Answer to your first question Do you see the VXM path for all the VMs ? actually the file is getting overwritten instead of appending it. Actually I have tried ruing script with 2 VM’s say WK500TES3 & WK500TES4. I have attached the output file when I pointed the destination datastore as NFS.
The CSV file updated with only the second VM name alone not a VMX path. It tried ruing this with other VMFS datastore and in that both VM name & VMX path was updated. I am not sure it is happening randomly. Anyhow today also let me try running it again with few other machines & confirm you again.
Could you please take a look at the 1t script & tell us are we pointed the export –CSV in the right place? Can we point his before unregistering the VM? I mean after the storage VMotion & before unregistering it.
Script 2:
Let me try your suggestions and let you know.
Regards
Suresh Dhanaraj
PowerCLI C:\Scripts> Get-VM $vm | Select Name,@{N="VMX";E={$vm.ExtensionData.Config.Files.VmPathName}}
Name VMX
---- ---
WK500TES4 [TESTFILER02_TRANS_400] WK500TES4/WK500TES4.vmx
PowerCLI C:\Scripts>
yes LuCD i am getting the details. It's the NFS datastore only. But somehow it's not updating it CSV. Let me try again today and let you know.
Regards
Suresh DhanarajI noticed you placed the Export-Csv inside the loop, I think it should be like this
$tgtDs = Get-Datastore -Name (Read-Host "Carefully enter the target Datastore name to move, PowerOFF and unregister the VM") $VMs = Get-Content -Path C:\Scripts\VM-To-sVMotion-Unregister.txt $vmObj = Get-vm $vms
&{foreach($vm in $vmObj){ Move-VM -VM $vm -Datastore $tgtDs -Confirm:$false
Shutdown-VMGuest -VM $vm -Confirm:$false
Write-Host "Shutting down $vm"
# Set the amount of time to wait before assuming the remaining powered on guests are stuck do { # Wait for the VMs to be Shutdown cleanly sleep 5
} until (Get-VM $vm | Where { $_.PowerState -eq "poweredOff" }) Write-Host "$vm Shutdown Complete"
Remove-VM -VM $vm -Confirm:$false -DeletePermanently:$false
Write-Host "Unregistering $vm" $vm | Select Name,@{N="VMX";E={$vm.ExtensionData.Config.Files.VmPathName}} }} | Export-Csv C:\Scripts\vm-vmx.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Oh got it LucD. Thanks again. Let me try and update you.
Regards
Suresh Dhanaraj
LucD,
Is there in any suitable place to put ‘-RunAsync’ in script 1 or 2 to reduce the timeline futher?
Regards
Suresh Dhanaraj