VMware Cloud Community
SureshDhanaraj
Contributor
Contributor

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)

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.

Tags (3)
Reply
0 Kudos
41 Replies
vkaranam
Enthusiast
Enthusiast

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

Reply
0 Kudos
SureshDhanaraj
Contributor
Contributor

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

Reply
0 Kudos
SureshDhanaraj
Contributor
Contributor

FYI I am just trying to save 173 hours.

Sl.NoTasksTime Required   Per VM (Minutes)Time required   for total 200 VMs (Minutes)Time required   for total 200 VMs Per Tasks (Hours)
1Moving The VMs to the   transport Datastore10200033.33
2Power off the VM5100016.67
3Unregister the VM   from Source vCenter 4.024006.67
4Reregister the VM in   the destination vCenter  24006.67
5Changing the VM   Network to DHCP VLAN (Say VLAN 15) 24006.67
6Powering on the VM360010.00
7Upgrade the VMware   tools5100016.67
8Reboot the VM24006.67
9shutting down the   VM’s to upgrade the VM hardware version24006.67
10Upgrade the VM   hardware version24006.67
11Powering on the VM360010.00
12Reboot again it if   the hardware upgrade requires reboot24006.67
13ipconfig/release and   Ipconfig/renew24006.67
14Move the VM from   Transport LUN to the Normal VMFS datastore10200033.33
16Total Time5210400173.33
Reply
0 Kudos
SureshDhanaraj
Contributor
Contributor

Hi LucD & Team,

Could you please help me providing the 2 scripts for the above requirements?

Regards

Suresh Dhanaraj

Reply
0 Kudos
lakshya32
Enthusiast
Enthusiast

Hi

Welcome to the communities.

But I think its not safe or recommended procedure .

If its production environment then you should not follow this .

"When you fail to plan, you plan to   fail."
Reply
0 Kudos
LucD
Leadership
Leadership

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.NoTask
1Moving The VMs to the   transport DatastoreMove-VM
2Power off the VMShutdown-VMGuest
3Unregister the VM   from Source vCenter 4.0Remove-VM -DeletePermanently:$false
4Reregister the VM in   the destination vCenter New-VM -VMFilePath
5Changing the VM   Network to DHCP VLAN (Say VLAN 15)Set-NetworkAdapter
6Powering on the VMStart-VM
7Upgrade the VMware   toolsUpdate-Tools
8Reboot the VMRestart-VMGuest
9shutting down the VM’s to upgrade the VM hardware versionShutdown-VMGuest +
10Upgrade the VM hardware versionSet-VM -Version
11Powering on the VMStart-VM
12Reboot again it if   the hardware upgrade requires rebootRestart-VMGuest
13ipconfig/release and   Ipconfig/renewInvoke-VMScript
14Move the VM from   Transport LUN to the Normal VMFS datastoreMove-VM


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
LucD
Leadership
Leadership

I would be curious to know why this procedure should not be considered "safe" ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
SureshDhanaraj
Contributor
Contributor

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

Reply
0 Kudos
LucD
Leadership
Leadership

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

Reply
0 Kudos
SureshDhanaraj
Contributor
Contributor

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

Reply
0 Kudos
SureshDhanaraj
Contributor
Contributor

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:

  1. The output (VMX location) is not get appended with the export-csv
  2. For VMFS datastore .vmx path updated in the export-csv but if we use NFS datastore .vmx path not getting updated

Script2 Challenges:

  1. Since we have moved the VM to a different datastore during start-vm it’s prompting for answering (copied-it or moved-it)
  2. VMware tools is not getting updated throwing error and the status always says guestToolsNotRunning in PowerCLI but vCenter I could see the tools in Running status
  3. Due to the above tools issue we are unable to send Shutdown-VMGuest instead we have to use Stop-VM

Regards

Suresh Dhanaraj

Reply
0 Kudos
LucD
Leadership
Leadership

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

Reply
0 Kudos
SureshDhanaraj
Contributor
Contributor

Hi LucD & Team,

I have modified the script given by you depending upon my requirement. It’s 99% completed except the above challenges/issues.

  1. Apart from the can we have one {if , else argument  or any logic} to validate is the VMware tools on the $vm is the current version (8389) or not. If it’s the current version it has to skip the tools installation else it has to install it and proceed further.
  2. Update-tools throwing me error in this script can we have any other robust method to update the VMware tools

Could you please help me ?

Regards

Suresh Dhanaraj

Reply
0 Kudos
SureshDhanaraj
Contributor
Contributor

Sorry just now i received your mail. Let me take look.

Thanks again.

SureshD

Reply
0 Kudos
SureshDhanaraj
Contributor
Contributor

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

Reply
0 Kudos
SureshDhanaraj
Contributor
Contributor

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 Dhanaraj
Reply
0 Kudos
LucD
Leadership
Leadership

I 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

Reply
0 Kudos
SureshDhanaraj
Contributor
Contributor

Oh got it LucD. Thanks again. Let me try and update you.

Regards

Suresh Dhanaraj

Reply
0 Kudos
SureshDhanaraj
Contributor
Contributor

LucD,

Is there in any suitable place to put ‘-RunAsync’ in script 1 or 2 to reduce the timeline futher?

Regards

Suresh Dhanaraj

Reply
0 Kudos