VMware Cloud Community
JT1024
Contributor
Contributor

Cannot seem to edit vmx file using following script

I am relatively new to using powercli and I am trying to write our DR script.  This will include needing to edit the vmx file to use a replacement disk.  I found the script on the following link, but it doesn't seem to work any more.  I SWEAR it worked once.  When I try now I don't get any errors from what I've got, and it just doesn't do what I want it to.  Is there a log of any sort that I can look at to find out what is going on with this?

http://www.vi-toolkit.com/wiki/index.php/PowerCLI:_Scripted_edit_of_a_VMX_file

0 Kudos
17 Replies
LucD
Leadership
Leadership

Anything in the VPXD logs ? Accessible from the vSphere client <Home><System logs>


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

0 Kudos
JT1024
Contributor
Contributor

script started...

StartTime               : 3/5/2012 1:38:50 PM

log file shows error.... 
[2012-03-05 13:38:52.447 07732 warning 'App' opID=HB-host-15990@105219] [VpxdMoHost::GetLinkedVmInt] Getting linked vm for vmid 178 on host %servername% - COULD NOT FIND IT
then something about a port group not found......
I wonder....   the network will also be changing for this machine as well.  that part of the script is working.  Will that have to work first before this will work ??
0 Kudos
JT1024
Contributor
Contributor

No, I just tried to put in a portion to change VLAN, then do the vmx file edit and it doesn't work either.

0 Kudos
LucD
Leadership
Leadership

I wonder, did you refresh the contents of the $vm variable after the network and/or VLAN changes ?

In other words did you do a new

$vm = Get-VM MYVM


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

0 Kudos
JT1024
Contributor
Contributor

doesn't that get done when it does a "$vm = Get-View (Get-VM servername).Id"  in the script that I posted link to  ?

I'll try and do a Get-VM servername after I add to inventory, after I reconfigure the VLAN, and before I run the script I posted...  for grins...

0 Kudos
LucD
Leadership
Leadership

Perhaps it would be useful if you attach the script you are using


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

0 Kudos
JT1024
Contributor
Contributor

Here's what I'm testing with....

$a = Get-Folder "FolderName"

$Cluster="ClusterName"

New-VM -VMFilePath "%path% with %filename%" -VMHost (Get-Cluster $Cluster | Get-VMHost ESXHOSTNAME | Select-Object -first 1) -Location $a[1] -RunAsync
sleep -seconds 10
Set-NetworkAdapter -NetworkAdapter ( Get-NetworkAdapter servername ) -NetworkName "VLAN NAME" -StartConnected $false -confirm:$false
sleep -seconds 10
$vm = Get-VM servername
sleep -seconds 10
<#  last part  #>
$key = "scsi0:8.filename"
$value = "/vmfs/volumes/ReplacementDiskNumber/servername/servername.vmdk"
$vm = Get-View (Get-VM servername).Id
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.extraconfig += New-Object VMware.Vim.optionvalue
$vmConfigSpec.extraconfig[0].Key=$key
$vmConfigSpec.extraconfig[0].Value=$value
$vm.ReconfigVM($vmConfigSpec)

First part seems to work fine, it adds machine to inventory

Second part works fine, adds correct vlan

last part, not so much.  I have used the $value from a vmx file that has been built manually thru removing and adding the disk via the gui.  I can relace the vmx file, but think that being able to edit a vmx file like this will be benificial in the future...

0 Kudos
LucD
Leadership
Leadership

You can't use this method to change values in the VMX file that can also be changed with other methods.

In this case you are trying to change the backing filename with the ReconfigVM_Task method.


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

0 Kudos
JT1024
Contributor
Contributor

So this will not change the keys of a vmx file ??

<#  last part  #>
$key = "scsi0:8.filename"
$value = "/vmfs/volumes/ReplacementDiskNumber/servername/servername.vmdk"
$vm = Get-View (Get-VM servername).Id
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.extraconfig += New-Object VMware.Vim.optionvalue
$vmConfigSpec.extraconfig[0].Key=$key
$vmConfigSpec.extraconfig[0].Value=$value
$vm.ReconfigVM($vmConfigSpec)

I was merely following the following link ....

http://www.vi-toolkit.com/wiki/index.php/PowerCLI:_Scripted_edit_of_a_VMX_file

Is this not correct ?

0 Kudos
JT1024
Contributor
Contributor

0 Kudos
LucD
Leadership
Leadership

The  ReconfigVM method to change VMX entries will work, but not for all entries in your VMX file.

If the entry is changable by another method, it will most probably not work this way (at least that is my experience).


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

0 Kudos
JDLangdon
Expert
Expert

I've never edited the .vmx file using PowerCli but I did notice that in order for manual edits to work I had to have the VM powered off and sometimes I had to remove the vm from and readd it to the inventory.

If this method works, I'll be one happy camper.

jd

0 Kudos
abirhasan
Enthusiast
Enthusiast

I have tried this script but it does not work properly. or may I did some things mistake?

abirhasan 
0 Kudos
JT1024
Contributor
Contributor

So what you're saying is that in your experience is that if the value can be changed via the GUI, then it will likely not be able to be changed via PowerCLI, correct?

0 Kudos
LucD
Leadership
Leadership

No, not the GUI.

If the property can be changed through one of the API methods, it will most probably not work through the ExtraOptions method.

These API methods are in fact what is underneath the PowerCLI cmdlets.

The VirtualMachineConfigInfo object is the one that holds all the config values for a VirtualMachine.

The ExtraConfig property is exactly that, an extra to configure all the values in the VMX file that are not accessible through other properties in the VirtualMachineConfigInfo object.


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

0 Kudos
JDLangdon
Expert
Expert

The modifications which I was attempting to make are not available in the GUI while the VM's are running.  I was trying to edit the advanced configurations.  Regardless if you are using the GUI or hacking the .vmx file, in my experience the VM had to be powered off.

0 Kudos
LucD
Leadership
Leadership

That is correct, for most of the VMX entries the VM needs to be powered off.

And sometimes even unregistered from the vCenter.

If you power off a VM it will save the in-memory settings to the VMX file, and the changes you made will most probably be overwritten.


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

0 Kudos