From 09509d2de31c928c674b71a4585078ef923899cb Mon Sep 17 00:00:00 2001 From: MartinGC94 <42123497+MartinGC94@users.noreply.github.com> Date: Sat, 9 Dec 2023 00:22:11 +0100 Subject: [PATCH] Fix completion crash for the SCCM provider (#20815) --- .../CommandCompletion/CompletionCompleters.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs index 357ce126ab..1f5c68fa31 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs @@ -4764,17 +4764,20 @@ namespace System.Management.Automation // Save relevant info and try again to get just the names. foreach (dynamic child in childItemOutput) { - childrenInfoTable.Add(GetChildNameFromPsObject(child, provider.ItemSeparator), child.PSIsContainer); + // TryAdd is used because some providers (like SCCM) may include duplicate PSPaths in a container. + _ = childrenInfoTable.TryAdd(GetChildNameFromPsObject(child, provider.ItemSeparator), child.PSIsContainer); } _ = context.Helper.CurrentPowerShell .AddCommandWithPreferenceSetting("Microsoft.PowerShell.Management\\Get-ChildItem") .AddParameter("LiteralPath", pathInfo.Path) - .AddParameter("Name"); + .AddParameter("Name") + .AddCommandWithPreferenceSetting("Microsoft.PowerShell.Utility\\Sort-Object") + .AddParameter("Unique"); childItemOutput = context.Helper.ExecuteCurrentPowerShell(out _); - foreach (var child in childItemOutput) + foreach (PSObject child in childItemOutput) { - var childName = (string)child.BaseObject; + string childName = (string)child.BaseObject; childNameList.Add(childName); } } @@ -4783,8 +4786,12 @@ namespace System.Management.Automation foreach (dynamic child in childItemOutput) { var childName = GetChildNameFromPsObject(child, provider.ItemSeparator); - childrenInfoTable.Add(childName, child.PSIsContainer); - childNameList.Add(childName); + + // TryAdd is used because some providers (like SCCM) may include duplicate PSPaths in a container. + if (childrenInfoTable.TryAdd(childName, child.PSIsContainer)) + { + childNameList.Add(childName); + } } }