Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -4857,10 +4857,22 @@ private static List<CompletionResult> GetDefaultProviderResults(

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);
Expand All @@ -4871,7 +4883,7 @@ private static string GetChildNameFromPsObject(dynamic psObject, char separator)

return childName.Substring(indexOfName + 1);
}

/// <summary>
/// Takes a path and rebuilds it with the specified variable replacements.
/// Also escapes special characters as needed.
Expand Down