Do you ever rename a server? Sure, in Windows that’s pretty easy. But having virtualized most of your servers, renaming brings along some new challenges.

When you first create a virtual machine, you enter a name for the machine. But that’s before you even started installing Windows on it. You might not even use the same names. Sure, the name in the VI Client is only a label and can easily be changed, even while the vm is running. So, no problems there? Look again. Have you ever tried browsing one of your datastores looking for the files that belong to some vm? Simple enough, as each vm gets its own folder, which is named after… the vm, obviously. But that folder is created when the vm is created. And when you rename the vm, guess what happens to the folder name: nothing.

Now, I don’t even want to begin to imagine the things that could go wrong when you’re browsing your datastores looking for those files, whether you are looking to copy them, remove them, … My advise would be to take a look at the vm settings, which lists the locations of the vmdk files ánd double-check the modified dates of the files you are touching to see whether they correspond to what you’d expect.

Although we should always keep checking and double-checking, it is still a good idea to try and keep your naming as consistent as possible. You never know whether somebody else will be checking as thoroughly as you are. And that’s where the Helpful Script of the Day comes in. This Powershell script, using the VI Toolkit, lists all your vm’s names, the windows host names (as reported back by the VMware Tools), plus the names of the folders and files this vm is using (for up to three disks). The output is sent to a csv file in a location you specify. I recommend opening it in Excel, which generates a nice tabular view and – if available in your version of Excel, using the feature Conditional Formatting to highlight duplicate values. All the values that are inconsistent should then stand out like a Dutchman in China.

Check-Names (Rename to .ps1)

 Any questions or remarks? Don’t hesitate to comment. I’ll be reading and trying to respond as soon as possible.



PS: I hit over 1000 visits this month, while the counter stuck at 130 just two months ago! I would like to thank you all for checking out my blog. Hope it’s proving useful to you.


Tagged with:

12 Responses to Are your names in sync? (Helpful Script of the Day)

  1. admin says:

    Before you ask: “But how can I correct all the errors I find?”, here’s one way to do it (use at your own risk):

  2. BrianRTS says:

    I get this error when running the script against the GA VItoolkit…

    You cannot call a method on a null-valued expression.
    At line 12, position 45
    $myObj.HostName = $VM.Guest.HostName.Split(“.”)[0].ToUpper()

  3. admin says:

    That error message indicates the $VM.Guest.HostName property is empty. I’m guessing your VM is turned off or the VMware Tools are not installed.

  4. ThomasV says:

    Actually if you perform a storage vmotion, the resulting VM also is renamed and uses the VI inventory name (the one you see in the VI client) as the base for it’s folder, vmx, vmdk files,… Without having to bring the machine down… Very error prone compared to editing the vmx and vmkfstool -e.

  5. admin says:

    Hi ThomasV,

    That would be a nice solution! Indeed less error-prone than editing files manually.

    Thank you for your contribution.

  6. [...] If you want more advanced description synching, check out the full script. [...]

  7. Matt says:

    i get the same error as brianRTS – looks like get-vm is only populated by the following info ?.

    ps1> get-vm vm1
    Name PowerState Num CPUs Memory (MB)
    —- ———- ——– ———–
    vm1 PoweredOn 1 4096

  8. admin says:

    Take a look at (Get-VM vm1).Guest and it’s subproperties. If they are empty, the vm was powered down, or the VMware Tools were not running.

  9. Matt says:

    ok so if your Vm’s only have one disk then you get the following error
    You cannot call a method on a null-valued expression.
    At :line:13 char:54
    + $myObj.Disk2Folder = $VM.HardDisks[1].FileName.Split( <<<< ” /.”)[1]
    if i take these lines out the script reports the correct infomation

  10. admin says:

    Allow me to translate that error. It says it cannot call the method Split (as indicated by the <<<<) on the object $VM.HardDisks[1].FileName because it is empty. This is true, because the $VM.Harddisks array only contains one object, referenced by $VM.HardDisks[0]. The result is that the output for disk 2 and 3 will be empty.
    If your script stops when encountering an error, add the following line to the start of the script (to tell the script to report the error, but continue to run):
    $errorActionPreference = “Continue”

  11. Craig says:

    I added a simple error skiping routine to clean things up.

    Make the first line of the script: $errorActionPreference = “Stop”

    In the ForEach loop add the trap {continue} statement after the last file and before the counter as shown:

    $myObj.Disk3File = $VM.HardDisks[2].FileName.Split(” /.”)[2]
    $myCol += $myObj

    When the script encounters a null error, it will jump to the trap and then just continue to the next VM in the array.

  12. I do agree with all of the concepts you have offered for your post. They’re really convincing and will certainly work. Nonetheless, the posts are too quick for novices. May just you please lengthen them a bit from subsequent time? Thanks for the post.

Leave a Reply