Search code examples
powershellvmwarepowershell-3.0powercli

Remove an entry from VM Advanced Config via PowerCLI and ReconfigVM


http://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.VirtualMachine.html#reconfigure

I'm writing a script to update the VMX state for virtual machines to comply with some standards, but also want to backup their state in case they need to re restored. The ReconfigVM task works fine, and I can pull the state via ($VM | Get-View).config.extraconfig

What I can't figure out is how to remove items using VirtualMachineConfigSpec.

Let's say I update a value as such.

$ExtraOptions = @{
    "isolation.device.connectable.disable"="true";
    "log.rotateSize"="100000";
}

$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
Foreach ($Option in $ExtraOptions.GetEnumerator()) {
    $OptionValue = New-Object VMware.Vim.optionvalue
    $OptionValue.Key = $Option.Key
    $OptionValue.Value = $Option.Value
    $vmConfigSpec.extraconfig += $OptionValue
}
# ...

foreach($vm in $vms){
    $vm.ReconfigVM($vmConfigSpec)
}

Now let's say I create a backup hashtable of the config spec for the values prior to changing them. Since these values did not exist prior to the update there's no value prior.

A $null throws an error.

"isolation.device.connectable.disable"=$null;

An empty "" does no update

"isolation.device.connectable.disable"="";

I could simply revert the Boolean values and set integer values to 0, but I'd rather just remove the value from the configuration.

The documentation states the following about extraconfig

Additional configuration information for the virtual machine. This describes a set of modifications to the additional options. An option is removed if the key is present but the value is not set or the value is an empty string. Otherwise, the key is set to the new value.

Configuration keys that would conflict with parameters that are explicitly configurable through other fields in the ConfigSpec object are silently ignored.

EDIT

Passing a whitespace character (i.e. " ") will set the value to false or 0.


Solution

  • Apparently you'd have to download the vmx file (where these options are stored), and then re-upload the modified file while the VM is powered off, and maybe also un-registered.