Hello,
I am working on a small script to deploy some VM's from a template. I keep getting the following error:
New-VM : 10/02/2012 11:37:04 New-VM An item with the same key has alr
eady been added.
At C:\scripts\deploy-template.ps1:46 char:7
+ New-VM <<<< -Name $vmname -Template $template_name -VMHost $destination_host
-Datastore $datastore_name -Confirm:$false
+ CategoryInfo : NotSpecified: (:) [New-VM], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomatio
n.ViCore.Cmdlets.Commands.NewVM
At the moment I get this error I can see the machine being deployed in vCenter but it seems it wants to deploy another VM and thus spawns the error.
The small script I use:
$vcenter = "vcenter.lab.local"
$vmname = "testing"
$destination_host = "host01.lab.local"
$template_name = "tpl-clean"
$datastore_name = "00-local"
Connect-VIServer -Server $vcenter -Protocol https
New-VM -Name $vmname -Template $template_name -VMHost $destination_host -Datastore $datastore_name
Disconnect-VIServer -Server $vcenter -Confirm:$false
Any help here?
Can you check if you have more than 1 vSphere server connection open ?
Do a
$defaultViServers
and check if there is more than 1 entry.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello Luc,
Yes, I always run with 1 connection.
It seems removing "-Protocol https" from the Connect-VIServer command resolves the error. I don't know if this is normal behavior, I would suspect that using https wouldn't be a problem tho...
try mentioniong username and passwords as well in the connect line. try changing vm name from testing to something else
then give it a go .
I added the username and password, I changed the VM name, hostname, datastore and even tried several usernames. All keep generating the same "An item with the same key has already been added." error.
For now I will just use "Connect-VIServer -Server $vcenter" since it seems this does the trick for me and the connection is via port 443.
Thanks for the help tho.
Can you post exception details in the errors? This may help us to isolate code that cause the error. You can do that with:
$error[0].Exception | select *
$error[0].Exception.InnerException | select *
Hello and thanks for the reply.
It seems the main problem wasn't a powercli problem but it was caused by the specific template I was using. I tried another template (since this was the only thing I didn't change for debugging) and this works without a problem. Even connecting through HTTPS.
The original template is a Debian 6 VM which was made in vSphere 4.1 (guestid: Other 2.6.x Linux 64-bit) and I changed this with the upgrade to vSphere 5.0 into Debian GNU/Linux 6 (64-bit). I will just play safe and recreate this template using the "new machine" wizard.
Problem solved in a weird way.
I shouted too soon it seems.
I added the code you've asked and got this as a result:
ErrorId : Core_BaseCmdlet_UnknownError
ErrorCategory : NotSpecified
TargetObject :
RecommendedAction :
SessionId :
ConnectionId : /VIServer=@localhost:443/
Severity : Error
Message : 10/02/2012 16:55:04 New-VM An item with the same
key has already been added.
Data : {ParameterValues}
InnerException : System.ArgumentException: An item with the same key has alr
eady been added.
at System.ThrowHelper.ThrowArgumentException(ExceptionRe
source resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey k
ey, TValue value, Boolean add)
at System.Collections.Generic.Dictionary`2.Add(TKey key,
TValue value)
at VMware.VimAutomation.ViCore.Impl.V1.Service.StaticDat
aServiceImpl.PopulateTaskDescriptionDictionary()
at VMware.VimAutomation.ViCore.Impl.V1.Task.TaskImpl.Cre
ate(ManagedObjectReference moRef, VimClient client, Diction
ary`2 props, Object extensionData)
at VMware.VimAutomation.ViCore.Impl.V1.Task.ViCoreTaskCo
reServiceProviderImpl.<GetTaskListForClient>d__7.MoveNext()
at System.Collections.Generic.List`1.InsertRange(Int32 i
ndex, IEnumerable`1 collection)
at System.Collections.Generic.List`1.AddRange(IEnumerabl
e`1 collection)
at VMware.VimAutomation.ViCore.Impl.V1.Task.ViCoreTaskCo
reServiceProviderImpl.GetTaskList(IEnumerable`1 uidList, IE
numerable`1 connectionUidList)
at VMware.VimAutomation.Sdk.Impl.V1.Task.CoreTaskService
Impl.GetTaskList(IEnumerable`1 uidList, IEnumerable`1 conne
ctionUidList)
at VMware.VimAutomation.ViCore.Impl.V1.Service.TaskServi
ceImpl.GetTaskCore(Nullable`1 state, IList`1 connectionUidL
ist)
at VMware.VimAutomation.ViCore.Impl.V1.Service.TaskServi
ceImpl.GetTask(Nullable`1 state, CmdletTaskInfoProvider cmd
letTaskInfoProvider)
at VMware.VimAutomation.ViCore.Impl.V1.Service.TaskServi
ceImpl.GetTaskById(String uid, CmdletTaskInfoProvider cmdle
tTaskInfoProvider)
at VMware.VimAutomation.ViCore.Impl.V1.Service.VmService
Impl.NewVM(TemplateInterop template, VMHostInterop vmHost,
String name, VIContainerInterop resourceContainer, FolderIn
terop location, StorageResourceInterop datastore, Nullable`
1 diskStorageFormat, OSCustomizationSpecInterop customizati
on, Boolean allowNested, Boolean isRequestedAsync, String d
escription, Nullable`1 haRestartPriority, Nullable`1 haIsol
ationResponse, Nullable`1 drsAutomationLevel, AdvancedOptio
n[] options)
at VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM.Do
Work(VIAutomation client, List`1 moList)
at VMware.VimAutomation.ViCore.Util10Ps.BaseCmdlet.BaseC
mdlet.DoWork(VIConnection connection, List`1 moList)
at VMware.VimAutomation.Sdk.Util10Ps.BaseCmdlet.BaseCmdl
et.DoWorkEx(VIConnection connection, List`1 moList)
at VMware.VimAutomation.Sdk.Util10Ps.BaseCmdlet.Connecti
onDispatchHelper.ExecuteRunlist(Dictionary`2 runlist, DoWor
kDelegate doWorkDelegate, UidDispatchHelper uidDispatchHelp
er, List`1 result)
TargetSite :
StackTrace :
HelpLink :
Source :
I am not sure if this is the information you requested?
Thanks for the error details.
It's really weird problem. It's caused by a duplicate keys when trying to populate task description from vCenter server.
What are the VC/ESX versions?
I am running the following:
vCenter server 5 (build 455964)
ESXi hosts vSphere 5 (build 515841)
The odd thing is that this doesn't happen everytime. I was testing around with the parameters and about 3/5 times the VM will deploy without a problem. Then for some reason it goes wrong and I keep getting the error for quite some time.
Basicly the steps which happen:
1: run script, clone template to a new vm: ok
2: delete vm
3: run script, clone template to a new vm: ok
4: delete vm
5: run script, clone template to a new vm: ok
6: delete vm
7: run script, duplicate error even tho the VM is being deployed at that point yet the whole PowerCLI is being ran (and a Start virtual machine is queued)
8: delete vm
Even when it gives the duplicate error and it is not listed via vCenter I even can't list the VM by using Get-VM or Get-View. At first my idea was like Luc said having multiple connections but since I always close my connection after a test (even getting the "not connected" warning/error) this clearly wasn't the problem.
The other weird thing is that if I either add or remove the $error code the script will work again...
I'm having the exact same error when trying to create VMs via PowerShell, the error only occurs when I try to clone from a VM or Template, the VM still gets created but not in the VApp specified, I've tried a new template but no difference.
My environment is running 4.1 Update 2 on Hosts and VC
Here's the line from PoSh, I've tried as below and with -Template instead of cloning the VM.
new-vm -Name TestVM -VM Windows2008R2Template -VAPP AISC_Test_VMs -Datastore UCL-DEV-DS01
From the system logs:
Feb 17 10:46:50.452: player| VMClientConnectSocketEx
Feb 17 10:46:50.676: player| CnxConnectAuthd: Returning false because CnxAuthdProtoConnect failed
Feb 17 10:46:50.676: player| Cnx_Connect: Returning false because CnxConnectAuthd failed
Feb 17 10:46:50.676: player| Cnx_Connect: Error message: There is no VMware process running for config file /vmfs/volumes/4dcc37c6-4a430307-ae7d-9c8e99fcad12/Windows2008R2Template/Windows2008R2Template.vmx
Feb 17 10:46:50.676: player| cui::MKS::OnReacquireDone: Failed to reconnect the MKS: There is no VMware process running for config file /vmfs/volumes/4dcc37c6-4a430307-ae7d-9c8e99fcad12/Windows2008R2Template/Windows2008R2Template.vmx
Feb 17 10:46:50.676: player| cui::MKS::ReconnectionFailure: Re-connection failed: There is no VMware process running for config file /vmfs/volumes/4dcc37c6-4a430307-ae7d-9c8e99fcad12/Windows2008R2Template/Windows2008R2Template.vmx
Feb 17 10:46:50.676: player| cui::MKS::ReconnectionFailure: Retrying...
Feb 17 10:46:50.676: player| cui::VMTicketingVIM::AcquireTicket
Feb 17 10:46:50.676: player| cui::VMTicketingVIM::AcquireTicket - FORCED_QUEUING - queuing
Output from PoSh:
New-VM : 17/02/2012 11:05:55 New-VM Operation is not valid due to the current state of the object.
At line:1 char:7
+ new-vm <<<< -Name TestVM -Template Windows2008R2Template -VAPP AISC_Test_VMs -Datastore UCL-DEV-DS01
+ CategoryInfo : NotSpecified: (:) [New-VM], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM
ErrorId : Core_BaseCmdlet_UnknownError
ErrorCategory : NotSpecified
TargetObject :
RecommendedAction : Error occured while executing cmdlet: New-VM. Check inner exception for more details.
SessionId :
ConnectionId :
Severity : Error
Message : 17/02/2012 11:05:55 New-VM Operation is not valid due to the current state of the object.
Data : {ParameterValues}
InnerException : System.InvalidOperationException: Operation is not valid due to the current state of the object.
at VMware.VimAutomation.ViCore.Impl.V1.Task.ViCoreTaskCoreServiceProviderImpl.BeginTaskCompletionPoll(List`1 taskList)
at VMware.VimAutomation.Sdk.Impl.V1.Task.CoreTaskServiceImpl.WaitForTask(IEnumerable`1 taskList, ProgressCallback progressCallback)
at VMware.VimAutomation.Sdk.Util10Ps.BaseCmdlet.BaseCmdlet.EndProcessingErrorHandled()
at VMware.VimAutomation.ViCore.Util10Ps.BaseCmdlet.BaseCmdlet.EndProcessingErrorHandled()
TargetSite : Void ThrowTerminatingError(System.Management.Automation.ErrorRecord)
StackTrace : at System.Management.Automation.MshCommandRuntime.ThrowTerminatingError(ErrorRecord errorRecord)
HelpLink :
Source : System.Management.Automation
Message : Operation is not valid due to the current state of the object.
Data : {}
InnerException :
TargetSite : Void BeginTaskCompletionPoll(System.Collections.Generic.List`1[VMware.VimAutomation.Sdk.Interop.V1.Task.TaskInterop])
StackTrace : at VMware.VimAutomation.ViCore.Impl.V1.Task.ViCoreTaskCoreServiceProviderImpl.BeginTaskCompletionPoll(List`1 taskList)
at VMware.VimAutomation.Sdk.Impl.V1.Task.CoreTaskServiceImpl.WaitForTask(IEnumerable`1 taskList, ProgressCallback progressCallback)
at VMware.VimAutomation.Sdk.Util10Ps.BaseCmdlet.BaseCmdlet.EndProcessingErrorHandled()
at VMware.VimAutomation.ViCore.Util10Ps.BaseCmdlet.BaseCmdlet.EndProcessingErrorHandled()
HelpLink :
Source : VMware.VimAutomation.ViCore.Impl
Assuming you are still experiencing this issue, see the following thread: First New-VM : An item with the same key has already been added.
It appears to be fixed in a forthcoming release of PowerCLI. At least I can verify it solved the problem for me thus far.