VMware Cloud Community
ArielGraner
Enthusiast
Enthusiast
Jump to solution

Editing .vmx files using PowerCLI

Hey there!

I was wondering if there is a way to edit a .vmx file by CLI...

To be precise, I want to be able to modify this line:

ethernet0.generatedAddress = "00:50:56:bc:4c:e8"

In each .vmx file, to replace the MAC address in the line above with one that is in a variable.

Is this possible to do inside a PowerCLI script? and if it is, I would like to get some help in regarding to how to do so.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I was afraid of that, if there is a direct method available to change a device setting, the ExtraConfig option will not work.

Try this instead, it does the same thing as the Set-NetworkAdapter, but without the range checking.

$vmName = "TestVM"
$nicName = "Network adapter 1"
$newMAC = "11:22:33:44:55:66"

$vm = Get-VM -Name $vmName
$nic = Get-NetworkAdapter -VM $vm -Name $nicName

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$devSpec = New-Object VMware.Vim.VirtualDeviceConfigSpec
$devSpec.Device = $nic.ExtensionData
$devSpec.Device.MacAddress = $newMAC
$devSpec.operation = "edit"

$spec.DeviceChange += $devSpec

$vm.ExtensionData.ReconfigVM($spec)


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

View solution in original post

0 Kudos
18 Replies
LucD
Leadership
Leadership
Jump to solution

Afaik there is no need to edit the VMX file for this, use the Set-NetworkAdapter cmdlet.


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

0 Kudos
ArielGraner
Enthusiast
Enthusiast
Jump to solution

Unfortunately this is not the way..

Yes, set-networkadapter will do the job, but in my case I need to set an address that is in the pool reserved for the system to auto generate MAC addresses, so if I use the set-networkadapter cmdlt then there is an error stating that the address is out of range.

The only way for me to set the address that I need  is to edit the .vmx file, hence the question above.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I see, that was not clear in the question.

Did you try the method described in Carter's post called Changing VMX files just got a whole lot easier


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

0 Kudos
ArielGraner
Enthusiast
Enthusiast
Jump to solution

Looks like the set-tkevmx cmdlt can do the job.

But it isn't clear to me how to add this command to my arsenal.

Do I only need to download and run viToolkitExtensions.psm1 ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, that module hasn't been maintained lately.

But Alan used something similar in Retrieve and Set VM Advanced Configuration (VMX) settings

Those are stand-alone functions, no need to load the module


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

0 Kudos
ArielGraner
Enthusiast
Enthusiast
Jump to solution

OK, so let me just verify this: I need to run the two scripts in Alan's post once and that's it? I will be able to use the Set-VMAdvancedConfiguration and Get-VMAdvancedConfiguration commands freely?

0 Kudos
kunaludapi
Expert
Expert
Jump to solution

Check this link may be helpful for you.

Powershell: Download VMX file, Rename, Upload, Add to Inventory | TechColumnist

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I must have had a temporary attack of amnesia :smileycry:

These functions are now available in the Get-AdvancedSetting and Set-AdvancedSetting cmdlets.


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

ArielGraner
Enthusiast
Enthusiast
Jump to solution

Luc, It's close, but not there yet.

The Get-advancedsetting and Set-advancedsetting cmdlets won't let me handle with the "ethernet0.generatedAddress" key.

So... what's next?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I was afraid of that, if there is a direct method available to change a device setting, the ExtraConfig option will not work.

Try this instead, it does the same thing as the Set-NetworkAdapter, but without the range checking.

$vmName = "TestVM"
$nicName = "Network adapter 1"
$newMAC = "11:22:33:44:55:66"

$vm = Get-VM -Name $vmName
$nic = Get-NetworkAdapter -VM $vm -Name $nicName

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$devSpec = New-Object VMware.Vim.VirtualDeviceConfigSpec
$devSpec.Device = $nic.ExtensionData
$devSpec.Device.MacAddress = $newMAC
$devSpec.operation = "edit"

$spec.DeviceChange += $devSpec

$vm.ExtensionData.ReconfigVM($spec)


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

0 Kudos
ArielGraner
Enthusiast
Enthusiast
Jump to solution

Luc, you are a true master of the craft. that worked like a charm.

You have no idea how happy I am right now!

Thanks!!!

0 Kudos
ArielGraner
Enthusiast
Enthusiast
Jump to solution

Luc, something isn't working well.

When I try to use your'e answer in order to change the MAC address of "Network adapter 2" nothing seems to happen, while this worked fine for changing "Network adapter 1" on the same VM.

What did I do wrong?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Does

Get-NetworkAdapter -VM $vm -Name $nicName

return the correct NIC, when you set the $nicName variable to "Network adapter 2" ?


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

0 Kudos
ArielGraner
Enthusiast
Enthusiast
Jump to solution

It dose.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Was that VM powered on while you ran the script ?

Did you try a poweroff/power on after the script ? Does the MAC address change ?


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

ArielGraner
Enthusiast
Enthusiast
Jump to solution

Found what the problem was, never mind...

0 Kudos
Faizan2
Contributor
Contributor
Jump to solution

@LucD 

 

 

I want to overwrite the existing config of uuid.bios in .vmx file and keep "uuid.bios"="56 4d 7f ab 8d 53 6c 37-f9 2e 48 89 8f a7 54 56" static always . Can it be possible using powercli ? I am using below script but its not working.

I tried "uuid.action"="keep"  its also not helpful


$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)

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If the property (Uuid) you want to change is directly available through the VirtualMachineConfigSpec, you have to make the changes through there.

 


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

0 Kudos