alexbenetatos
Contributor
Contributor

An item with the same key has already been added.

I'm trying to create a new VM using the command:

get-vmhost [esx_server_name] | new-vm -name [vm_name] and get the following error:

New-VM : 10/01/2011 16:22:57    New-VM        An item with the same key has already been added.

   + CategoryInfo          : NotSpecified: (:) [New-VM], VimException
   + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM

I'm using Powershell v2 and Powercli 4.1.1 and my VMware environment is vSphere 4.0 U1

My code was working fine previously, so I'm just wondering if anyone come across this particular error before?

Many thanks

Tags (2)
0 Kudos
24 Replies
LucD
Leadership
Leadership

Did you check if there is already a guest with the name [vm_name] on that host ?


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

0 Kudos
avlieshout
VMware Employee
VMware Employee

There is already a VM present with the name you are trying to create.

Try

Get-VM

To see if that returns anything.

Arnim

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
alexbenetatos
Contributor
Contributor

Many thanks for the responses, unfortunately the error is different when a VM with the same name exists:

New-VM : 11/01/2011 09:15:03    New-VM        VM with name 'NGHLABT001' already exists.

+ CategoryInfo          : InvalidArgument: (:) [New-VM], DuplicateName
   + FullyQualifiedErrorId : Client20_VMServiceImpl_TryValidateUniqieVmName_DuplicateName,VMware.VimAutomation.ViCore
  .Cmdlets.Commands.NewVM

When I run the new-vm command, the VM is actually created but the command returns the error 'An item with the same key has already been added.' Obviously I could suppress the error, but I suspect that this is happening for a particular reason and I would like to understand why.

This happens when I try to connect to either of my VC servers (running in linked mode) and create a VM in any of my farms.

0 Kudos
avlieshout
VMware Employee
VMware Employee

What is the exact syntax you're using. Are you deploying from a template or just creating a new VM?

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
alexbenetatos
Contributor
Contributor

connect-viserver [vc_servername]

get-vmhost [ESXi_host_name] | new-vm -name NGHLABT001

When I initiate this command, the command returns the 'An item with the same key has already been added.' error BUT still creates the VM object.

I am running this from a powershell session, but the same issue occurs when I run the command from within a script. I have stripped out all the other variables (ie memory size, disk size, network etc) that I would normally include.

0 Kudos
avlieshout
VMware Employee
VMware Employee

Can't reproduce your problem. Works on my environment without error. My ESX servers are 4.1 though, but I don't think that's causing the issue here.

Have you tried different hosts?

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
avlieshout
VMware Employee
VMware Employee

Did you check the virtual HW after the VM is created?

The error sounds to me that the problem is with creating an item in the virtual HW, Like Nic,SCSI controleer,and so forth

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
alexbenetatos
Contributor
Contributor

Many thanks for the suggestions.

I've checked the hardware that has been assigned to the VM, it looks no different from any other VM that has been created via powershell nor a VM that I have created through the vSphere client.

Unfortunately I can only assume that something has changed in my environment. This code worked fine last week, now it works but returns the error as well.

I've found another situation where this same error code is returned.

get-vm nghlabt001 | New-HardDisk -CapacityKB 1024

It seems that anytime I am programatically creating a new object, the 'same key has already been added' error is returned even though the action is completed successfully. If I use the vSphere GUI, I don't get any issues reported and in both cases the error is not mentioned in the VC logs.

0 Kudos
avlieshout
VMware Employee
VMware Employee

Do you get the same error message when connected directly to the ESX host?

If so, try to restart the management agents on that host and try again.

If not, it is probably something with your vCenter server, try to reboot it and try again.

I'm just guessing here, I haven't got any clue why this is happening.

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
alexbenetatos
Contributor
Contributor

Thanks for the help, once again.

When I connect dirtectly to the host, the command works perfectly - no errors. I'll schedule the reboots for the VC servers and let you know if that fixes the issue.

Many thanks

Alex

0 Kudos
alexbenetatos
Contributor
Contributor

I rebooted both my servers this morning and tested the code again - same error.

Unfortunately I still don't understand what is causing this, but we do plan to upgrade to 4.1 next week so maybe that will resolve the situation.

0 Kudos
avlieshout
VMware Employee
VMware Employee

Just wondering if this has anything to do with running in linked mode.

Are you connected to both servers when you try to create the VM?

Have you tried to provide the -Server parameter on the New-VM cmdlet, to make sure that the command is only run against that vCenter?

$vc = Connect-VIServer myVC

Get-VMHost myHost | New-VM -Name myVM -Server $vc

Arnim

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
alexbenetatos
Contributor
Contributor

Thanks for the suggestion, Arnim.

I had only been connected to a single VC when I had been testing previously but I have just tried using your code and get the same error.

New-VM : 12/01/2011 08:47:28    New-VM        An item with the same key has already been added.

    + CategoryInfo          : NotSpecified: (:) [New-VM], VimException
    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM

I was also wondering if it was something to do with linked mode as each VC has its own database but both VCs started behaving this way at the same time. Haviong said that however, I have no idea how I can troubleshoot this further.

0 Kudos
avlieshout
VMware Employee
VMware Employee

Maybe you should file a SR with VMware.

At this point I don't have any clue either and I would file an SR at this point.

Arnim

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
admin
Immortal
Immortal

You said that your code was working previously - were you executing it against linked-mode VC or a stand-alone VC?

The fact that you don't see the error in the GUI suggests that the problem is within PowerCLI - probably because of some unexpected data.  Could you please post the stacktrace of the exception so that I get a better idea where the problem might be?

$error[0].exception.stacktrace

0 Kudos
alexbenetatos
Contributor
Contributor

Apologies for the delay in responding.

I ran the new-vm command again this morning and got exactly the same error. The stacktrace from that attempt is below:

at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input, Hashtable error
Results, Boolean enumerate)
   at System.Management.Automation.PipelineNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList, ExecutionC
ontext context)
   at System.Management.Automation.StatementListNode.ExecuteStatement(ParseTreeNode statement, Array input, Pipe output
Pipe, ArrayList& resultList, ExecutionContext context)

But now here is the strange thing - since outputting that stacktrace, I have tried the new-vm command again - exactly the same syntax, hostname, vm name etc - AND IT WORKS. Now I get the expected result:

Name                 PowerState Num CPUs Memory (MB)
----                 ---------- -------- -----------
NGHLABT001           PoweredOff 1        256

Is there something about $error[0].stacktrace that would have fixed this?

0 Kudos
alexbenetatos
Contributor
Contributor

I think I got too excited there, the command is failing again :smileycry:

Full stack trace:

at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at VMware.VimAutomation.Client20.StaticDataServiceImpl.PopulateTaskDescriptionDictionary()
   at VMware.VimAutomation.Client20.StaticDataServiceImpl.GetTaskDescription(String taskDescriptionId)
   at VMware.VimAutomation.Client20.TaskImpl.Create(ManagedObjectReference moRef, VimClient client, Dictionary`2 props)
   at VMware.VimAutomation.Client20.TaskServiceImpl.GetTaskCore(Nullable`1 state)
   at VMware.VimAutomation.Client20.TaskServiceImpl.GetTask(Nullable`1 state, CmdletTaskInfoProvider cmdletTaskInfoProvider)
   at VMware.VimAutomation.Client20.TaskServiceImpl.GetTaskById(MoId id, CmdletTaskInfoProvider cmdletTaskInfoProvider)
   at VMware.VimAutomation.Client20.VmServiceImpl.NewVM(VMHostImpl host, String name, ResourcePoolImpl pool, FolderImpl location, DatastoreImpl datastore, Int64[] diskSizes, String[] diskPaths, Int64 memorySize, Int32 cpuCount, Boolean hasFloppy, Boolean hasCD, String guestId, String alternateGuestName, String[] networks, OSCustomizationSpec customization, Boolean allowNested, Boolean isRequestedAsync, String description, Nullable`1 haRestartPriority, Nullable`1 haIsolationResponse, Nullable`1 drsAutomationLevel)
   at VMware.VimAutomation.Client20.VmServiceImpl.NewVM(VMHost host, String name, ResourcePool pool, Folder location, Datastore datastore, Int64[] diskSizes, String[] diskPaths, Int64 memorySize, Int32 cpuCount, Boolean hasFloppy, Boolean hasCD, String guestId, String alternateGuestName, String[] networks, OSCustomizationSpec customization, Boolean allowNested, Boolean isRequestedAsync, String description, Nullable`1 haRestartPriority, Nullable`1 haIsolationResponse, Nullable`1 drsAutomationLevel)
   at VMware.VimAutomation.Commands.NewVM.DoWork(VIAutomation client, List`1 moList)
   at VMware.VimAutomation.BaseCmdlet.DoWorkEx(VIAutomation client, List`1 moList)
   at VMware.VimAutomation.Util.ClientVisitor`1.Visit(T[] objs, Worker worker, ErrorCallback errorCallback)
   at VMware.VimAutomation.BaseCmdlet.ProcessRecordCore()
   at VMware.VimAutomation.BaseCmdlet.ProcessRecord()
   at System.Management.Automation.Cmdlet.DoProcessRecord()
   at System.Management.Automation.CommandProcessor.ProcessRecord()

The command works normally as soon as I output this stacktrace. I have been able to create a further 4 VMs using the same command (different names) without an error so far. i'll test again in a short while.

0 Kudos
admin
Immortal
Immortal

Ok, I looked at the code and found the problem. We are retrieving information for the task we initiated on the server for VM creation - for some reason the server is reporting some duplicate information and our code doesn't handle that case correctly. Unfortunately there isn't a workaround for this - it has to be fixed in our code.

But looking at the stack trace it looks like you are using and older version of PowerCLI. You may consider upgrading it to the latest one - 4.1.1, although this won't fix the described issue.

Regards,

Dimitar

PowerCLI team

0 Kudos
alexbenetatos
Contributor
Contributor

Dimitar, many thanks for the information.

We are planning an upgrade of our vSphere environment and will upgrade our PowerCLI version as part of that upgrade. I have been running 4.1.1 on my own laptop and was experiencing the same issue, so I can confirm that this won't fix the issue.

Currently, I am still able to create VMs without the error occuring again. Is there any reason why outputting the stacktrace would have resolved the issue?

thanks again for your time.

Alex

0 Kudos