Title: Add-Type fails sporadically with "An item with the same key has already been added" · Issue #19470 · PowerShell/PowerShell · GitHub
Open Graph Title: Add-Type fails sporadically with "An item with the same key has already been added" · Issue #19470 · PowerShell/PowerShell
X Title: Add-Type fails sporadically with "An item with the same key has already been added" · Issue #19470 · PowerShell/PowerShell
Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerShell 5.1 and PowerShell. Steps to reprodu...
Open Graph Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
X Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/19470
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add-Type fails sporadically with \"An item with the same key has already been added\"","articleBody":"### Prerequisites\r\n\r\n- [X] Write a descriptive title.\r\n- [X] Make sure you are able to repro it on the [latest released version](https://github.com/PowerShell/PowerShell/releases)\r\n- [X] Search the existing issues.\r\n- [X] Refer to the [FAQ](https://github.com/PowerShell/PowerShell/blob/master/docs/FAQ.md).\r\n- [X] Refer to [Differences between Windows PowerShell 5.1 and PowerShell](https://docs.microsoft.com/powershell/scripting/whats-new/differences-from-windows-powershell).\r\n\r\n### Steps to reproduce\r\n\r\nThis is elusive but *Add-Type* fails sporadically with *\"An item with the same key has been already added\"* when auto-importing module on daily build of PS 7.3 (Preview.3 and Preview.4) and PS 7.2.4 as well.\r\n\r\nIt happens usually on freshly started PowerShell console session.\r\n\r\n**What is strange is that importing the failing module again but implicitly with *-Force* switch succeeds (please see the actual behavior)**\r\n\r\nThis is not unique to specific module. In the attached example the *Add-Type* fails in official release of *PowerShellGet* module (2.2.5) - lines 517 and 796 as well as in my module *KBSystemTools* line 136:\r\n\r\nPowerShellGet (file - PSModule.psm1):\r\n~line 517\r\n```powershell\r\n try {\r\n $AddType_prams = @{\r\n TypeDefinition = $InternalWebProxySource\r\n Language = 'CSharp'\r\n ErrorAction = 'SilentlyContinue'\r\n }\r\n if (-not $script:IsCoreCLR -or $script:IsNanoServer) {\r\n $AddType_prams['ReferencedAssemblies'] = $RequiredAssembliesForInternalWebProxy\r\n }\r\n\u003e\u003e\u003eEXCEPTION\u003e\u003e\u003e Add-Type @AddType_prams\r\n }\r\n catch {\r\n Write-Warning -Message \"InternalWebProxy: $_\"\r\n }\r\n```\r\n~line 796\r\n```powershell\r\n try {\r\n $AddType_prams = @{\r\n TypeDefinition = $Win32HelpersSource\r\n Language = 'CSharp'\r\n ErrorAction = 'SilentlyContinue'\r\n }\r\n if ((-not $script:IsCoreCLR -or $script:IsNanoServer) -and $RequiredAssembliesForWin32Helpers) {\r\n $AddType_prams['ReferencedAssemblies'] = $RequiredAssembliesForWin32Helpers\r\n }\r\n\u003e\u003e\u003eEXCEPTION\u003e\u003e\u003e Add-Type @AddType_prams\r\n }\r\n catch {\r\n Write-Warning -Message \"Win32Helpers: $_\"\r\n }\r\n}\r\n```\r\nKBSystemTools (file - KBSystemTools.psm1):\r\n~ line 136\r\n```powershell\r\n\t$Local_ReferencedAssemblies = if ($PSVersionTable.PSVersion.Major -lt 6) {\r\n\t\tAdd-Type -Path ([IO.Path]::Combine($PSScriptRoot, 'Assemblies', 'net20', 'Cassia.dll')) -ErrorAction Stop\r\n\t\tAdd-Type -Path ([IO.Path]::Combine($PSScriptRoot, 'Assemblies', 'net472', 'NuGet.Versioning.dll')) -ErrorAction Stop\r\n\t\tAdd-Type -Path ([IO.Path]::Combine($PSScriptRoot, 'Assemblies', 'net45', 'Figgle.dll')) -ErrorAction Stop\r\n\t} else {\r\n\t\tAdd-Type -Path ([IO.Path]::Combine($PSScriptRoot, 'Assemblies', 'netstandard20', 'Cassia.dll')) -ErrorAction Stop\r\n\t\tAdd-Type -Path ([IO.Path]::Combine($PSScriptRoot, 'Assemblies', 'netstandard20', 'NuGet.Versioning.dll')) -ErrorAction Stop\r\n\t\tAdd-Type -Path ([IO.Path]::Combine($PSScriptRoot, 'Assemblies', 'netstandard13', 'Figgle.dll')) -ErrorAction Stop\r\n\t\t[String[]][IO.Directory]::EnumerateFiles([IO.Path]::Combine($PSHOME, 'ref'), '*.dll', [IO.SearchOption]::TopDirectoryOnly)\r\n\t}\r\n\u003e\u003e\u003eEXCEPTION\u003e\u003e\u003e\tAdd-Type -TypeDefinition $Local_KBSystemToolsType -ReferencedAssemblies $Local_ReferencedAssemblies -ErrorAction Stop\r\n```\r\n\r\n\r\n\r\n### Expected behavior\r\n\r\n```console\r\nAdd-Type should not fail.\r\n```\r\n\r\n\r\n### Actual behavior\r\n\r\n```console\r\n[7.2][Bukem@ATLANTIS] \u003e Update-KBModule -Verbose\r\nWARNING: InternalWebProxy: An item with the same key has already been added. Key: 1151434954\r\nWARNING: Win32Helpers: An item with the same key has already been added. Key: -620943648\r\nUpdate-KBModule: The term 'Update-KBModule' is not recognized as a name of a cmdlet, function, script file, or executable program.\r\nCheck the spelling of the name, or if a path was included, verify that the path is correct and try again.\r\n[7.2][Bukem@ATLANTIS] \u003e Import-Module KBSystemTools -Force -Verbose\r\nVERBOSE: Loading module from path 'D:\\USERS\\BUKEM\\Documents\\PowerShell\\Modules\\KBSystemTools\\KBSystemTools.psd1'.\r\nVERBOSE: Removing the imported \"Compare-KBCmdletParameter\" function.\r\nVERBOSE: Removing the imported \"Connect-KBShadowSession\" function.\r\nVERBOSE: Removing the imported \"Find-KBModulePopular\" function.\r\nVERBOSE: Removing the imported \"Find-KBModuleUpdate\" function.\r\nVERBOSE: Removing the imported \"Get-KBMcAfeeEpoPattern\" function.\r\nVERBOSE: Removing the imported \"Get-KBPublicIP\" function.\r\nVERBOSE: Removing the imported \"Get-KBSymantecMailSecurityPattern\" function.\r\nVERBOSE: Removing the imported \"Get-KBWindowsUpdate\" function.\r\nVERBOSE: Removing the imported \"Install-KBWindowsUpdate\" function.\r\nVERBOSE: Removing the imported \"Send-KBMouseLeftClick\" function.\r\nVERBOSE: Removing the imported \"Set-KBPSEConfiguration\" function.\r\nVERBOSE: Removing the imported \"Test-KBWebAccess\" function.\r\nVERBOSE: Removing the imported \"Uninstall-KBOutdatedModule\" function.\r\nVERBOSE: Removing the imported \"Update-KBHelp\" function.\r\nVERBOSE: Removing the imported \"Update-KBModule\" function.\r\nVERBOSE: Removing the imported \"Write-KBAsciiBanner\" function.\r\nVERBOSE: Loading 'FormatsToProcess' from path 'D:\\USERS\\BUKEM\\Documents\\PowerShell\\Modules\\KBSystemTools\\Data\\FormatData\\KBModulePopular.Format.ps1xml'.\r\nVERBOSE: Loading 'FormatsToProcess' from path 'D:\\USERS\\BUKEM\\Documents\\PowerShell\\Modules\\KBSystemTools\\Data\\FormatData\\KBModuleUpdate.Format.ps1xml'.\r\nVERBOSE: Loading module from path 'D:\\USERS\\BUKEM\\Documents\\PowerShell\\Modules\\KBSystemTools\\KBSystemTools.psm1'.\r\nVERBOSE: Importing function 'Compare-KBCmdletParameter'.\r\nVERBOSE: Importing function 'Connect-KBShadowSession'.\r\nVERBOSE: Importing function 'Find-KBModulePopular'.\r\nVERBOSE: Importing function 'Find-KBModuleUpdate'.\r\nVERBOSE: Importing function 'Get-KBMcAfeeEpoPattern'.\r\nVERBOSE: Importing function 'Get-KBPublicIP'.\r\nVERBOSE: Importing function 'Get-KBSymantecMailSecurityPattern'.\r\nVERBOSE: Importing function 'Get-KBWindowsUpdate'.\r\nVERBOSE: Importing function 'Install-KBWindowsUpdate'.\r\nVERBOSE: Importing function 'Send-KBMouseLeftClick'.\r\nVERBOSE: Importing function 'Set-KBPSEConfiguration'.\r\nVERBOSE: Importing function 'Test-KBWebAccess'.\r\nVERBOSE: Importing function 'Uninstall-KBOutdatedModule'.\r\nVERBOSE: Importing function 'Update-KBHelp'.\r\nVERBOSE: Importing function 'Update-KBModule'.\r\nVERBOSE: Importing function 'Write-KBAsciiBanner'.\r\n[7.2][Bukem@ATLANTIS] \u003e Update-KBModule -Verbose\r\nVERBOSE: Local module: CompletionPredictor v0.1.0\r\nVERBOSE: Local module: Microsoft.PowerShell.ConsoleGuiTools v0.6.2\r\nVERBOSE: Local module: Microsoft.PowerShell.Crescendo v1.0.0\r\nVERBOSE: Local module: ScriptBlockDisassembler v1.1.0\r\nVERBOSE: Local module: WindowsCompatibility v1.0.0\r\nVERBOSE: Cached PSGallery module: [2022-05-26 18:33:10] Microsoft.PowerShell.Crescendo v1.0.0\r\nVERBOSE: Cached PSGallery module: [2022-05-26 18:33:11] WindowsCompatibility v1.0.0\r\nVERBOSE: Cached PSGallery module: [2022-05-26 18:33:11] Microsoft.PowerShell.ConsoleGuiTools v0.6.2\r\nVERBOSE: Cached PSGallery module: [2022-05-26 18:33:10] CompletionPredictor v0.1.0\r\nVERBOSE: Cached PSGallery module: [2022-05-26 18:33:09] ScriptBlockDisassembler v1.1.0\r\nVERBOSE: Local module is up-to-date: CompletionPredictor\r\nVERBOSE: Local module is up-to-date: Microsoft.PowerShell.ConsoleGuiTools\r\nVERBOSE: Local module is up-to-date: Microsoft.PowerShell.Crescendo\r\nVERBOSE: Local module is up-to-date: ScriptBlockDisassembler\r\nVERBOSE: Local module is up-to-date: WindowsCompatibility\r\n```\r\n\r\n\r\n### Error details\r\n\r\n```console\r\nPSMessageDetails : \r\nException : System.Management.Automation.CommandNotFoundException: The term 'Update-KBModule' is not recognized as a name of a cmdlet, function, script file, or executable program.\r\n Check the spelling of the name, or if a path was included, verify that the path is correct and try again.\r\n at System.Management.Automation.CommandDiscovery.LookupCommandInfo(String commandName, CommandTypes commandTypes, SearchResolutionOptions searchResolutionOptions, CommandOrigin commandOrigin, ExecutionContext context)\r\n at System.Management.Automation.CommandDiscovery.TryModuleAutoDiscovery(String commandName, ExecutionContext context, String originalCommandName, CommandOrigin commandOrigin, SearchResolutionOptions searchResolutionOptions, CommandTypes commandTypes, Exception\u0026 lastError)\r\n at System.Management.Automation.CommandDiscovery.LookupCommandInfo(String commandName, CommandTypes commandTypes, SearchResolutionOptions searchResolutionOptions, CommandOrigin commandOrigin, ExecutionContext context)\r\n at System.Management.Automation.ExecutionContext.CreateCommand(String command, Boolean dotSource)\r\n at System.Management.Automation.PipelineOps.AddCommand(PipelineProcessor pipe, CommandParameterInternal[] commandElements, CommandBaseAst commandBaseAst, CommandRedirection[] redirections, ExecutionContext context)\r\n at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext)\r\n at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)\r\n at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)\r\nTargetObject : Update-KBModule\r\nCategoryInfo : ObjectNotFound: (Update-KBModule:String) [], CommandNotFoundException\r\nFullyQualifiedErrorId : CommandNotFoundException\r\nErrorDetails : \r\nInvocationInfo : System.Management.Automation.InvocationInfo\r\nScriptStackTrace : at \u003cScriptBlock\u003e, \u003cNo file\u003e: line 1\r\nPipelineIterationInfo : {}\r\n\r\nPSMessageDetails : \r\nException : System.ArgumentException: An item with the same key has already been added. Key: -1537659534\r\n at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)\r\n at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)\r\n at Microsoft.PowerShell.Commands.AddTypeCommand.DoEmitAndLoadAssembly(Compilation compilation, EmitOptions emitOptions)\r\n at Microsoft.PowerShell.Commands.AddTypeCommand.CompileToAssembly(List`1 syntaxTrees, CompilationOptions compilationOptions, EmitOptions emitOptions)\r\n at Microsoft.PowerShell.Commands.AddTypeCommand.SourceCodeProcessing()\r\n at Microsoft.PowerShell.Commands.AddTypeCommand.EndProcessing()\r\n at System.Management.Automation.CommandProcessorBase.Complete()\r\nTargetObject : \r\nCategoryInfo : NotSpecified: (:) [Add-Type], ArgumentException\r\nFullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.AddTypeCommand\r\nErrorDetails : \r\nInvocationInfo : System.Management.Automation.InvocationInfo\r\nScriptStackTrace : at \u003cScriptBlock\u003e, D:\\USERS\\BUKEM\\Documents\\PowerShell\\Modules\\KBSystemTools\\KBSystemTools.psm1: line 136\r\n at \u003cScriptBlock\u003e, \u003cNo file\u003e: line 1\r\nPipelineIterationInfo : {}\r\n\r\nPSMessageDetails : \r\nException : System.ArgumentException: An item with the same key has already been added. Key: -620943648\r\n at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)\r\n at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)\r\n at Microsoft.PowerShell.Commands.AddTypeCommand.DoEmitAndLoadAssembly(Compilation compilation, EmitOptions emitOptions)\r\n at Microsoft.PowerShell.Commands.AddTypeCommand.CompileToAssembly(List`1 syntaxTrees, CompilationOptions compilationOptions, EmitOptions emitOptions)\r\n at Microsoft.PowerShell.Commands.AddTypeCommand.SourceCodeProcessing()\r\n at Microsoft.PowerShell.Commands.AddTypeCommand.EndProcessing()\r\n at System.Management.Automation.CommandProcessorBase.Complete()\r\nTargetObject : \r\nCategoryInfo : NotSpecified: (:) [Add-Type], ArgumentException\r\nFullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.AddTypeCommand\r\nErrorDetails : \r\nInvocationInfo : System.Management.Automation.InvocationInfo\r\nScriptStackTrace : at \u003cScriptBlock\u003e, C:\\program files\\powershell\\7\\Modules\\PowerShellGet\\PSModule.psm1: line 796\r\n at \u003cScriptBlock\u003e, \u003cNo file\u003e: line 1\r\nPipelineIterationInfo : {}\r\n\r\nPSMessageDetails : \r\nException : System.ArgumentException: An item with the same key has already been added. Key: 1151434954\r\n at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)\r\n at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)\r\n at Microsoft.PowerShell.Commands.AddTypeCommand.DoEmitAndLoadAssembly(Compilation compilation, EmitOptions emitOptions)\r\n at Microsoft.PowerShell.Commands.AddTypeCommand.CompileToAssembly(List`1 syntaxTrees, CompilationOptions compilationOptions, EmitOptions emitOptions)\r\n at Microsoft.PowerShell.Commands.AddTypeCommand.SourceCodeProcessing()\r\n at Microsoft.PowerShell.Commands.AddTypeCommand.EndProcessing()\r\n at System.Management.Automation.CommandProcessorBase.Complete()\r\nTargetObject : \r\nCategoryInfo : NotSpecified: (:) [Add-Type], ArgumentException\r\nFullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.AddTypeCommand\r\nErrorDetails : \r\nInvocationInfo : System.Management.Automation.InvocationInfo\r\nScriptStackTrace : at \u003cScriptBlock\u003e, C:\\program files\\powershell\\7\\Modules\\PowerShellGet\\PSModule.psm1: line 517\r\n at \u003cScriptBlock\u003e, \u003cNo file\u003e: line 1\r\nPipelineIterationInfo : {}\r\n```\r\n\r\n\r\n### Environment data\r\n\r\n```powershell\r\nName Value\r\n---- -----\r\nPSVersion 7.2.4\r\nPSEdition Core\r\nGitCommitId 7.2.4\r\nOS Microsoft Windows 10.0.19044\r\nPlatform Win32NT\r\nPSCompatibleVersions {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.10032.0, 6.0.0, 6.1.0…}\r\nPSRemotingProtocolVersion 2.3\r\nSerializationVersion 1.1.0.1\r\nWSManStackVersion 3.0\r\n\r\nName Value\r\n---- -----\r\nPSVersion 7.3.0-preview.3\r\nPSEdition Core\r\nGitCommitId 7.3.0-preview.3-139-g0882737efb830d8072dc0c2830b50a74ab2166df\r\nOS Microsoft Windows 10.0.19044\r\nPlatform Win32NT\r\nPSCompatibleVersions {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.10032.0, 6.0.0, 6.1.0…}\r\nPSRemotingProtocolVersion 2.3\r\nSerializationVersion 1.1.0.1\r\nWSManStackVersion 3.0\r\n```\r\n\r\n\r\n### Visuals\r\n\r\n_No response_","author":{"url":"https://github.com/kborowinski","@type":"Person","name":"kborowinski"},"datePublished":"2022-05-26T17:42:49.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":14},"url":"https://github.com/19470/PowerShell/issues/19470"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:a8786e61-2d10-ca95-3d5a-b360be9ac78b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C9CA:135D4D:39DAE3:4DCB46:6A553B88 |
| html-safe-nonce | 3d24dcb01d9f29c5383362775202c7bbc93a817b45919e11356491a780cbf939 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDOUNBOjEzNUQ0RDozOURBRTM6NERDQjQ2OjZBNTUzQjg4IiwidmlzaXRvcl9pZCI6IjczODE4ODAxNjE2NjE0MzQ3NjAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 36e1829ec522e2115a837b02c689eae9a249a6ef1c5510a395e5b094fd00ae28 |
| hovercard-subject-tag | issue:1659386746 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/19470/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f4ffa5b5f4ec85ad8ba9a9b90d671352d56e58fbf9072349656941e6f04b07e1/PowerShell/PowerShell/issues/19470 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f4ffa5b5f4ec85ad8ba9a9b90d671352d56e58fbf9072349656941e6f04b07e1/PowerShell/PowerShell/issues/19470 |
| og:image:alt | Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kborowinski |
| hostname | github.com |
| expected-hostname | github.com |
| None | fbd8fce4d63ac394c5405faa20aa50d5c9e56b979ac43ee1b26f8e7c47c4cd89 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 811f50b757204c6a1b4da0ea780075b3e1cc9e57 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width