page contents

Tuesday, June 11, 2013

Changing Displaynames for Linked Clones on VMware Workstation

While working on VLAB2GO, I had completed the base sysprepped image of Windows Server 2012 and had my customization scripts done for the nodes. Easy stuff with PowerShell, actually. (*hint: look in the DISTR\Scripts directory in the VLAB!)

I wanted to automate the creation of the linked clones as well as add the portable environment to the Workstation interface. The challenge? The official supported method of modifying the VMX file (that contains, among other things, the display name) involved a heavy add-on package that I'd rather not have my customers have to install. So how could I modify a VMX file correctly leveraging PowerShell?

The answer? A simple batch file and Powershell script! The batch file leverages VMRUN - the command line utility that is installed with VMware Workstation. I use it to create linked clones from a snapshot that I took of the sysprepped base image called "Base". The command line is super simple:

REM Create Linked Clones
"%ProgramFiles(x86)%\VMWARE\VMware Workstation\vmrun" clone .\VLab2Go0.2.vmx .\DC01\DC01.vmx linked Base

And so on to create the 7 linked clones for the VLAB2GO Domain environment. The problem? The display name of each of the servers display in the GUI as "Clone of VLab2Go0.2".

That sucks! I want the display names to reflect the names of the hosts that they will grow up to become! The mighty DC01 (iSCSI target by day, Domain Controller at night!), NODE01 (the plucky potential SQL server, or humble Exchange CAS server)...

The workaround? A really easy line of Powershell that replaces the default string to what I actually wanted:

Code:

(get-content $pwd\DC01\DC01.vmx) | foreach-object {$_ -replace 'displayName = "Clone of VLab2Go0.2"','displayName = "DC01"'} | set-content $pwd\DC01\DC01.vmx
(get-content $pwd\NODE01\NODE01.vmx) | foreach-object {$_ -replace 'displayName = "Clone of VLab2Go0.2"','displayName = "NODE01"'} | set-content $pwd\NODE01\NODE01.vmx
(get-content $pwd\NODE02\NODE02.vmx) | foreach-object {$_ -replace 'displayName = "Clone of VLab2Go0.2"','displayName = "NODE02"'} | set-content $pwd\NODE02\NODE02.vmx
(get-content $pwd\NODE03\NODE03.vmx) | foreach-object {$_ -replace 'displayName = "Clone of VLab2Go0.2"','displayName = "NODE03"'} | set-content $pwd\NODE03\NODE03.vmx
(get-content $pwd\NODE04\NODE04.vmx) | foreach-object {$_ -replace 'displayName = "Clone of VLab2Go0.2"','displayName = "NODE04"'} | set-content $pwd\NODE04\NODE04.vmx
(get-content $pwd\NODE05\NODE05.vmx) | foreach-object {$_ -replace 'displayName = "Clone of VLab2Go0.2"','displayName = "NODE05"'} | set-content $pwd\NODE05\NODE05.vmx
(get-content $pwd\NODE06\NODE06.vmx) | foreach-object {$_ -replace 'displayName = "Clone of VLab2Go0.2"','displayName = "NODE06"'} | set-content $pwd\NODE06\NODE06.vmx

Voila! I got the results I wanted and everything works great! I had a scripted install of the linked clone environment, and when I add them to the VMware Workstation GUI they will look right without all of those pesky right-clicks.

Obviously, a similar mechanism could be used for other elements in the VMX File. All code is offered as is, drive responsibly, yada yada.

Stay thirsty, my friends.

No comments: