mark_chuman
Hot Shot
Hot Shot

Script to migrate cluster from one vCenter to another vCenter

Attached is a script I use to migrate an ESXi cluster from one vCenter to another.  I'll be updating the script (will add VM folder locations and attributes, more qa, root credential storage - PASSWORD IS STORED IN TRANSCRIPT FILE - so, adjust this in script or cleanse transcript after run), but for now it handles HA settings, DRS settings, EVC, DRS vm rules (affinity/anti), folder import/export, permissions (datacenter, cluster, host, folder and vm) and maybe some other things I left out.  Does not deal with dvs as we use local vswitches.

You'll need two VCs and a cluster you want to migrate.  The script does not create the datacenter in the destination VC, but it will create the cluster, disconnect, remove an add the hosts.

I hope it can help someone as I've mainly been on the receiving end with regards to help from this forum Smiley Happy.  Special note to Luc for a lot of the items in the script (folder functions, splat etc Smiley Happy).

Just unzip to a directory and run with powercli (requested credentials must have VC privs to disconnect hosts etc.).

As always with offerings, use at your own risk!

*Using to go from 5.0 to 5.5.

90 Replies
LucD
Leadership
Leadership

Great, thanks for sharing.


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

0 Kudos
munishpalmakhij
Contributor
Contributor

Thanks for sharing wonderful script , I have a similar requirement hope we can work together to achieve this

Here is the detailed requirement

We have shared pool of ESXi Servers which we use for many customers (we provide DR as a Service using SRM )

We have dedicated vCenter and SRM Server for each and every customer

Currently we have to manually remove/configure when we move around servers between customer

I would want to write script to perform following steps

  • Disconnect ESXi from vCenter1
  • Create new ESXi Management Port Group
  • Modify the DNS Name for ESXi (Probably using CSV File, Assumption DNS entries are present already)
  • Remove existing VM Network Port Groups
  • Remove the exisiting ESXi Management Port Group
  • Connect ESXi to vCenter 2 using new ESXi Management Port Group (using DNS or IP )
  • Apply Host Profiles with Port Group Details or Create Port Groups Manually using csv file
  • Add host to existing cluster
0 Kudos
Madmax01
Expert
Expert

Hello,

until now i used the InventorySnapshot from the Vmware Labs.  and was working very well so far. also with 5.1 > 5.5.

Will have a look to the script. Idea behind is very very interesting. Everybody knows the big effort in the Back once migrating to new vCenter. theirefore i don't understand why vmware don't deliver an official Solution. Shouldn't be such a hard progress for them.

Best regards

Max

0 Kudos
mark_chuman
Hot Shot
Hot Shot

Thanks.  The script has been working well for us so far.  I am working on adding in host profiles, but am not sure yet about the differences between host profile usage in 5.1 vs 5.5.  You want to just change the name of the ESX server, not the actual DNS record for the ESX server correct?

0 Kudos
mark_chuman
Hot Shot
Hot Shot

Completely agree about VMware formally supporting a migration tool.  I tried to start as well with the inventorysnapshot tool, which is awesome, but I would randomly hit errors in the script during migrations and it took a lot of work to figure out what exactly was going wrong.  I was very interested in understanding exactly what's going on at any point during the script run and I just couldn't get that with the vmware labs provided script.  I'm currently working on an audit script that will check around 40 or 50 configuration details within our vCenter servers.  We'll probably dump it to Splunk to incorporate with our other vCenter related trending data.

0 Kudos
JohnMcKeown
Enthusiast
Enthusiast

Very nice script. Just tested 5.1 to 5.5 worked as stated.

Only negative is that it didn't import the resource pools. But did take a huge amount of work out of the migration.   

Oh forgot to add, the root password read host didnt seem to work so manually edited the script with a default on.

0 Kudos
mark_chuman
Hot Shot
Hot Shot


Really glad it helped John.  I've got a new version which I'll upload soon after I "sanitize" it for public use.

0 Kudos
mark_chuman
Hot Shot
Hot Shot

Here is the latest version.  Not exactly sure what's different from the one I originally posted, but there are some new items.  Here are some notes.

Notes on script:

- Handles root password more securely.

- There are pauses in the script.  Just adjust the seconds in those sections or comment out all together if you don't want pauses.

- No more errors at empty DRS rules.

- This part renames the old cluster.  I have it currently commented out.  Customize and uncomment to use:

Set-Cluster -Cluster $Cluster -Name $Cluster" - Migrated to https://newvcenter.company.com/" -Confirm:$false

- This part emails out reporting on migration activities.  I have it currently commented out.  Customize and uncomment to use:

$CSVhosts = Import-Csv .\$SourcevCenter\$Cluster\VMHost.csv | out-string

Send-MailMessage -To "SOMEONE <SOMEONE@SOMEWHERE.com>", "SOMEONE2 <SOMEONE2@SOMEWHERE.com>" `

-From "ME <ME@SOMEWHERE.com>" -subject "Notice - ESXi Servers Migrated" `

-smtp smtp.server.com -body "$CSVhosts Source vCenter $SourcevCenter

Destination vCenter $DestinationvCenter" | out-null

0 Kudos
kiraa
Enthusiast
Enthusiast

Great script.

I have not used this in a production environment but have used in a lab and it works well.

Jake

0 Kudos
mark_chuman
Hot Shot
Hot Shot

Glad you like it.  Try the new version (attached).  Working towards trying to have it fully automated (ie, no user input), but this version hits a csv with the information, so you only have to enter credentials once and it will cycle through the different clusters in the CVS.  FYI, I've used it multiple times in production.

0 Kudos
iliketurbos
Enthusiast
Enthusiast

nice script but I don't see where it does any resources pool yet. for it to be totally useful that would be required.

but a great start so far!

0 Kudos
rnotaro
Contributor
Contributor

Trying to run this script and I'm getting this error

Get-View : Cannot validate argument on parameter 'VIObject'. The argument is null, empty, or an element of the argument collection contains a null value.

Supply a collection that does not contain any null values and then try the command again.

At C:\vcenter_migration\Functions\get_folder_path_function.ps1:46 char:25

+         $fld = Get-View $fld.Parent

+                         ~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

This happens right after it tried to export folders.  Any ideas?

0 Kudos
mark_chuman
Hot Shot
Hot Shot

Can you upload the transcript file? - Look it over first as it may contain some of your company data etc.

0 Kudos
rnotaro
Contributor
Contributor

**********************

Windows PowerShell transcript start

Start time: 20150122154328

Username  : XXXXXXXX

Machine  : XXXXXXXx (Microsoft Windows NT 6.1.7601 Service Pack 1)

**********************

Please confirm your information before proceeding

Target cluster         - XXXXX

Source vCenter         - XXXXXXSource Datacenter      - XXXXXXX

Destination vCenter    - XXXXXX

Destination datacenter - XXXXXXX

Please confirm that the above information is correct and press any key to continue . . . 

Running QA tests against the information provided

Cluster XXXXX DOES exist in XXXXXXX

Checking connection to the ESX hosts with the root password provided

Datacenter XXXXXX DOES exist in XXXXXX

1

Exporting folders in source datacenter XXXXXXX

Action to take for this exception:

Cannot validate argument on parameter 'VIObject'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a

collection that does not contain any null values and then try the command again.

[C] Continue  [I] Silently Continue  [B] Break  [S] Suspend  [?] Help (default is "C"): c

Get-View : Cannot validate argument on parameter 'VIObject'. The argument is null, empty, or an element of the argument collection contains a null value.

Supply a collection that does not contain any null values and then try the command again.

At C:\vcenter_migration\Functions\get_folder_path_function.ps1:46 char:25

+         $fld = Get-View $fld.Parent

+                         ~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

Action to take for this exception:

Cannot validate argument on parameter 'VIObject'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a

collection that does not contain any null values and then try the command again.

[C] Continue  [I] Silently Continue  [B] Break  [S] Suspend  [?] Help (default is "C"): c

Get-View : Cannot validate argument on parameter 'VIObject'. The argument is null, empty, or an element of the argument collection contains a null value.

Supply a collection that does not contain any null values and then try the command again.

At C:\vcenter_migration\Functions\get_folder_path_function.ps1:46 char:25

+         $fld = Get-View $fld.Parent

+                         ~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

Action to take for this exception:

Cannot validate argument on parameter 'VIObject'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a

collection that does not contain any null values and then try the command again.

[C] Continue  [I] Silently Continue  [B] Break  [S] Suspend  [?] Help (default is "C"): b

Action to take for this exception:

Cannot validate argument on parameter 'VIObject'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a

collection that does not contain any null values and then try the command again.

[C] Continue  [I] Silently Continue  [B] Break  [S] Suspend  [?] Help (default is "C"): b

% : Cannot validate argument on parameter 'VIObject'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a

collection that does not contain any null values and then try the command again.

At C:\vcenter_migration\Functions\get_folder_path_function.ps1:38 char:15

+     $Folder | %{

+               ~~

    + CategoryInfo          : InvalidData: (:) [ForEach-Object], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.ForEachObjectCommand

PowerCLI C:\vcenter_migration>

0 Kudos
BrianOwen9
Contributor
Contributor

Mark, do you have any updates for your script since you last  uploaded on 11/3?

0 Kudos
mark_chuman
Hot Shot
Hot Shot

Attached is the latest version.  Can't remember exactly what's been added, but I do know, notes, attributes, folder permissions etc were added.  Just ran it in the lab and it ran fine. Also, have used it on 500+ hosts in production.

NOTES:

- Adjust the name of the vCenter if needed:

Set-Cluster -Cluster $Cluster -Name $Cluster" - Migrated to https://New_vCenter_WebAddress_Here/" -Confirm:$false

- Un comment this portion and enter your data to send an email notification at end of migration:

#Notify others of migration activities

$CSVhosts = Import-Csv .\$SourcevCenter\$Cluster\VMHost.csv | out-string

#Send-MailMessage -To "SOMEONE1 <someone1@somewhere.com>", "SOMEONE2 <SOMEONE2@fmr.com>" `

#-From "SENDER <SENDER@somewhere.com>" -subject "Notice - ESXi Servers Migrated" `

#-smtp smtp.fmr.com -body "$CSVhosts Source vCenter $SourcevCenter

#Destination vCenter $DestinationvCenter" | out-null

-Script no longer creates cluster in new location with EVC settings.  EVC settings are exported, but not imported.  We ran into majore issues trying to migrate over EVC settings, so removed it from the script.  See - http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=101286...

0 Kudos
mark_chuman
Hot Shot
Hot Shot

Can you try to run the latest version - attached to my latest reply today.

0 Kudos
iliketurbos
Enthusiast
Enthusiast

Mark would it be okay if I try to add resource pools?

0 Kudos
iliketurbos
Enthusiast
Enthusiast

It’ll be next week before I can try, but I’m going to do a few host. I’ll be doing over 6000 vms soon so I’ll be sure to let you know Smiley Happy

Thanks!

0 Kudos