// Expand a list of (possibly wildcarded) expressions into resolved expressions that // match property names on the incoming objects. private static void ExpandExpressions(PSObject inputObject, List <MshParameter> UnexpandedParametersWithWildCardPattern, List <MshParameter> expandedParameterList) { if (UnexpandedParametersWithWildCardPattern != null) { foreach (MshParameter unexpandedParameter in UnexpandedParametersWithWildCardPattern) { MshExpression ex = (MshExpression)unexpandedParameter.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey); SortedDictionary <string, MshExpression> expandedPropertyNames = new SortedDictionary <string, MshExpression>(StringComparer.OrdinalIgnoreCase); if (inputObject == null) { continue; } foreach (MshExpression resolvedName in ex.ResolveNames(PSObject.AsPSObject(inputObject))) { expandedPropertyNames[resolvedName.ToString()] = resolvedName; } foreach (MshExpression expandedExpression in expandedPropertyNames.Values) { MshParameter expandedParameter = new MshParameter(); expandedParameter.hash = (Hashtable)unexpandedParameter.hash.Clone(); expandedParameter.hash[FormatParameterDefinitionKeys.ExpressionEntryKey] = expandedExpression; expandedParameterList.Add(expandedParameter); } } } }
private void ProcessExpandParameter(MshParameter p, PSObject inputObject, List <PSNoteProperty> matchedProperties) { List <MshExpressionResult> values = (p.GetEntry("expression") as MshExpression).GetValues(inputObject); if (values.Count == 0) { ErrorRecord errorRecord = new ErrorRecord(PSTraceSource.NewArgumentException("ExpandProperty", this.ResourcesBaseName, "PropertyNotFound", new object[] { this.expand }), "ExpandPropertyNotFound", ErrorCategory.InvalidArgument, inputObject); throw new SelectObjectException(errorRecord); } if (values.Count > 1) { ErrorRecord record2 = new ErrorRecord(PSTraceSource.NewArgumentException("ExpandProperty", this.ResourcesBaseName, "MutlipleExpandProperties", new object[] { this.expand }), "MutlipleExpandProperties", ErrorCategory.InvalidArgument, inputObject); throw new SelectObjectException(record2); } MshExpressionResult result = values[0]; if (result.Exception == null) { IEnumerable enumerable = LanguagePrimitives.GetEnumerable(result.Result); if (enumerable == null) { PSObject obj2 = PSObject.AsPSObject(result.Result); this.FilteredWriteObject(obj2, matchedProperties); } else { foreach (object obj3 in enumerable) { if (obj3 != null) { PSObject obj4 = PSObject.AsPSObject(obj3); foreach (PSNoteProperty property in matchedProperties) { try { if (obj4.Properties[property.Name] != null) { this.WriteAlreadyExistingPropertyError(property.Name, inputObject, "AlreadyExistingUserSpecifiedPropertyExpand"); } else { obj4.Properties.Add(property); } } catch (ExtendedTypeSystemException) { this.WriteAlreadyExistingPropertyError(property.Name, inputObject, "AlreadyExistingUserSpecifiedPropertyExpand"); } } this.FilteredWriteObject(obj4, matchedProperties); } } } } else { ErrorRecord record3 = new ErrorRecord(result.Exception, "PropertyEvaluationExpand", ErrorCategory.InvalidResult, inputObject); throw new SelectObjectException(record3); } }
private static void EvaluateSortingExpression(MshParameter p, PSObject inputObject, List <ObjectCommandPropertyValue> orderValues, List <ErrorRecord> errors, out string propertyNotFoundMsg) { MshExpression entry = p.GetEntry("expression") as MshExpression; List <MshExpressionResult> list = entry.GetValues(inputObject, false, true); if (list.Count == 0) { orderValues.Add(ObjectCommandPropertyValue.NonExistingProperty); propertyNotFoundMsg = StringUtil.Format(SortObjectStrings.PropertyNotFound, entry.ToString()); } else { propertyNotFoundMsg = null; foreach (MshExpressionResult result in list) { if (result.Exception == null) { orderValues.Add(new ObjectCommandPropertyValue(result.Result)); } else { ErrorRecord item = new ErrorRecord(result.Exception, "ExpressionEvaluation", ErrorCategory.InvalidResult, inputObject); errors.Add(item); orderValues.Add(ObjectCommandPropertyValue.ExistingNullProperty); } } } }
private void Emit(OrderByPropertyEntry entry, string sideIndicator) { Diagnostics.Assert(entry != null, "null entry"); PSObject mshobj; if (PassThru) { mshobj = PSObject.AsPSObject(entry.inputObject); } else { mshobj = new PSObject(); if (Property == null || 0 == Property.Length) { PSNoteProperty inputNote = new PSNoteProperty( InputObjectPropertyName, entry.inputObject); mshobj.Properties.Add(inputNote); } else { List <MshParameter> mshParameterList = _orderByProperty.MshParameterList; Diagnostics.Assert(mshParameterList != null, "null mshParameterList"); Diagnostics.Assert(mshParameterList.Count == Property.Length, "mshParameterList.Count " + mshParameterList.Count); for (int i = 0; i < Property.Length; i++) { // 2005/07/05 This is the closest we can come to // the string typed by the user MshParameter mshParameter = mshParameterList[i]; Diagnostics.Assert(mshParameter != null, "null mshParameter"); Hashtable hash = mshParameter.hash; Diagnostics.Assert(hash != null, "null hash"); object prop = hash[FormatParameterDefinitionKeys.ExpressionEntryKey]; Diagnostics.Assert(prop != null, "null prop"); string propName = prop.ToString(); PSNoteProperty propertyNote = new PSNoteProperty( propName, entry.orderValues[i].PropertyValue); try { mshobj.Properties.Add(propertyNote); } catch (ExtendedTypeSystemException) { // this is probably a duplicate add } } } } mshobj.Properties.Remove(SideIndicatorPropertyName); PSNoteProperty sideNote = new PSNoteProperty( SideIndicatorPropertyName, sideIndicator); mshobj.Properties.Add(sideNote); WriteObject(mshobj); }
private void ProcessParameter(MshParameter p, PSObject inputObject, List <PSNoteProperty> result) { string entry = p.GetEntry("name") as string; MshExpression re = p.GetEntry("expression") as MshExpression; List <MshExpressionResult> list = new List <MshExpressionResult>(); foreach (MshExpression expression2 in re.ResolveNames(inputObject)) { if ((this.exclusionFilter == null) || !this.exclusionFilter.IsMatch(expression2)) { List <MshExpressionResult> values = expression2.GetValues(inputObject); if (values != null) { foreach (MshExpressionResult result2 in values) { list.Add(result2); } } } } if (list.Count == 0) { list.Add(new MshExpressionResult(null, re, null)); } else if (!string.IsNullOrEmpty(entry) && (list.Count > 1)) { ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(SelectObjectStrings.RenamingMultipleResults), "RenamingMultipleResults", ErrorCategory.InvalidOperation, inputObject); base.WriteError(errorRecord); return; } foreach (MshExpressionResult result3 in list) { if ((this.exclusionFilter == null) || !this.exclusionFilter.IsMatch(result3.ResolvedExpression)) { PSNoteProperty property; if (string.IsNullOrEmpty(entry)) { string str3 = result3.ResolvedExpression.ToString(); if (string.IsNullOrEmpty(str3)) { PSArgumentException exception = PSTraceSource.NewArgumentException("Property", this.ResourcesBaseName, "EmptyScriptBlockAndNoName", new object[0]); base.ThrowTerminatingError(new ErrorRecord(exception, "EmptyScriptBlockAndNoName", ErrorCategory.InvalidArgument, null)); } property = new PSNoteProperty(str3, result3.Result); } else { property = new PSNoteProperty(entry, result3.Result); } result.Add(property); } } }
private void WritePropertyName(StringBuilder Listtag, MshParameter p) { string entry = p.GetEntry("label") as string; if (entry != null) { Listtag.Append(entry); } else { Listtag.Append((p.GetEntry("expression") as MshExpression).ToString()); } }
/// <summary> /// To write the Property name /// </summary> private void WritePropertyName(StringBuilder Listtag, MshParameter p) { //for writing the property name string label = p.GetEntry(ConvertHTMLParameterDefinitionKeys.LabelEntryKey) as string; if (label != null) { Listtag.Append(label); } else { MshExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as MshExpression; Listtag.Append(ex.ToString()); } }
// Expand a list of (possibly wildcarded) expressions into resolved expressions that // match property names on the incoming objects. private static List <MshParameter> ExpandExpressions(List <PSObject> inputObjects, List <MshParameter> unexpandedParameterList) { List <MshParameter> expandedParameterList = new List <MshParameter>(); if (unexpandedParameterList != null) { foreach (MshParameter unexpandedParameter in unexpandedParameterList) { MshExpression ex = (MshExpression)unexpandedParameter.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey); if (!ex.HasWildCardCharacters) // this special cases 1) script blocks and 2) wildcard-less strings { expandedParameterList.Add(unexpandedParameter); } else { SortedDictionary <string, MshExpression> expandedPropertyNames = new SortedDictionary <string, MshExpression>(StringComparer.OrdinalIgnoreCase); if (inputObjects != null) { foreach (object inputObject in inputObjects) { if (inputObject == null) { continue; } foreach (MshExpression resolvedName in ex.ResolveNames(PSObject.AsPSObject(inputObject))) { expandedPropertyNames[resolvedName.ToString()] = resolvedName; } } } foreach (MshExpression expandedExpression in expandedPropertyNames.Values) { MshParameter expandedParameter = new MshParameter(); expandedParameter.hash = (Hashtable)unexpandedParameter.hash.Clone(); expandedParameter.hash[FormatParameterDefinitionKeys.ExpressionEntryKey] = expandedExpression; expandedParameterList.Add(expandedParameter); } } } } return(expandedParameterList); }
private static void EvaluateSortingExpression( MshParameter p, PSObject inputObject, List <ObjectCommandPropertyValue> orderValues, List <ErrorRecord> errors, out string propertyNotFoundMsg, ref bool comparable) { // NOTE: we assume globbing was not allowed in input PSPropertyExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression; // get the values, but do not expand aliases List <PSPropertyExpressionResult> expressionResults = ex.GetValues(inputObject, false, true); if (expressionResults.Count == 0) { // we did not get any result out of the expression: // we enter a null as a place holder orderValues.Add(ObjectCommandPropertyValue.NonExistingProperty); propertyNotFoundMsg = StringUtil.Format(SortObjectStrings.PropertyNotFound, ex.ToString()); return; } propertyNotFoundMsg = null; // we obtained some results, enter them into the list foreach (PSPropertyExpressionResult r in expressionResults) { if (r.Exception == null) { orderValues.Add(new ObjectCommandPropertyValue(r.Result)); } else { ErrorRecord errorRecord = new ErrorRecord( r.Exception, "ExpressionEvaluation", ErrorCategory.InvalidResult, inputObject); errors.Add(errorRecord); orderValues.Add(ObjectCommandPropertyValue.ExistingNullProperty); } comparable = true; } }
private void WritePropertyValue(StringBuilder Listtag, MshParameter p) { MshExpression entry = p.GetEntry("expression") as MshExpression; foreach (MshExpressionResult result in entry.GetValues(this.inputObject)) { if (result.Result != null) { string str = WebUtility.HtmlEncode(SafeToString(result.Result)); Listtag.Append(str); } Listtag.Append(", "); } if (Listtag.ToString().EndsWith(", ", StringComparison.Ordinal)) { Listtag.Remove(Listtag.Length - 2, 2); } }
private static List <MshParameter> ExpandExpressions(List <PSObject> inputObjects, List <MshParameter> unexpandedParameterList) { List <MshParameter> list = new List <MshParameter>(); if (unexpandedParameterList != null) { foreach (MshParameter parameter in unexpandedParameterList) { MshExpression entry = (MshExpression)parameter.GetEntry("expression"); if (!entry.HasWildCardCharacters) { list.Add(parameter); } else { SortedDictionary <string, MshExpression> dictionary = new SortedDictionary <string, MshExpression>(StringComparer.OrdinalIgnoreCase); if (inputObjects != null) { foreach (object obj2 in inputObjects) { if (obj2 != null) { foreach (MshExpression expression2 in entry.ResolveNames(PSObject.AsPSObject(obj2))) { dictionary[expression2.ToString()] = expression2; } } } } foreach (MshExpression expression3 in dictionary.Values) { MshParameter item = new MshParameter { hash = (Hashtable)parameter.hash.Clone() }; item.hash["expression"] = expression3; list.Add(item); } } } } return(list); }
private void Emit(OrderByPropertyEntry entry, string sideIndicator) { PSObject obj2; if (this.PassThru != 0) { obj2 = PSObject.AsPSObject(entry.inputObject); } else { obj2 = new PSObject(); if ((this.Property == null) || (this.Property.Length == 0)) { PSNoteProperty property = new PSNoteProperty("InputObject", entry.inputObject); obj2.Properties.Add(property); } else { List <MshParameter> mshParameterList = this.orderByProperty.MshParameterList; for (int i = 0; i < this.Property.Length; i++) { MshParameter parameter = mshParameterList[i]; object obj3 = parameter.hash["expression"]; PSNoteProperty property2 = new PSNoteProperty(obj3.ToString(), entry.orderValues[i].PropertyValue); try { obj2.Properties.Add(property2); } catch (ExtendedTypeSystemException) { } } } } obj2.Properties.Remove("SideIndicator"); PSNoteProperty member = new PSNoteProperty("SideIndicator", sideIndicator); obj2.Properties.Add(member); base.WriteObject(obj2); }
/// <summary> /// To write the Property value /// </summary> private void WritePropertyValue(StringBuilder Listtag, MshParameter p) { MshExpression exValue = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as MshExpression; // get the value of the property List <MshExpressionResult> resultList = exValue.GetValues(_inputObject); foreach (MshExpressionResult result in resultList) { // create comma sep list for multiple results if (result.Result != null) { string htmlEncodedResult = WebUtility.HtmlEncode(SafeToString(result.Result)); Listtag.Append(htmlEncodedResult); } Listtag.Append(", "); } if (Listtag.ToString().EndsWith(", ", StringComparison.Ordinal)) { Listtag.Remove(Listtag.Length - 2, 2); } }
private void ProcessExpandParameter(MshParameter p, PSObject inputObject, List <PSNoteProperty> matchedProperties) { MshExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as MshExpression; List <MshExpressionResult> expressionResults = ex.GetValues(inputObject); if (expressionResults.Count == 0) { ErrorRecord errorRecord = new ErrorRecord( PSTraceSource.NewArgumentException("ExpandProperty", SelectObjectStrings.PropertyNotFound, ExpandProperty), "ExpandPropertyNotFound", ErrorCategory.InvalidArgument, inputObject); throw new SelectObjectException(errorRecord); } if (expressionResults.Count > 1) { ErrorRecord errorRecord = new ErrorRecord( PSTraceSource.NewArgumentException("ExpandProperty", SelectObjectStrings.MutlipleExpandProperties, ExpandProperty), "MutlipleExpandProperties", ErrorCategory.InvalidArgument, inputObject); throw new SelectObjectException(errorRecord); } MshExpressionResult r = expressionResults[0]; if (r.Exception == null) { // ignore the property value if it's null if (r.Result == null) { return; } System.Collections.IEnumerable results = LanguagePrimitives.GetEnumerable(r.Result); if (results == null) { // add NoteProperties if there is any // If r.Result is a base object, we don't want to associate the NoteProperty // directly with it. We want the NoteProperty to be associated only with this // particular PSObject, so that when the user uses the base object else where, // its members remain the same as before the Select-Object command run. PSObject expandedObject = PSObject.AsPSObject(r.Result, true); AddNoteProperties(expandedObject, inputObject, matchedProperties); FilteredWriteObject(expandedObject, matchedProperties); return; } foreach (object expandedValue in results) { // ignore the element if it's null if (expandedValue == null) { continue; } // add NoteProperties if there is any // If expandedValue is a base object, we don't want to associate the NoteProperty // directly with it. We want the NoteProperty to be associated only with this // particular PSObject, so that when the user uses the base object else where, // its members remain the same as before the Select-Object command run. PSObject expandedObject = PSObject.AsPSObject(expandedValue, true); AddNoteProperties(expandedObject, inputObject, matchedProperties); FilteredWriteObject(expandedObject, matchedProperties); } } else { ErrorRecord errorRecord = new ErrorRecord( r.Exception, "PropertyEvaluationExpand", ErrorCategory.InvalidResult, inputObject); throw new SelectObjectException(errorRecord); } }
private void ProcessParameter(MshParameter p, PSObject inputObject, List <PSNoteProperty> result) { string name = p.GetEntry(NameEntryDefinition.NameEntryKey) as string; MshExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as MshExpression; List <MshExpressionResult> expressionResults = new List <MshExpressionResult>(); foreach (MshExpression resolvedName in ex.ResolveNames(inputObject)) { if (_exclusionFilter == null || !_exclusionFilter.IsMatch(resolvedName)) { List <MshExpressionResult> tempExprResults = resolvedName.GetValues(inputObject); if (tempExprResults == null) { continue; } foreach (MshExpressionResult mshExpRes in tempExprResults) { expressionResults.Add(mshExpRes); } } } // allow 'Select-Object -Property noexist-name' to return a PSObject with property noexist-name, // unless noexist-name itself contains wildcards if (expressionResults.Count == 0 && !ex.HasWildCardCharacters) { expressionResults.Add(new MshExpressionResult(null, ex, null)); } // if we have an expansion, renaming is not acceptable else if (!string.IsNullOrEmpty(name) && expressionResults.Count > 1) { string errorMsg = SelectObjectStrings.RenamingMultipleResults; ErrorRecord errorRecord = new ErrorRecord( new InvalidOperationException(errorMsg), "RenamingMultipleResults", ErrorCategory.InvalidOperation, inputObject); WriteError(errorRecord); return; } foreach (MshExpressionResult r in expressionResults) { // filter the exclusions, if any if (_exclusionFilter != null && _exclusionFilter.IsMatch(r.ResolvedExpression)) { continue; } PSNoteProperty mshProp; if (string.IsNullOrEmpty(name)) { string resolvedExpressionName = r.ResolvedExpression.ToString(); if (string.IsNullOrEmpty(resolvedExpressionName)) { PSArgumentException mshArgE = PSTraceSource.NewArgumentException( "Property", SelectObjectStrings.EmptyScriptBlockAndNoName); ThrowTerminatingError( new ErrorRecord( mshArgE, "EmptyScriptBlockAndNoName", ErrorCategory.InvalidArgument, null)); } mshProp = new PSNoteProperty(resolvedExpressionName, r.Result); } else { mshProp = new PSNoteProperty(name, r.Result); } result.Add(mshProp); } }