public static DataTable CastObjectsToTableView(List <PSObject> psObjects, FormatViewDefinition fvd) { var formattedObjects = psObjects.Select((ps, idx) => CastObjectToDataTableRow(ps, fvd, idx)).ToList(); var columnTypes = GetTypesForColumns(formattedObjects); var columnHeaders = GetDataColumnsForObject(psObjects.First(), fvd, columnTypes); return(new DataTable(columnHeaders, formattedObjects)); }
public static List <DataTableColumn> GetDataColumnsForObject(PSObject ps, FormatViewDefinition fvd, List <Type> types) { var labels = new List <string>(); var propertyAccesors = new List <string>(); if (fvd == null) { labels = ps.Properties.Select(x => x.Name).ToList(); propertyAccesors = ps.Properties.Select(x => $"$_.{x.Name}").ToList(); } else { var tableControl = fvd.Control as TableControl; var definedColumnLabels = tableControl.Headers.Select(x => x.Label); var displayEntries = tableControl.Rows[0].Columns.Select(x => x.DisplayEntry); var propertyLabels = displayEntries.Select(x => x.Value); //Use the TypeDefinition Label if availble otherwise just use the property name as a label labels = definedColumnLabels.Zip(propertyLabels, (definedColumnLabel, propertyLabel) => { if (String.IsNullOrEmpty(definedColumnLabel)) { return(propertyLabel); } return(definedColumnLabel); }).ToList(); propertyAccesors = displayEntries.Select(x => { //If it's a propety access directly if (x.ValueType == DisplayEntryValueType.Property) { return($"$_.{x.Value}"); } //Otherwise return access script return(x.Value); }).ToList(); } var dataColumns = new List <DataTableColumn>(); for (var i = 0; i < labels.Count; i++) { dataColumns.Add(new DataTableColumn(labels[i], i, types[i].Name, propertyAccesors[i])); } return(dataColumns); }
public static Guid GetFormatViewDefinitionInstanceId(PSObject instance) { if (instance == null) { throw PSTraceSource.NewArgumentNullException("instance"); } FormatViewDefinition baseObject = instance.BaseObject as FormatViewDefinition; if (baseObject == null) { throw PSTraceSource.NewArgumentNullException("instance"); } return(baseObject.InstanceId); }
protected override void ProcessRecord() { List <ViewDefinition> viewDefinitionList = base.Context.FormatDBManager.Database.viewDefinitionsSection.viewDefinitionList; Dictionary <string, List <FormatViewDefinition> > dictionary = new Dictionary <string, List <FormatViewDefinition> >(); foreach (ViewDefinition definition in viewDefinitionList) { foreach (TypeOrGroupReference reference in definition.appliesTo.referenceList) { PSControl control = null; if (definition.mainControl is TableControlBody) { control = new TableControl((TableControlBody)definition.mainControl); } if (definition.mainControl is ListControlBody) { control = new ListControl((ListControlBody)definition.mainControl); } if (definition.mainControl is WideControlBody) { control = new WideControl((WideControlBody)definition.mainControl); } if (control != null) { FormatViewDefinition item = new FormatViewDefinition(definition.name, control, definition.InstanceId); foreach (WildcardPattern pattern in this._filter) { if (pattern.IsMatch(reference.name)) { if (!dictionary.ContainsKey(reference.name)) { dictionary.Add(reference.name, new List <FormatViewDefinition>()); } dictionary[reference.name].Add(item); } } } } } foreach (string str in dictionary.Keys) { base.WriteObject(new ExtendedTypeDefinition(str, dictionary[str])); } }
internal static void WriteToXML(XmlWriter _writer, IEnumerable <ExtendedTypeDefinition> _typeDefinitions, bool exportScriptBlock) { _writer.WriteStartElement(_tagConfiguration); _writer.WriteStartElement(_tagViewDefinitions); Dictionary <Guid, List <ExtendedTypeDefinition> > dictionary = new Dictionary <Guid, List <ExtendedTypeDefinition> >(); Dictionary <Guid, FormatViewDefinition> dictionary2 = new Dictionary <Guid, FormatViewDefinition>(); foreach (ExtendedTypeDefinition definition in _typeDefinitions) { foreach (FormatViewDefinition definition2 in definition.FormatViewDefinition) { if (!dictionary.ContainsKey(definition2.InstanceId)) { dictionary.Add(definition2.InstanceId, new List <ExtendedTypeDefinition>()); } if (!dictionary2.ContainsKey(definition2.InstanceId)) { dictionary2.Add(definition2.InstanceId, definition2); } dictionary[definition2.InstanceId].Add(definition); } } foreach (Guid guid in dictionary2.Keys) { FormatViewDefinition definition3 = dictionary2[guid]; _writer.WriteStartElement(_tagView); _writer.WriteElementString(_tagName, definition3.Name); _writer.WriteStartElement(_tagViewSelectedBy); foreach (ExtendedTypeDefinition definition4 in dictionary[guid]) { _writer.WriteElementString(_tagTypeName, definition4.TypeName); } _writer.WriteEndElement(); definition3.Control.WriteToXML(_writer, exportScriptBlock); _writer.WriteEndElement(); } _writer.WriteEndElement(); _writer.WriteEndElement(); }
/// <summary> /// Takes out the content from the database and writes them out. /// </summary> protected override void ProcessRecord() { // Remoting detection: // * Automatic variable $PSSenderInfo is defined in true remoting contexts as well as in background jobs. // * $PSSenderInfo.ApplicationArguments.PSVersionTable.PSVersion contains the client version, as a [version] instance. // Note: Even though $PSVersionTable.PSVersion is of type [semver] in PowerShell 6+, it is of type [version] here, // presumably because only the latter type deserializes type-faithfully. var clientVersion = PowerShellVersion; PSSenderInfo remotingClientInfo = GetVariableValue("PSSenderInfo") as PSSenderInfo; if (clientVersion == null && remotingClientInfo != null) { clientVersion = PSObject.Base((PSObject.Base(remotingClientInfo.ApplicationArguments["PSVersionTable"]) as PSPrimitiveDictionary)?["PSVersion"]) as Version; } // During remoting, remain compatible with v5.0- clients by default. // Passing a -PowerShellVersion argument allows overriding the client version. bool writeOldWay = (remotingClientInfo != null && clientVersion == null) // To be safe: Remoting client version could unexpectedly not be determined. || (clientVersion != null && (clientVersion.Major < 5 || (clientVersion.Major == 5 && clientVersion.Minor < 1))); TypeInfoDataBase db = this.Context.FormatDBManager.Database; List<ViewDefinition> viewdefinitions = db.viewDefinitionsSection.viewDefinitionList; Dictionary<string, List<string>> typeGroupMap = GetTypeGroupMap(db.typeGroupSection.typeGroupDefinitionList); var typedefs = new Dictionary<ConsolidatedString, List<FormatViewDefinition>>(ConsolidatedString.EqualityComparer); foreach (ViewDefinition definition in viewdefinitions) { if (definition.isHelpFormatter) continue; var consolidatedTypeName = CreateConsolidatedTypeName(definition, typeGroupMap); if (!ShouldGenerateView(consolidatedTypeName)) continue; PSControl control; var tableControlBody = definition.mainControl as TableControlBody; if (tableControlBody != null) { control = new TableControl(tableControlBody, definition); } else { var listControlBody = definition.mainControl as ListControlBody; if (listControlBody != null) { control = new ListControl(listControlBody, definition); } else { var wideControlBody = definition.mainControl as WideControlBody; if (wideControlBody != null) { control = new WideControl(wideControlBody, definition); if (writeOldWay) { // Alignment was added to WideControl in V2, but wasn't // used. It was removed in V5, but old PowerShell clients // expect this property or fail to rehydrate the remote object. var psobj = new PSObject(control); psobj.Properties.Add(new PSNoteProperty("Alignment", 0)); } } else { var complexControlBody = (ComplexControlBody)definition.mainControl; control = new CustomControl(complexControlBody, definition); } } } // Older version of PowerShell do not know about something in the control, so // don't return it. if (writeOldWay && !control.CompatibleWithOldPowerShell()) continue; var formatdef = new FormatViewDefinition(definition.name, control, definition.InstanceId); List<FormatViewDefinition> viewList; if (!typedefs.TryGetValue(consolidatedTypeName, out viewList)) { viewList = new List<FormatViewDefinition>(); typedefs.Add(consolidatedTypeName, viewList); } viewList.Add(formatdef); } // write out all the available type definitions foreach (var pair in typedefs) { var typeNames = pair.Key; if (writeOldWay) { foreach (var typeName in typeNames) { var etd = new ExtendedTypeDefinition(typeName, pair.Value); WriteObject(etd); } } else { var etd = new ExtendedTypeDefinition(typeNames[0], pair.Value); for (int i = 1; i < typeNames.Count; i++) { etd.TypeNames.Add(typeNames[i]); } WriteObject(etd); } } }
internal void WriteToXml(IEnumerable <ExtendedTypeDefinition> typeDefinitions) { var views = new Dictionary <Guid, List <ExtendedTypeDefinition> >(); var formatdefs = new Dictionary <Guid, FormatViewDefinition>(); foreach (ExtendedTypeDefinition typedefinition in typeDefinitions) { foreach (FormatViewDefinition viewdefinition in typedefinition.FormatViewDefinition) { List <ExtendedTypeDefinition> viewList; if (!views.TryGetValue(viewdefinition.InstanceId, out viewList)) { viewList = new List <ExtendedTypeDefinition>(); views.Add(viewdefinition.InstanceId, viewList); } if (!formatdefs.ContainsKey(viewdefinition.InstanceId)) { formatdefs.Add(viewdefinition.InstanceId, viewdefinition); } viewList.Add(typedefinition); } } _writer.WriteStartElement("Configuration"); _writer.WriteStartElement("ViewDefinitions"); foreach (var pair in formatdefs) { Guid id = pair.Key; FormatViewDefinition formatdef = pair.Value; _writer.WriteStartElement("View"); _writer.WriteElementString("Name", formatdef.Name); _writer.WriteStartElement("ViewSelectedBy"); foreach (ExtendedTypeDefinition definition in views[id]) { _writer.WriteElementString("TypeName", definition.TypeName); } _writer.WriteEndElement(/*</ViewSelectedBy>*/); var groupBy = formatdef.Control.GroupBy; if (groupBy != null) { _writer.WriteStartElement("GroupBy"); WriteDisplayEntry(groupBy.Expression); if (!string.IsNullOrEmpty(groupBy.Label)) { _writer.WriteElementString("Label", groupBy.Label); } if (groupBy.CustomControl != null) { WriteCustomControl(groupBy.CustomControl); } _writer.WriteEndElement(/*</GroupBy>*/); } if (formatdef.Control.OutOfBand) { _writer.WriteElementString("OutOfBand", ""); } formatdef.Control.WriteToXml(this); _writer.WriteEndElement(/*</View>*/); } _writer.WriteEndElement(/*</ViewDefinitions>*/); _writer.WriteEndElement(/*</Configuration>*/); }
/// <summary> /// Takes out the content from the database and writes them out. /// </summary> protected override void ProcessRecord() { bool writeOldWay = PowerShellVersion == null || PowerShellVersion.Major < 5 || (PowerShellVersion.Major == 5 && PowerShellVersion.Minor < 1); TypeInfoDataBase db = this.Context.FormatDBManager.Database; List <ViewDefinition> viewdefinitions = db.viewDefinitionsSection.viewDefinitionList; Dictionary <string, List <string> > typeGroupMap = GetTypeGroupMap(db.typeGroupSection.typeGroupDefinitionList); var typedefs = new Dictionary <ConsolidatedString, List <FormatViewDefinition> >(ConsolidatedString.EqualityComparer); foreach (ViewDefinition definition in viewdefinitions) { if (definition.isHelpFormatter) { continue; } var consolidatedTypeName = CreateConsolidatedTypeName(definition, typeGroupMap); if (!ShouldGenerateView(consolidatedTypeName)) { continue; } PSControl control; var tableControlBody = definition.mainControl as TableControlBody; if (tableControlBody != null) { control = new TableControl(tableControlBody, definition); } else { var listControlBody = definition.mainControl as ListControlBody; if (listControlBody != null) { control = new ListControl(listControlBody, definition); } else { var wideControlBody = definition.mainControl as WideControlBody; if (wideControlBody != null) { control = new WideControl(wideControlBody, definition); if (writeOldWay) { // Alignment was added to WideControl in V2, but wasn't // used. It was removed in V5, but old PowerShell clients // expect this property or fail to rehydrate the remote object. var psobj = new PSObject(control); psobj.Properties.Add(new PSNoteProperty("Alignment", 0)); } } else { var complexControlBody = (ComplexControlBody)definition.mainControl; control = new CustomControl(complexControlBody, definition); } } } // Older version of PowerShell do not know about something in the control, so // don't return it. if (writeOldWay && !control.CompatibleWithOldPowerShell()) { continue; } var formatdef = new FormatViewDefinition(definition.name, control, definition.InstanceId); List <FormatViewDefinition> viewList; if (!typedefs.TryGetValue(consolidatedTypeName, out viewList)) { viewList = new List <FormatViewDefinition>(); typedefs.Add(consolidatedTypeName, viewList); } viewList.Add(formatdef); }// foreach(ViewDefinition... // write out all the available type definitions foreach (var pair in typedefs) { var typeNames = pair.Key; if (writeOldWay) { foreach (var typeName in typeNames) { var etd = new ExtendedTypeDefinition(typeName, pair.Value); WriteObject(etd); } } else { var etd = new ExtendedTypeDefinition(typeNames[0], pair.Value); for (int i = 1; i < typeNames.Count; i++) { etd.TypeNames.Add(typeNames[i]); } WriteObject(etd); } } }
public static DataTableRow CastObjectToDataTableRow(PSObject ps, FormatViewDefinition fvd, int objectIndex) { var expressions = new List <PSPropertyExpression>(); //Just iterate properties if no type def is found if (fvd == null) { foreach (var property in ps.Properties) { expressions.Add(new PSPropertyExpression(property.Name)); } } else { var tableControl = fvd.Control as TableControl; var columns = tableControl.Rows[0].Columns; foreach (var column in columns) { var displayEntry = column.DisplayEntry; if (displayEntry.ValueType == DisplayEntryValueType.Property) { expressions.Add(new PSPropertyExpression(displayEntry.Value)); } if (displayEntry.ValueType == DisplayEntryValueType.ScriptBlock) { expressions.Add(new PSPropertyExpression(ScriptBlock.Create(displayEntry.Value))); } } } // Stringify expression's results because we don't get the types from ExpressionResult var stringData = expressions.Select(x => { var result = x.GetValues(ps).FirstOrDefault().Result; if (result == null) { return(string.Empty); } return(result.ToString()); }); var data = stringData .Select <string, IValue>(x => { var isDecimal = decimal.TryParse(x, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var decimalValue); if (isDecimal) { return(new DecimalValue { Value = x, SortValue = decimalValue }); } return(new StringValue { Value = x }); }).ToList(); return(new DataTableRow(data, objectIndex)); }
private List <DataTableColumn> GetDataColumnsForObject(List <PSObject> psObjects) { var dataColumns = new List <DataTableColumn>(); foreach (PSObject obj in psObjects) { var labels = new List <string>(); FormatViewDefinition fvd = GetFormatViewDefinitionForObject(obj); var propertyAccessors = new List <string>(); if (fvd == null) { if (PSObjectIsPrimitive(obj)) { labels = new List <string> { obj.BaseObject.GetType().Name }; propertyAccessors = new List <string> { "$_" }; } else { labels = obj.Properties.Select(x => x.Name).ToList(); propertyAccessors = obj.Properties.Select(x => $"$_.{x.Name}").ToList(); } } else { var tableControl = fvd.Control as TableControl; var definedColumnLabels = tableControl.Headers.Select(x => x.Label); var displayEntries = tableControl.Rows[0].Columns.Select(x => x.DisplayEntry); var propertyLabels = displayEntries.Select(x => x.Value); //Use the TypeDefinition Label if availble otherwise just use the property name as a label labels = definedColumnLabels.Zip(propertyLabels, (definedColumnLabel, propertyLabel) => { if (String.IsNullOrEmpty(definedColumnLabel)) { return(propertyLabel); } return(definedColumnLabel); }).ToList(); propertyAccessors = displayEntries.Select(x => { //If it's a propety access directly if (x.ValueType == DisplayEntryValueType.Property) { return($"$_.{x.Value}"); } //Otherwise return access script return(x.Value); }).ToList(); } for (var i = 0; i < labels.Count; i++) { dataColumns.Add(new DataTableColumn(labels[i], propertyAccessors[i])); } } return(dataColumns.Distinct().ToList()); }