lunedì 30 dicembre 2019

VMWare VSPhere - RENAME VIRTUAL MACHINE and it's files (rename multiple file using PowerCLI )

WARNING: play with VM files could damage the virtual machine itself and make it unusable, do it at your own risk and only if you know what you are doing! (better if you have a backup!)

If you want to rename a virtual machine, you can simply do it from the web interface.
The problem with this is that you'll obtain the new name in the vcenter web console but then the folder on the disk will remain with the old name and also the files inside will remain with old names and this could be confusing and lead to errors, we need a way to maintain consistency.

The safest and best way is to do a cold vmotion to a different datastore. During the migration vsphere will fix folder name and files name to match the new name.
But there are times when you cannot or don't want to use this approach.
For example if you have a really big vm that would require a lot of time to move and you cannot keep it powered down too much time. Or maybe you don't have another datastore where to move the machine, or you don't have enough space on another datastore and so on.

Another way to fix the name mismatch is to manually edit folder name, files name and their content to match the new name.
This approach is RISKY! if you make some mistake you'll break down your machine and make it unusable!
I suggest you to first try these steps on a small test machine to better understand how it works, what you can and cannot do and to get confortable with the steps and their results.

Some notes:
1. the vm must be turned off during these steps
2. the vm must be removed from the vcenter inventory
3. it's better to NOT HAVE any snapshot in place, snapshot files make your life harder and more prone to errors and issues, so please delete every snapshot from the vm before proceed if you can

Here the general idea to rename virtual machine and it's files:
1. turn off VM
2. delete all the snapshots (if you can do it )
3. check from the config where the vm is located on the datastore (the path)
4. remove the vm from the vcenter inventory
5. rename the vm folder
6. rename all the files to reflect the new vm name
7. edit the .vmx and all the (small) .vmdk files in order to replace the old name with the new one
(do the same for the .vmsd file if you didn't delete all the snapshots), don't try to edit .vmsn files or other file extensions, they are binary and not editable
8. from the vcenter web interface browse the datastore, right click on the .vmx file and "register vm" with the new name
9. power on the vm to check if everything is fine

In case you didnt' removed the snapshot before the rename task, you could have surprises now, especially if you now revert to one of the snapshot. We didn't edit the binary snapshot files, so reverting to them will put the old name again in production somewhere, so in this case is better after the revert to check again all the files content and rename the old machine name with the new one where this applies.

RENAMING MULTIPLE FILES BY COMMAND

WARNING: manually touching vm files could corrupt the VM and make it unusable!

Instead of renaming the files one by one you could do it using a command.

There was a time when in VSPhere it was possible to rename multiple files from the shell using the command "RENAME" . It was a Perl command.

Now I'm using VSPhere 6.5 and it seems this command is not available anymore.

I didn't want to find a way to use Perl again, my idea was to find a new way, already available to do the same job.
So I find a simple way is using PowerCLI

Get the installation package from vmware download page and install it on your PC.

Here the idea :

FIRST TURN OFF THE VIRTUAL MACHINE (else the files will be locked as in use)

1. run powercli
2. connect to vcenter
3. go to vmstore disk
4. change working folder to the VM one (where the files to be renamed are located )
5. run the powershell command to rename the files

Now the detailed tasks with commands:

1. run powercli
2. Connect-VIServer (asks vcenter IP and credentials)
3. cd vmstore:
4. cd .\datacentername\datastorename\machinename

type ls to list the folder content

Now, let's say the machine is called SERVER01 , then the folder content would be something like this:

Name                       Type                 Id
----                       ----                 --
SERVER01.nvram             DatastoreFile
SERVER01.vmx               DatastoreFile
SERVER01-aux.xml           DatastoreFile
SERVER01.vmxf              DatastoreFile
SERVER01-flat.vmdk         DatastoreFile
SERVER01.vmsd              DatastoreFile
SERVER01.vmdk              DatastoreFile
vmware-3.log               DatastoreFile
vmware-2.log               DatastoreFile
vmware-1.log               DatastoreFile
vmware.log                 DatastoreFile

Now, we want to rename all those SERVER01* files into SERVER02* , type the command:

5. ls SERVER01* | rename-item -newname {$_.name -replace 'SERVER01','SERVER02'}

The "ls SERVER01*" is used to get the list of files to be renamed, we are interested just in files starting with SERVER01

the rename-item is the powercli command to rename a file

then we need to pass the new filename to the -newname parameter

to create the new name we use the actual name ( $_.name ) where we replace the string SERVER01 with the string SERVER02

This command will iterate between all the files returned by the ls command and one by one rename the file.

Please remember that if you rename the VM files like this, you need also to edit .vmx file and all of the small .vmdk files in order to fix the filenames inside, to not broke the references.
See below what I mean in detail

good luck