Automation

 View Only
Expand all | Collapse all

An item with the same key has already been added.

  • 1.  An item with the same key has already been added.

    Posted Jan 10, 2011 04:31 PM

    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



  • 2.  RE: An item with the same key has already been added.

    Posted Jan 10, 2011 04:39 PM

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



  • 3.  RE: An item with the same key has already been added.

    Broadcom Employee
    Posted Jan 10, 2011 04:47 PM

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

    Try

    Get-VM

    To see if that returns anything.

    Arnim



  • 4.  RE: An item with the same key has already been added.

    Posted Jan 11, 2011 09:20 AM

    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.



  • 5.  RE: An item with the same key has already been added.

    Broadcom Employee
    Posted Jan 11, 2011 10:54 AM

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



  • 6.  RE: An item with the same key has already been added.

    Posted Jan 11, 2011 10:59 AM

    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.



  • 7.  RE: An item with the same key has already been added.

    Broadcom Employee
    Posted Jan 11, 2011 11:33 AM

    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?



  • 8.  RE: An item with the same key has already been added.

    Broadcom Employee
    Posted Jan 11, 2011 11:38 AM

    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



  • 9.  RE: An item with the same key has already been added.

    Posted Jan 11, 2011 01:13 PM

    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.



  • 10.  RE: An item with the same key has already been added.

    Broadcom Employee
    Posted Jan 11, 2011 01:44 PM

    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.



  • 11.  RE: An item with the same key has already been added.

    Posted Jan 11, 2011 01:59 PM

    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



  • 12.  RE: An item with the same key has already been added.

    Posted Jan 12, 2011 08:23 AM

    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.



  • 13.  RE: An item with the same key has already been added.

    Broadcom Employee
    Posted Jan 12, 2011 08:45 AM

    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



  • 14.  RE: An item with the same key has already been added.

    Posted Jan 12, 2011 08:52 AM

    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.



  • 15.  RE: An item with the same key has already been added.

    Broadcom Employee
    Posted Jan 12, 2011 10:07 AM

    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



  • 16.  RE: An item with the same key has already been added.

    Posted Jan 12, 2011 04:49 PM

    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



  • 17.  RE: An item with the same key has already been added.

    Posted Jan 14, 2011 09:56 AM

    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?



  • 18.  RE: An item with the same key has already been added.

    Posted Jan 14, 2011 10:28 AM

    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.



  • 19.  RE: An item with the same key has already been added.

    Posted Jan 14, 2011 12:17 PM

    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



  • 20.  RE: An item with the same key has already been added.

    Posted Jan 14, 2011 12:25 PM

    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



  • 21.  RE: An item with the same key has already been added.

    Posted Jan 14, 2011 01:21 PM

    Outputting the stack trace can't solve the issue - it's simply coincidence. I guess the issue appears from time to time depending on something happening on the server - not sure what that is though.



  • 22.  RE: An item with the same key has already been added.

    Posted Apr 18, 2011 06:04 PM

    I ran into this issue when trying to create a snapshot of a VM.  It only happens on the first occurence of a command in a new PowerCLI session after I close out another session.  The quick fix can be to do something like the following..  You can repeat the first command against PowerCLI, first in the try block with a "-ea stop" which properly signals a stop exception and will then run the catch block to run the command again.

    if($vm | get-snapshot | where {$_.name -eq "arraysnap"}) {
        write "Removing Previous VM Snapshot .."
        try {
            Write "Trying to remove snap.."
            $vm | get-snapshot | where {$_.name -eq "arraysnap"} | remove-snapshot -confirm:$false -ea stop| out-null
        } catch {
            Write "Encountered PowerCLI bug.. trying again.."
            $vm | get-snapshot | where {$_.name -eq "arraysnap"} | remove-snapshot -confirm:$false | out-null
        }  
    }



  • 23.  RE: An item with the same key has already been added.

    Posted Aug 29, 2012 06:54 AM

    I've been getting this error intermittently before as well, but now it has come to stay, it seems. I'm using vCloud Director 1.5.

    I'm using the VcloudSDK_V1_5.dll in .NET, and are getting it when calling the com.vmware.vcloud.sdk.Vdc.GetVdcByReference(vCloudClient client, ReferenceType vdcRef) method. Now, this seems to happen only to one user (I log him in in the API, and then I can't get the Vdc). This code has worked before for this user. The only thing that's happened (that I can remember) is that I changed ownership of a vApp from me to him. Now this call fails.

    The stack I have at the moment is:

    com.vmware.vcloud.sdk.utility.VCloudException: An item with the same key has already been added.

    at com.vmware.vcloud.sdk.Vdc.GetVdcByReference(vCloudClient client, ReferenceType vdcRef)

    [..]

    my code

    I saw something about an upgrade in an earlier post from you, is there a later version for the .DLL as well, or do we need to upgrade on the servers?

    Thanks!



  • 24.  RE: An item with the same key has already been added.

    Posted Aug 29, 2012 07:04 AM

    I tried pulling the InnerException property from the VCloudException that I catch, but it's null - so that's the best call stack I have..



  • 25.  RE: An item with the same key has already been added.

    Posted Aug 29, 2012 07:09 AM

    Ok, so I changed ownership of the vApp in vCloud back to me, and all of a sudden, the user could login again without the error.

    It also only happens in the API DLL. If I use the vCloud gui for the user, it worked just fine. Still need to know why though, if this happens every time ownership changes - we're not going to be able to use the APIs..