Fix completion crash for the SCCM provider (#20915)

This commit is contained in:
MartinGC94
2024-01-16 16:56:13 -08:00
committed by GitHub
parent b2ba584423
commit 9da657e3a8
@@ -4878,10 +4878,22 @@ namespace System.Management.Automation
private static string GetChildNameFromPsObject(dynamic psObject, char separator)
{
// The obvious solution would be to use the "PSChildName" property
// but some providers don't have it (like the Variable provider)
// So we use a substring of "PSPath" instead.
string childName = psObject.PSPath ?? string.Empty;
if (((PSObject)psObject).BaseObject is string result)
{
// The "Get-ChildItem" call for this provider returned a string that we assume is the child name.
// This is what the SCCM provider returns.
return result;
}
string childName = psObject.PSChildName;
if (childName is not null)
{
return childName;
}
// Some providers (Like the variable provider) don't include a PSChildName property
// so we get the child name from the path instead.
childName = psObject.PSPath ?? string.Empty;
int ProviderSeparatorIndex = childName.IndexOf("::", StringComparison.Ordinal);
childName = childName.Substring(ProviderSeparatorIndex + 2);
int indexOfName = childName.LastIndexOf(separator);
@@ -4892,7 +4904,7 @@ namespace System.Management.Automation
return childName.Substring(indexOfName + 1);
}
/// <summary>
/// Takes a path and rebuilds it with the specified variable replacements.
/// Also escapes special characters as needed.