image

@bjornbats asked me if I knew a way to add multiple values to a VMX file via PowerCLI. I remembered a blog post from Carter at http://blogs.vmware.com/vipowershell/ about this subject. So I tested the code and changed it a little bit. Now you can create a CSV file with the Keys and Values you want to change and the script will add them to the VMX file.

The CSV file I used looks like this:

Key,Value
isolation.tools.copy.disable,true
isolation.tools.diskShrink.disable,true
isolation.tools.diskWiper.disable,true
isolation.tools.paste.disable,true
isolation.tools.setGUIOptions.enable,true
log.keepOld,10
log.rotateSize,100000

The script below will add the entry’s from the CSV file to every VMX file, even when the VM is still running. I don’t know if you need to reboot the VM to apply the changes. So if someone can test that for me, please leave a comment with the results.

$vCenter = Read-Host "Enter your vCenter servername"
Connect-VIServer $vCenter

$import = Import-Csv "C:\Scripts\PS\vmxsettings.csv"

$vms = Get-View -ViewType VirtualMachine | where {-not $_.config.template}

$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec

    foreach($item in $import){

        $extra = New-Object VMware.Vim.optionvalue
        $extra.Key=$item.Key
        $extra.Value=$item.Value
        $vmConfigSpec.extraconfig += $extra
    }

    foreach($vm in $vms){

        $vm.ReconfigVM($vmConfigSpec)
    }

Disconnect-VIServer –Confirm:$false

When you start the script, you will notice that vCenter is very busy reconfiguring the virtual machines:

image

You will notice The operation is not… warning in the recent tasks window. This is because the Get-View -ViewType VirtualMachine will also add the Templates.

And here you can see the added lines into your VMX file:

image

16 thoughts on “PowerCLI: Add multiple values to a VMX file

  1. Arne, nice script and concept.

    I would like to propose a few performance improvements.

    1) Use the Get-View cmdlet instead of the Get-VM cmdlet.
    It’s a lot faster
    2) The VirtualMachineConfigSpec is the same for each guest.
    By taking it outside the loop through the guests, you only need to populate the object once.
    3) You can specify an array of optionvalue objects in the extraconfig property.
    That way you need to call the ReconfigVM method only once for each guest.

    The script would then look something like this:

    $vCenter = Read-Host “Enter your vCenter servername”

    $import = Import-Csv “C:\Scripts\PS\vmxsettings.csv”
    $vms = Get-View -ViewType VirtualMachine

    $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
    foreach($item in $import){

    $extra = New-Object VMware.Vim.optionvalue
    $extra.Key=$item.Key
    $extra.Value=$item.Value
    $vmConfigSpec.extraconfig += $extra
    }

    foreach($vm in $vms){

    $vm.ReconfigVM($vmConfigSpec)
    }

    Disconnect-VIServer – Confirm:$false

  2. hi Arne, bjorn bats, LucD and Bouke , Could you share what are some of the common reasons you have found yourself to edit the vmx file or add newer lines to it. From a tracking perspective, what vmx configurations would you like to track and see a simple report on?
    thanks

  3. Arne and LucD, This script is awesome. I am new to powershell and am looking to modify this so I can update either 1 server at a time to verify or identify all servers in a cluster and update only those versus all vms at once…. Can anyone help?

  4. Can I add/change Configuration Parameters like this using PowerCLI commands? I can’t see this exposed ‘direct’.

    1. Figured it out right after i posted…for others that may want to know

      “Get-Folder -Name Folder | Get-VM -Name vmname | New-AdvancedSetting -Name “keyboard.typematicMinDelay” -Value 2000000 -Confirm:$false -Force “

  5. I am able to set the value’s as per this post, but i would like to be able to read the values of each of the key’s back. i tried the Get-TkeVmxEntries but it seems to be a broken function in that it will not accept any Datastore path only local. I am able to do a Copy-TkeDatastoreFile to my local workstation and then parse through it, but I would like to be able to do this inline not copying files back and forth to ensure they were set, and I also require this for audit purposes.

    Thenaks for any help

  6. You do need to power off / on the VM(s) for these settings to take effect. The .vmx file is read into memory upon VM startup. Rebooting the guest OS isnt enough.

  7. Hi there… I’m in a need to change a KEY that as added from a similar script previously that I run. Turns out the script has a typo and I end up with a invalid entry….just ignored by my vms, but I don’t want them to be left there, you know…
    Any help would be appreciated!

    Thanks

  8. Hello together,

    Does anybody know why the script does not run in my environment?

    _____

    $vCenter = Read-Host “Enter your vCenter servername”
    Connect-VIServer $vCenter

    $import = Import-Csv “C:\advanced_values.csv”

    $vms = Get-View -ViewType VirtualMachine

    $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec

    foreach($item in $import){

    $extra = New-Object VMware.Vim.optionvalue
    $extra.Key=$item.Key
    $extra.Value=$item.Value
    $vmConfigSpec.extraconfig += $extra
    }

    foreach($vm in $vms){

    $vm.ReconfigVM($vmConfigSpec)
    }

    ____________________________________

    This is the exception:

    Exception calling “ReconfigVM” with “1” argument(s): “The operation is not supp
    orted on the object.”
    At C:\advanced_values.ps1:20 char:23
    + $vm.ReconfigVM <<<< ($vmConfigSpec)
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

    This is my csv content

    Key,Value
    isolation.tools.diskWiper.disable,TRUE
    isolation.tools.diskShrink.disable,TRUE

    Thank you all very much in advance
    Timo

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.