I have just migrated to vsphere 4.1 and esx 4.1. I have powershell 2 and powercli and I am in the process of upgrading the tools and the vm h/w version which i dont have a problem doing however, now i'm trying to edit a vmx file config parameters. i found several powercli scripts and all of them seem to change the config params and one of the files takes input from a .csv file however only one record gets added. how can i get this script to add all of the entries from my csv file to a single vm. here is one of the scripts i'm trying to use. also, are all command & params kosher with powercli?
$vcenter=Read-Host "enter your vcenter servername"
connect-viserver $vcenter
$import=import-csv "c:\path to csv file\vmxsettings.csv"
$vms=get-view (get-vm nameofvm).ID
$vmconfigspec=new-object vmware.vim.virtualmachineconfigspec
foreach($item in $import){
$extra=new-object vmware.vim.optionvalue
$extra.key=$item.key
$extra.value=$item.value
$vmconfigspec.extraconfig +=$extra
$vm.reconfigvm($vmconfigspec)
}
disconnect-viserver -confirm:$false
Discussion moved to the PowerCLI forums
the errors i get back when i run the script is: property 'key' cannot be found on this object; make sure it exists and is settable.
property 'value' cannot be found on this object...
Your script is neatly correct, you just have to move the reconfigvm call outside the foreach loop.
I also changed the $vms variable to $vm
$vcenter=Read-Host "enter your vcenter servername"
connect-viserver $vcenter$import = import-csv "c:\test.csv" -UseCulture
$vm = get-view (get-vm nameofvm).ID$vmconfigspec = new-object vmware.vim.virtualmachineconfigspec
foreach($item in $import){
$extra = new-object vmware.vim.optionvalue
$extra.key=$item.key
$extra.value=$item.value
$vmconfigspec.extraconfig +=$extra
}
$vm.reconfigvm($vmconfigspec)disconnect-viserver -confirm:$false
The next thing to check is the layout of the CSV file.
The one I used for testing looks like this.
"Key","Value"
"key1","value1"
"key2","value2
"key3","value3"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Interesting discussion. Which values are worth to be changed this way?
Thanks for feedback
regards
Andreas
I mentioned some security-related .vmx parameters in my Security – Hardening – Part 1 – Virtual Machines post.
See also the latest Security Hardening paper.
And most of the .vmx params are documented on the SanBarrow site.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you, Lucd, I will try this tomorrow. BTW, is there a way to check if the entries I want to add are not already there like a (-isnot) or (no duplicate)? I want to make sure that none of the entries I want to add are preexisting.
Lucd, just as important, how can I have the script check for preexisting entries in the config params file? I want to check that if any of the entries i want to add are already there then only add the remaining entries.
I actually have about 15 entries to add
You will find most of the VMX parameters with this line
$vm = Get-VM MyVM
$vm.Extensiondata.Config.ExtraConfig
To check if a specific parameter is already set, you can do
$tgtParam = "
isolation.tools.paste.disable
"$vm.Extensiondata.Config.ExtraConfig | where {$_.Key -eq $tgtParam}
To check if a specific value is set for that param, you can do
$tgtParam = "
isolation.tools.paste.disable
"$vm.Extensiondata.Config.ExtraConfig | where {$_.Key -eq $tgtParam -and $_.Value}
We can test the Value field in this case like that because it will have a true/false value, which PowerShell will convert to the Boolean $true or $false.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference