VMware Cloud Community
saidar
Contributor
Contributor

how can modify/change key in VMX file

Hi ,

How can modify or change specific key in VMX file

i need help with modify  not add  since "Configuration keys that would conflict with parameters that are  explicitly configurable through other fields in the ConfigSpec object  are silently ignored."

Thanks

0 Kudos
16 Replies
LucD
Leadership
Leadership

Which specific key(s) did you have in mind ?

The text you quote means to say that you can't use this methods when there is a property in a configuration spec that controls the value of a specific VMX file entry.

The VMX entries that are configurable (add & edit) this way, can be set through the reconfigVM_Task method and the extraConfig property.

See for example Steve Jin's post called How to Change VMX Programmatically?

There are several examples in this community as well.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
saidar
Contributor
Contributor

Actually i want to edit this key ethernet9.present = "FALSE" and change it toTRUE

and i already see this post before but didn't understand it

0 Kudos
LucD
Leadership
Leadership

Afaik, that VMX key can not be changed through the ExtraConfig property.

If I understand correctly, you want to remove a NIC that is present on the VM(s).

Do you have the NIC type or NIC label for the NIC that needs to be removed ?

If yes, it would be a lot simpler to use the Remove-NetworkAdapter cmdlet.

Something like this


Get-VM
-Name MyVM |
Get-NetworkAdapter -Name "Network adapter 2" |
Remove-NetworkAdapter -Confirm:$false

If you want, you can first have a look at the NICs connected to that VM as follows

Get-VM -Name MyVM | Get-NetworkAdapter

If you want to add a NIC, you will have to use the New-NetworkAdapter cmdlet.

Something like this

Get-VM -Name MyVM | 
New-NetworkAdapter
-NetworkName portgroupname -Type vmxnet3 -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
saidar
Contributor
Contributor

ok i will will explain more , i have Vnic before in my setup  , after i delete them i get the key ethernet2.present with value FALSE  .

i want to add NIC but manually using VMX file key

so before add the new nic i have to change the value from FALSE to TRUE or simply to delete this line .

for add new vkey with value i am using these command :

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.extraConfig += New-Object VMware.Vim.OptionValue
$spec.extraConfig[0].key = "xxx.yyy.zzz"
$spec.extraConfig[0].value = "xxxxxxxxx"
(get-view (get-vm -name VM).ID).ReconfigVM_Task($spec) 
0 Kudos
LucD
Leadership
Leadership

I fail to see the reason why you want to do this at all ? That line is there for historic reasons and doesn't have any effect in a vSphere environment afaik.

But this is one way of remving that line from the VMX file:

$vm = Get-VM -Name MyVM
$datastorename,$filepath = $vm.ExtensionData.Config.Files.VmPathName.split(" ")
$datastorename = $datastorename.TrimStart("[").TrimEnd("]")
$filename = $filepath.Split('/')[-1]
$oldvmx = $env:temp + "\" + $filename 
$newvmx
= $env:temp + "\new" + (Get-Random -Maximum 1GB) + ".vmx"
$ds = Get-Datastore -Name $datastorename
Remove-VM
-VM $vm -Confirm:$false
New-PSDrive
-Name ds -PSProvider VimDatastore -Root '/' -Location $ds Copy-DatastoreItem -Item "ds:\$($filepath)" -Destination $oldvmx
Get-Content
$oldvmx | where {$_ -notmatch "ethernet9.present"} | Set-Content $newvmx
Copy-DatastoreItem
-Item $newvmx -Destination "ds:\$($filepath)" -Force -Confirm:$false Remove-PSDrive -Name ds
New-VM
-VMFilePath $vm.ExtensionData.Config.Files.VmPathName -VMHost $vm.Host -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
saidar
Contributor
Contributor

Thank you very much , i will use it and update you ,

i have another quis about add key

i faced problem with add value , althought test finished successfully but there is no key added to VMX file

Script :

$key = @("key1","key2","key3")

$value = @("value1","value2","value3")
$vms=Get-VMHost 10.136.138.1 | get-vm -name qa-l-vrt-138-009| get-view
$i=0
while ($i -le 2) {
     $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
     echo $key[$i]
     echo $value[$i]
     $extra = New-Object VMware.Vim.optionvalue
     $extra.Key=$Key[$i]
     $extra.Value=$Value[$i]
     $vmConfigSpec.extraconfig += $extra
     $vms.ReconfigVM($vmConfigSpec)
     $i++
}

and on another ESX (VM) i get this error

Method invocation failed because [System.Object[]] doesn't contain a method named 'ReconfigVM'.

At C:\Users\Administrator\AppData\Local\Temp\f68d3192-a31f-4e1d-8c4d-43d9543da076.ps1:22 char:16

+ $vms.ReconfigVM <<<< ($vmConfigSpec)

    + CategoryInfo          : InvalidOperation: (ReconfigVM:String) [], RuntimeException

    + FullyQualifiedErrorId : MethodNotFound

0 Kudos
LucD
Leadership
Leadership

It looks as if the Get-VM returned more than 1 VM, that makes that $vms is an array.

You can check with

Get-VMHost 10.136.138.1 | get-vm -name qa-l-vrt-138-009

The ReconfigVM method is not known on an array.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
saidar
Contributor
Contributor

The funny thing is this way work just once Smiley Happy

work perfect ,when run it again stopped working .

i will try later

0 Kudos
LucD
Leadership
Leadership

Any error messages ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
saidar
Contributor
Contributor

No there is no error but have a problem when insert some key ,

Is there any reservation in vmx key

0 Kudos
saidar
Contributor
Contributor

About delete contenet of file script working perfect

just you miss Enter between New-psdrive and copy-datastoreitem

Thank you

0 Kudos
LucD
Leadership
Leadership

I think that missing CR-LF might be the interaction between Jive forum SW, on which the VMTN Community is running, and IE browsers.

We often see CR-LF being dropped when doing a copy/paste.

Alternative browsers (FF, Opera...) don't seem to have this problem.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
LucD
Leadership
Leadership

Not sure what you mean with the "reservation in vmx key" ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
saidar
Contributor
Contributor

sorry for late

for example i want to insert this key and value

$key = "ethernet2.dvs.switchId"
$value = "78 76 6c 61 6e 5f 76 64-73 00 00 00 00 00 00 00"

but nothing modified into VMX file , any other $key  work fine

0 Kudos
LucD
Leadership
Leadership

The settings that can be done through through other means can not be done through the VMX file entries.

In this case the setting is created when you assign the NIC with a portgroup to the VM.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
saidar
Contributor
Contributor

sorry 4 late again

I think there is no workarroung to enter special key ,

Really i  appreciate your help ,

thank you very much .

0 Kudos