VMware Cloud Community
sgrinker
Hot Shot
Hot Shot
Jump to solution

First New-VM : An item with the same key has already been added.


I'm getting a strange error that I've seen a few other posts about, but with no resolution (or at least one that I can find).  When attempting to create a new VM using the New-VM cmdlet, I receive the error "An item with the same key has already been added."

Now here is the key that I haven't seen a lot of others mention.  This ONLY happens on the first attempt for each new connection with Connect-VIServer.  I can create as many of the same machines as I want after this first error with the same paramaters (new name of course).  If I disconnect and reconnect again, the error appears for the first time.

Normally with PowerShell, I would just trap the error and be on my way.  However, in this case I'm using the -RunAsync paramter, so that I can capture the task that is returned in a variable.  From there I use Wait-Task until the VM is complete before moving on with the script.  Everythings works great when the error doesn't occur, but when it does occur the task object is not returned and the rest of the script barfs out errors.

I'm running the lastest release of PowerCLI and I've tried this on PowerShell v3.0 and v2.0 and get the same results.  I've tried various templates and hosts with no success.  I've also tried this with and without linked mode using -AllLinked on Connect-VIServer.  I should mention that I am cloning from template, specifying a customization spec, datastore cluster, and host.  Again, the only common repeatable thread here is that this ONLY occurs on the first attempt to use New-VM per session to vCenter.

Any thoughts or suggestions?

Below is the output details on the exception if that helps at all:

PS C:\> $Error[0]
New-VM : 6/5/2013 9:49:22 AM    New-VM        An item with the same key has already been added.
At line:1 char:11
+ $VMTask = New-VM -Name "NEWTEST20" -Template "Template" -OS ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [New-VM], VimException
    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM

PS C:\> $Error[0].Exception | Select *


ErrorId           : Core_BaseCmdlet_UnknownError
ErrorCategory     : NotSpecified
TargetObject      :
RecommendedAction :
SessionId         :
ConnectionId      : /VIServer=USERNAME@SERVER:443/
Severity          : Error
Message           : 6/5/2013 9:49:22 AM    New-VM        An item with the same key has already been added.
Data              : {ParameterValues}
InnerException    : System.ArgumentException: An item with the same key has already been added.
                       at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
                       at VMware.VimAutomation.ViCore.Impl.V1.Service.StaticDataServiceImpl.PopulateTaskDescriptionDict
                    ionary()
                       at VMware.VimAutomation.ViCore.Impl.V1.Service.StaticDataServiceImpl.GetTaskDescription(String
                    taskDescriptionId)
                       at VMware.VimAutomation.ViCore.Impl.V1.Task.TaskImpl.Create(ManagedObjectReference moRef,
                    VimClient client, Dictionary`2 props, Object extensionData)
                       at VMware.VimAutomation.ViCore.Impl.V1.Task.ViCoreTaskCoreServiceProviderImpl.<GetTaskListForCli
                    ent>d__7.MoveNext()
                       at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
                       at VMware.VimAutomation.ViCore.Impl.V1.Task.ViCoreTaskCoreServiceProviderImpl.GetTaskList(IEnume
                    rable`1 uidList, IEnumerable`1 connectionUidList)
                       at VMware.VimAutomation.Sdk.Impl.V1.Task.CoreTaskServiceImpl.GetTaskList(IEnumerable`1 uidList,
                    IEnumerable`1 connectionUidList)
                       at VMware.VimAutomation.ViCore.Impl.V1.Service.TaskServiceImpl.GetTaskCore(Nullable`1 state,
                    IList`1 connectionUidList)
                       at VMware.VimAutomation.ViCore.Impl.V1.Service.TaskServiceImpl.GetTask(Nullable`1 state,
                    CmdletTaskInfoProvider cmdletTaskInfoProvider)
                       at VMware.VimAutomation.ViCore.Impl.V1.Service.TaskServiceImpl.GetTaskById(String uid,
                    CmdletTaskInfoProvider cmdletTaskInfoProvider)
                       at VMware.VimAutomation.ViCore.Impl.V1.Service.VmServiceImpl.NewVM(TemplateInterop template,
                    VMHostInterop vmHost, String name, VIContainerInterop resourceContainer, FolderInterop location,
                    StorageResourceInterop datastore, Nullable`1 diskStorageFormat, OSCustomizationSpecInterop
                    customization, Boolean allowNested, Boolean isRequestedAsync, String description, Nullable`1
                    haRestartPriority, Nullable`1 haIsolationResponse, Nullable`1 drsAutomationLevel, AdvancedOption[]
                    options)
                       at VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM.DoWork(VIAutomation client, List`1 moList)
                       at VMware.VimAutomation.ViCore.Util10Ps.BaseCmdlet.BaseCmdlet.DoWork(VIConnection connection,
                    List`1 moList)
                       at VMware.VimAutomation.Sdk.Util10Ps.BaseCmdlet.BaseCmdlet.DoWorkEx(VIConnection connection,
                    List`1 moList)
                       at
                    VMware.VimAutomation.Sdk.Util10Ps.BaseCmdlet.ConnectionDispatchHelper.ExecuteRunlist(Dictionary`2
                    runlist, DoWorkDelegate doWorkDelegate, UidDispatchHelper uidDispatchHelper, List`1 result)
TargetSite        :
StackTrace        :
HelpLink          :
Source            :

PS C:\> $Error[0].Exception.InnerException | Select *


Message        : An item with the same key has already been added.
ParamName      :
Data           : {ConnectionId}
InnerException :
TargetSite     : Void Insert(TKey, TValue, Boolean)
StackTrace     :    at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
                    at
                 VMware.VimAutomation.ViCore.Impl.V1.Service.StaticDataServiceImpl.PopulateTaskDescriptionDictionary()
                    at VMware.VimAutomation.ViCore.Impl.V1.Service.StaticDataServiceImpl.GetTaskDescription(String
                 taskDescriptionId)
                    at VMware.VimAutomation.ViCore.Impl.V1.Task.TaskImpl.Create(ManagedObjectReference moRef,
                 VimClient client, Dictionary`2 props, Object extensionData)
                    at VMware.VimAutomation.ViCore.Impl.V1.Task.ViCoreTaskCoreServiceProviderImpl.<GetTaskListForClient
                 >d__7.MoveNext()
                    at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
                    at
                 VMware.VimAutomation.ViCore.Impl.V1.Task.ViCoreTaskCoreServiceProviderImpl.GetTaskList(IEnumerable`1
                 uidList, IEnumerable`1 connectionUidList)
                    at VMware.VimAutomation.Sdk.Impl.V1.Task.CoreTaskServiceImpl.GetTaskList(IEnumerable`1 uidList,
                 IEnumerable`1 connectionUidList)
                    at VMware.VimAutomation.ViCore.Impl.V1.Service.TaskServiceImpl.GetTaskCore(Nullable`1 state,
                 IList`1 connectionUidList)
                    at VMware.VimAutomation.ViCore.Impl.V1.Service.TaskServiceImpl.GetTask(Nullable`1 state,
                 CmdletTaskInfoProvider cmdletTaskInfoProvider)
                    at VMware.VimAutomation.ViCore.Impl.V1.Service.TaskServiceImpl.GetTaskById(String uid,
                 CmdletTaskInfoProvider cmdletTaskInfoProvider)
                    at VMware.VimAutomation.ViCore.Impl.V1.Service.VmServiceImpl.NewVM(TemplateInterop template,
                 VMHostInterop vmHost, String name, VIContainerInterop resourceContainer, FolderInterop location,
                 StorageResourceInterop datastore, Nullable`1 diskStorageFormat, OSCustomizationSpecInterop
                 customization, Boolean allowNested, Boolean isRequestedAsync, String description, Nullable`1
                 haRestartPriority, Nullable`1 haIsolationResponse, Nullable`1 drsAutomationLevel, AdvancedOption[]
                 options)
                    at VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM.DoWork(VIAutomation client, List`1 moList)
                    at VMware.VimAutomation.ViCore.Util10Ps.BaseCmdlet.BaseCmdlet.DoWork(VIConnection connection,
                 List`1 moList)
                    at VMware.VimAutomation.Sdk.Util10Ps.BaseCmdlet.BaseCmdlet.DoWorkEx(VIConnection connection,
                 List`1 moList)
                    at
                 VMware.VimAutomation.Sdk.Util10Ps.BaseCmdlet.ConnectionDispatchHelper.ExecuteRunlist(Dictionary`2
                 runlist, DoWorkDelegate doWorkDelegate, UidDispatchHelper uidDispatchHelper, List`1 result)
HelpLink       :
Source         : mscorlib


1 Solution

Accepted Solutions
sgrinker
Hot Shot
Hot Shot
Jump to solution

I have been informed that this is actually a "known bug" and a fix has already been planned for future code release.  After being granted access to a beta release, it does appear to be resolved in the upcomming version.

View solution in original post

14 Replies
LucD
Leadership
Leadership
Jump to solution

A wild guess, would there happen to be a snapshot on the VM when you converted it to a template ?


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

sgrinker
Hot Shot
Hot Shot
Jump to solution


Not to my knowledge, but definitely got me thinking.  With the two templates I was testing with thus far, I know that one of them was created using the other.  So I was wondering if there was possibly some key related to both of them that was generating the error since using either gave me the error.

To rule that out though, I just walked through the same steps using a new 2012 template and customization spec that I know I built from scratch with no snapshots.  Still got the exact same error on the first use of New-VM.

Thanks for the suggestion!

0 Kudos
sgrinker
Hot Shot
Hot Shot
Jump to solution

So far I have tried to swap out all paramaters being passed to New-VM with something else (VM name, template, specification, datastore, host, etc).  However, I consistently get the error when creating the first VM.  Every time after that I can use the same parameters with a new name and it works fine repeatedly until I disconnect and reconnect.  Same occurs obviously if I close PowerShell and start a new console too.

I have opened up an SR on the issue, but would still be interested if anyone has any additional feedback on what to look at in the meantime.

Thanks again!

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

I have the exact same problem when I add a vSphere Distributed Switch. The first switch I add in a PowerCLI session will give the "An item with the same key has already been added" error, although the switch seems to be created fine. All other switches I create in the same session don't give this error. See the attached screenshot for a complete session.

PowerCLI error An item with the same key has already been added.PNG

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
sgrinker
Hot Shot
Hot Shot
Jump to solution

Very interesting to know this is occuring for other object types.  I'm curious if getting the error once for any object will make it not occur for others.  In other words, sad as it is I'm thinking of creating a "fake" object in my script in order to force the error once and trap it.  That is at least until as real resolution can be found on the problem.

Unfortunately I can't just trap the actual code when I'm trying to create a real VM, as the it doesn't return the task object when the error occurs.  That means that Wait-Task doesn't work, and the rest of the script pretty much fails.  As you mentioned with your Distributed Switch, the VM does get created in my case too.

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Today I had the same error with the first Remove-VDSwitch command in my PowerCLI session. When I tried to create a new VDSwitch it went without an error. Maybe you are right and will this happen only once in a session.

Remove-VDSwitch An item with the same key has already been added error.PNG

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
sgrinker
Hot Shot
Hot Shot
Jump to solution

Very interesting, and thanks again!  I did some more testing, and turns out I get the same error even using Set-VM and just simply setting the "Notes" property wtih -RunAsync.  Again, this is ONLY the first time per session.

I'm going to pass this news along to the tech assigned to the SR I have opened.  Hopefully a resolution can be found soon.  Otherwise I'm just going to do what I consider "sloppy code" and set something basic once to safely capture the error on the first trip for now.

0 Kudos
sgrinker
Hot Shot
Hot Shot
Jump to solution

I have been informed that this is actually a "known bug" and a fix has already been planned for future code release.  After being granted access to a beta release, it does appear to be resolved in the upcomming version.

theyssef
Contributor
Contributor
Jump to solution

Hi SGrinker,

Thanks for sharing this information.

did they inform you when this fix will be released?

Would you mind sharing the update with me as well?

thanks!

Filip

0 Kudos
sgrinker
Hot Shot
Hot Shot
Jump to solution

Unfortunately I cannot help you on either request.  I was not given an ETA on release when I asked, and was also specifically informed that this version cannot be re-distributed.  If you are having the same problem, you may be able to gain access by opening an SR of your own.  Hope that helps!

Thanks!

0 Kudos
SBC350
Enthusiast
Enthusiast
Jump to solution

Hi,

I have the same issue and have opened a SR and it is going nowhere, Would mind sharing your SR number so I can relate to it ?

Thank you

If you found this or any other answer useful please consider the use of the Helpful or correct buttons to award points Alex Consultant - VMware Specialist
0 Kudos
sgrinker
Hot Shot
Hot Shot
Jump to solution

Check your PM

0 Kudos
gamename
Enthusiast
Enthusiast
Jump to solution

I'm seeing this error as well. Are there any workarounds for it until we get a permanent fix?

0 Kudos
sgrinker
Hot Shot
Hot Shot
Jump to solution

The only (and by no means a great one) recommendation I can suggest is intentionally tripping the error at least one time and trapping it with try/catch.  The the error only happens the first time after each new connection to vCenter, you can force it to happen when you expect it and trap the error.  Again, not necessarily something I would recommend, but until the updated version of PowerCLI is released where this is fixed it may be one of the only other routes.  I can't say for sure if they will provide it, but you may want to open an SR to see if you can get the beta release to help resolve your problem sooner.