Retrieve and Set VM Advanced Configuration (VMX) settings

Retrieve and Set VM Advanced Configuration (VMX) settings

UPDATE: From PowerCLI 5.1 these functions are no longer required as they have been included in the *-AdvancedSetting cmdlets.

The following functions are used to retrieve infromation from a VM or multiple VM's advanced configuration or VMX file and also used to Set a configuration for a single or multiple VMs.

Note: The first function was taken from the "VMware vSphere PowerCLI Reference" book which can be found here: http://PowerCLIBook.com

function Set-VMAdvancedConfiguration { 
<#
.SYNOPSIS
  Sets an advanced configuration setting (VMX Setting) for a VM
  or multiple VMs
.DESCRIPTION
  The function will set a VMX setting for a VM
  or multiple VMs
.NOTES
  Source:  Automating vSphere Administration
  Authors: Luc Dekens, Arnim van Lieshout, Jonathan Medd,
           Alan Renouf, Glenn Sizemore
  Adjusted: 07 June 2012 by Alan Renouf to accept a list of options
.PARAMETER VM
  A virtual machine or multiple virtual machines
.PARAMETER Key
  The Key to use for the advanced configuration
.PARAMETER Value
  The value of the key
.EXAMPLE 1
  PS> Set-VMAdvancedConfiguration -key log.rotatesize -value 10000
.EXAMPLE 2
  PS> $file = Import-Csv c:\tmp\Settings.txt -Header Key,Value
  PS> Set-VMAdvancedConfiguration -vm $VM -OptionList $file
#>

  param(
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
      $vm,
      [String]$key,
      [String]$value,
      [Array]$OptionList
      )

  process{
    $vmConfigSpec = new-object VMware.Vim.VirtualMachineConfigSpec
    If ($OptionList) {
        $OptionList | Foreach {
            $Values = new-object vmware.vim.optionvalue
            $Values.key=$_.key
            $Values.value=$_.value
            $vmConfigSpec.ExtraConfig += $Values
            Write-Host "Adding $($_.Key) = $($_.Value)"
        }
    } Else {
        $vmConfigSpec.ExtraConfig += new-object VMware.Vim.OptionValue
        $vmConfigSpec.ExtraConfig[0].key = $key
        $vmConfigSpec.ExtraConfig[0].value = $value
        Write-Host "Adding $Key = $Value"
    }
    foreach ($singlevm in $vm) {
      $Task = ($singlevm.ExtensionData).ReconfigVM_Task($vmConfigSpec)
      Write "Set Advanced configuration for $($singleVM.Name)"
    }
  }
}

function Get-VMAdvancedConfiguration {
<#
.SYNOPSIS
  Lists advanced configuration setting (VMX Setting) for a VM
  or multiple VMs
.DESCRIPTION
  The function will list a VMX setting for a VM
  or multiple VMs
.PARAMETER VM
  A virtual machine or multiple virtual machines
.EXAMPLE 1
  PS> Get-VM MyVM1 | Get-VMAdvancedConfiguration
#>

  param(
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
      $vm,
      [String]$key
  )

  process{
    if ($key) {
        $VM | Foreach {
            $_.ExtensionData.Config.ExtraConfig | Select * -ExcludeProperty DynamicType, DynamicProperty | Where { $_.Key -eq $key }
        }
    } Else {
        $VM | Foreach {
                $_.ExtensionData.Config.ExtraConfig | Select * -ExcludeProperty DynamicType, DynamicProperty
            }
    }
  }
}

Comments

Hey Alan,

I made a few tweaks to your function call, not sure if you would think its better/worse. I just didnt like that FE loop stuck in there. Let me know what you think, now if we could only get a "Remove-VMAdvancedConfiguration" to work 😜

Also I changed the display to show "Setting" instead of Key but that obviously could be changed back

-----

Function Get-VMAdvancedConfiguration {
<#
.SYNOPSIS
  Lists advanced configuration setting (VMX Setting) for a VM
  or multiple VMs
.DESCRIPTION
  The function will list a VMX setting for a VM
  or multiple VMs
.PARAMETER VM
  A virtual machine or multiple virtual machines
.EXAMPLE 1
  PS> Get-VM MyVM1 | Get-VMAdvancedConfiguration
.EXAMPLE 2
  PS> Get-VMAdvancedConfiguration MyVM2 -Setting RemoteDisplay.maxConnections
#>

  param(
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
      $vm,
      [String]$setting
  )

  process{
    If ($setting){(((Get-VM $VM).ExtensionData).Config).ExtraConfig | Where { $_.Key -match $setting } | Select @{n="Setting";e="Key"}, Value}
    Else {(((Get-VM $VM).ExtensionData).Config).ExtraConfig | Select @{n="Setting";e="Key"}, Value}
  }
}

Hello,

thanks for this script. I get the error message

You cannot call a method on a null-valued expression.
At C:\Batch\Scripts\Sven\Set-VMAdvancedConfiguration.ps1:54 char:56
+       $Task = ($singlevm.ExtensionData).ReconfigVM_Task <<<< ($vmConfigSpec)
    + CategoryInfo          : InvalidOperation: (ReconfigVM_Task:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

the command was

Set-VMAdvancedConfiguration -VM MyVM -key isolation.tools.guestInitiatedUpgrade.disable -value false

best regards, Sven

Hi Alan,

I have bought the PowerCLI Book and use the above mentioned functions to configure a VMs for a Lab which will run Hyper-V 2012. I used the following commands to configure the necessary VM properties:

         $VM | Set-VMAdvancedConfiguration -key "mce.enable"  -value "TRUE"
         $VM | Set-VMAdvancedConfiguration -key "featMask.vm.hv.capable"  -value "Min:1"
         $VM | Set-VMAdvancedConfiguration -key "hypervisor.cpuid.v0"  -value "FALSE"

         $VM | Set-VMAdvancedConfiguration -key "vhv.enable"  -value "TRUE"

The first 3 work as expected butthe last doesn't at anything to the VMs VMX file.

I use ESXi 5.1 last Build with the latest vCenter Appliance.

Any help will be highly appreciated.

Best regards

Paul Kratz

As a note, from PowerCLI 5.1 these functions are no longer needed as they are covered by the *-AdvancedSetting cmdlets

Alan,

The AdvancedSetting cmdlets in PowerCLI 5.1 do not support Import-csv as the function does. This is a very useful feature when trying to add or replace multiple VMX settings.  I realize that the new cmdlets could be used in a set of loops to accomplish this task but would very inconvenient compared to the ease of the function. I would recommend VMware consider adding the Import-csv support to a future release of PowerCLI.

Thanks,

Mike

#wpavmug

Version history
Revision #:
1 of 1
Last update:
‎03-06-2012 08:46 AM
Updated by: