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.
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
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
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.
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
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 ?
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
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?
Check this link may be helpful for you.
Powershell: Download VMX file, Rename, Upload, Add to Inventory | TechColumnist
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
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?
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
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!!!
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?
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
It dose.
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
Found what the problem was, never mind...
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)
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