VMware Cloud Community
gary1012
Expert
Expert
Jump to solution

Scripted edits of the vmx file?

I have a number of existing VMs that I'd like to add some advanced configuration settings. Rather than using the GUI to add the values I'd like to automate the process. My plan is to write a script that powers off the VM and adds the new values (isolation.tools.x or isolation.device.x) to the vmx file(s). Is this safe/advisable to do via a script? Does anyone have an existing sample script they'd be willing to share?

Community Supported, Community Rewarded - Please consider marking questions answered and awarding points to the correct post. It helps us all.
Reply
0 Kudos
1 Solution

Accepted Solutions
Craig_Baltzer
Expert
Expert
Jump to solution

If you want to do it with just the VI Toolkit itself, then you'll need to do something like this

$vm = Get-View (Get-VM NameofVMtoUpdate).ID
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.extraconfig = New-Object VMware.Vim.optionvalue
$vmConfigSpec.extraconfig[0].Key="
lefthandsideofentry"
$vmConfigSpec.extraconfig[0].Value="
rigthhandsideofentry+"
$vm.ReconfigVM($vmConfigSpec)

Where

  • NameofVMtoUpdate is the name of the VM you want to update (from the vCenter inventory)

  • lefthandsideofentry is the advanced configuration entry you want to set

  • rigthhandsideofentry is the value for the advanced configuration entry

As an alternative some of the folks that hang out over in the VI Toolkit area have put together a set of Powershell extensions for the VI toolkit available from CodePlex (). Set-TkeVmx will let you set values (basically the same code as above just rolled into a function to keep your code cleaner)

View solution in original post

10 Replies
kjb007
Immortal
Immortal
Jump to solution

If you're only adding additonal values to the vmx files, it is fairly simple to do. The problem I've seen when hand-editing the vmx files, is that your changes aren't always seen right away. I've found the only sure way to make sure your config change takes hold right away, is to remove the vm from inventory, and to re-add/register the vm back to where it was. That seems to replace whatever caching mechanism is used for the vm configs.

Otherwise, it would be more advisable to use a powershell/perl toolkit's to perform the task. How, is a question for a different forum, but you should be able to set that value fairly easily using the toolkits, as opposed to editing vmx files directly.

-KjB

vExpert/VCP/VCAP vmwise.com / @vmwise -KjB
gary1012
Expert
Expert
Jump to solution

Thx. kjb. Do you happen to know what cmdlets would be used with Powershell?

Community Supported, Community Rewarded - Please consider marking questions answered and awarding points to the correct post. It helps us all.
Reply
0 Kudos
kjb007
Immortal
Immortal
Jump to solution

Off-hand, no.

I would ask that question here: http://communities.vmware.com/community/developer/windows_toolkit

-KjB

vExpert/VCP/VCAP vmwise.com / @vmwise -KjB
Reply
0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

As kjb007 stated, changes will not take place right away when you manually edit .vmx file as it's not loaded up while the VM is still running or even if it's powered off for that matter.

Without having to go through the process of unregistering/modify/re-register you can utilize "vimsh" and depending on what version of ESX/ESXi there will be different versions of the command, take a look here to figure out which version you have: http://engineering.ucsb.edu/~duonglt/vmware/#vmware_vimsh

Now onto a possibly scripted solution, if you plan on powering down the VM and making the changes to the .vmx file using sed or evening just appending the appropriate lines, you can use the VmId of the VM you've modified and just run a reload function from "vimsh".

Steps:

  • Power down VM

  • Modify .vmx file

  • Lookup VmId for your VM display name

vmware-vim-cmd vmsvc/getallvms | grep VM_NAME" | awk '{print $1}'

  • Take this VmId and run the following to reload the configuration file

vmware-vim-cmd vmsvc/reload VmId

  • Power on VM and the changes should take affect

-


Here is an example of what the output would look on ESX 3.5u3:

# vmware-vim-cmd vmsvc/getallvms | grep "William-XP" | awk '{print $1}'

1072

# vmware-vim-cmd vmsvc/reload 1072

#

Knowing this, you can easily script something that'll automate this for you given a list of VM(s). Hope this helps, the other solution is looking at Powershell or VI Perl Toolkit

=========================================================================

--William

VMware ESX/ESXi scripts and resources at:

gary1012
Expert
Expert
Jump to solution

Thx lamw. I'll give this a try too. Also, I stumbled across this LINK that might cure my issues too. Appreciate all the feedback...

Community Supported, Community Rewarded - Please consider marking questions answered and awarding points to the correct post. It helps us all.
Reply
0 Kudos
Craig_Baltzer
Expert
Expert
Jump to solution

If you want to do it with just the VI Toolkit itself, then you'll need to do something like this

$vm = Get-View (Get-VM NameofVMtoUpdate).ID
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.extraconfig = New-Object VMware.Vim.optionvalue
$vmConfigSpec.extraconfig[0].Key="
lefthandsideofentry"
$vmConfigSpec.extraconfig[0].Value="
rigthhandsideofentry+"
$vm.ReconfigVM($vmConfigSpec)

Where

  • NameofVMtoUpdate is the name of the VM you want to update (from the vCenter inventory)

  • lefthandsideofentry is the advanced configuration entry you want to set

  • rigthhandsideofentry is the value for the advanced configuration entry

As an alternative some of the folks that hang out over in the VI Toolkit area have put together a set of Powershell extensions for the VI toolkit available from CodePlex (). Set-TkeVmx will let you set values (basically the same code as above just rolled into a function to keep your code cleaner)

petedr
Virtuoso
Virtuoso
Jump to solution

If you want more information on scripting with 'vimsh' Xtravirt has a excellent document on it.

http://knowledge.xtravirt.com/white-papers/scripting.html

www.thevirtualheadline.com www.liquidwarelabs.com
Reply
0 Kudos
richard_judd
Contributor
Contributor
Jump to solution

does anyone know how to do the same actions in either c# or vb.net?

my attempt so far (falls over on the red highlighted line):

Dim extraConf() As VMware.Vim.OptionValue = Nothing numDisplaysKey = "Svga.numDisplays" numDisplaysValue = "2"

extraConf(0).Key() = numDisplaysKey

extraConf(0).Value = numDisplaysValue info = New VMware.Vim.VirtualMachineConfigSpec info.ExtraConfig = extraConf

Reply
0 Kudos
jawadbutt
Contributor
Contributor
Jump to solution

Tried this and working in C# today. Posting for reference use.

        public static void InsertExtraConfigElement(VirtualMachine vm, string argument, string value)

        {

            vm.ReconfigVM(new VirtualMachineConfigSpec

            {

                ExtraConfig = vm.Config.ExtraConfig.Concat(new OptionValue[] { new OptionValue() { Key = argument, Value = value } }).ToArray()

            });

        }

Calling the above method from other places in this fashion:

VMwareUtilities.InsertExtraConfigElement(virtualMachineObject, "isolation.tools.diskWiper.disable", "true");

Reply
0 Kudos
Faizan2
Contributor
Contributor
Jump to solution

@gary1012  @kjb007 

if I want to change uuid.bios setting of my VMX file.

If I have uuid.bios="xxx xxx"  and I want to overwrite it static to uuid.bios = "42 08 fa 23 c6 14 3a d2-f7 71 be 21 5d 7e c8 51"  always . 

can I achieve with same command in powershell as below

 

 $vm = Get-View (Get-VM $vmName).ID
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.extraconfig += New-Object VMware.Vim.optionvalue
$vmConfigSpec.extraconfig[0].Key="uuid.bios"
$vmConfigSpec.extraconfig[0].Value="56 4d 7f ab 8d 53 6c 37-f9 2e 48 89 8f a7 54 56"
$vm.ReconfigVM($vmConfigSpec) 

 

 

Reply
0 Kudos