-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fix Issue in Select-Object where Hashtable members (e.g., Keys) cannot be used with -Property or -ExpandProperty
#11097
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
afa6c9a
d406d2e
2e7dd02
41ce650
989bde8
532c83e
74e066c
cfea794
f7845eb
3d83000
f0802de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| using System.Collections; | ||
| using System.Collections.Generic; | ||
| using System.Collections.ObjectModel; | ||
| using System.Linq; | ||
| using System.Management.Automation; | ||
| using System.Management.Automation.Internal; | ||
| using System.Management.Automation.Language; | ||
|
|
@@ -159,27 +160,43 @@ public List<PSPropertyExpression> ResolveNames(PSObject target, bool expand) | |
|
|
||
vexx32 marked this conversation as resolved.
Show resolved
Hide resolved
vexx32 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // If the object passed in is a hashtable, then turn it into a PSCustomObject so | ||
| // that property expressions can work on it. | ||
| target = IfHashtableWrapAsPSCustomObject(target); | ||
| var wrappedTarget = IfHashtableWrapAsPSCustomObject(target, out bool wasHashtable); | ||
|
|
||
| // we have a string value | ||
| IEnumerable<PSMemberInfo> members = null; | ||
| IEnumerable<PSMemberInfo> members; | ||
| if (HasWildCardCharacters) | ||
| { | ||
| // get the members first: this will expand the globbing on each parameter | ||
| members = target.Members.Match(_stringValue, | ||
| PSMemberTypes.Properties | PSMemberTypes.PropertySet | PSMemberTypes.Dynamic); | ||
| members = wrappedTarget.Members.Match( | ||
| _stringValue, | ||
| PSMemberTypes.Properties | PSMemberTypes.PropertySet | PSMemberTypes.Dynamic); | ||
|
|
||
| // if target was a hashtable and no result is found from the keys, then use property value if available | ||
| if (wasHashtable && !members.Any()) | ||
| { | ||
| members = target.Members.Match( | ||
| _stringValue, | ||
| PSMemberTypes.Properties | PSMemberTypes.PropertySet | PSMemberTypes.Dynamic); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We evaluate this again. Maybe put this in a variable before line 172?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is evaluated against the wrapped target object in the prior case.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean |
||
| } | ||
| } | ||
| else | ||
| { | ||
| // we have no globbing: try an exact match, because this is quicker. | ||
| PSMemberInfo x = target.Members[_stringValue]; | ||
| PSMemberInfo x = wrappedTarget.Members[_stringValue]; | ||
|
|
||
| if ((x == null) && (target.BaseObject is System.Dynamic.IDynamicMetaObjectProvider)) | ||
| if (x == null) | ||
| { | ||
| // We could check if GetDynamicMemberNames includes the name... but | ||
| // GetDynamicMemberNames is only a hint, not a contract, so we'd want | ||
| // to attempt the binding whether it's in there or not. | ||
| x = new PSDynamicMember(_stringValue); | ||
| if (wasHashtable) | ||
| { | ||
| x = target.Members[_stringValue]; | ||
| } | ||
| else if (wrappedTarget.BaseObject is System.Dynamic.IDynamicMetaObjectProvider) | ||
| { | ||
| // We could check if GetDynamicMemberNames includes the name... but | ||
| // GetDynamicMemberNames is only a hint, not a contract, so we'd want | ||
| // to attempt the binding whether it's in there or not. | ||
| x = new PSDynamicMember(_stringValue); | ||
| } | ||
| } | ||
|
|
||
| List<PSMemberInfo> temp = new List<PSMemberInfo>(); | ||
|
|
@@ -269,10 +286,6 @@ public List<PSPropertyExpressionResult> GetValues(PSObject target, bool expand, | |
| { | ||
| List<PSPropertyExpressionResult> retVal = new List<PSPropertyExpressionResult>(); | ||
|
|
||
| // If the object passed in is a hashtable, then turn it into a PSCustomObject so | ||
| // that property expressions can work on it. | ||
| target = IfHashtableWrapAsPSCustomObject(target); | ||
|
|
||
| // process the script case | ||
| if (Script != null) | ||
| { | ||
|
|
@@ -282,13 +295,10 @@ public List<PSPropertyExpressionResult> GetValues(PSObject target, bool expand, | |
| return retVal; | ||
| } | ||
|
|
||
| // process the expression | ||
| List<PSPropertyExpression> resolvedExpressionList = this.ResolveNames(target, expand); | ||
|
|
||
| foreach (PSPropertyExpression re in resolvedExpressionList) | ||
| foreach (PSPropertyExpression resolvedName in ResolveNames(target, expand)) | ||
| { | ||
| PSPropertyExpressionResult r = re.GetValue(target, eatExceptions); | ||
| retVal.Add(r); | ||
| PSPropertyExpressionResult result = resolvedName.GetValue(target, eatExceptions); | ||
| retVal.Add(result); | ||
daxian-dbw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return retVal; | ||
|
|
@@ -344,13 +354,21 @@ private PSPropertyExpressionResult GetValue(PSObject target, bool eatExceptions) | |
| } | ||
| } | ||
|
|
||
| private PSObject IfHashtableWrapAsPSCustomObject(PSObject target) | ||
| private PSObject IfHashtableWrapAsPSCustomObject(PSObject target, out bool wrapped) | ||
| { | ||
| wrapped = false; | ||
|
|
||
| // If the object passed in is a hashtable, then turn it into a PSCustomObject so | ||
| // that property expressions can work on it. | ||
| if (PSObject.Base(target) is Hashtable targetAsHash) | ||
| { | ||
| target = (PSObject)(LanguagePrimitives.ConvertPSObjectToType(targetAsHash, typeof(PSObject), false, null, true)); | ||
| wrapped = true; | ||
| return (PSObject)(LanguagePrimitives.ConvertPSObjectToType( | ||
| targetAsHash, | ||
| typeof(PSObject), | ||
| recursion: false, | ||
| formatProvider: null, | ||
| ignoreUnknownMembers: true)); | ||
| } | ||
|
|
||
| return target; | ||
|
|
@@ -363,4 +381,3 @@ private PSObject IfHashtableWrapAsPSCustomObject(PSObject target) | |
| #endregion Private Members | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.