简体   繁体   中英

Powershell - How to use a variable to define columns to output in a select-object

I'm trying to cut down the output of a table to just the following columns

$tcolumns = "JobName,VMname,Sunday 08-06-2014,Saturday 07-06-2014"

$Report = $table | select $tcolumns | ConvertTo-HTML -head $style

Outputs a table that has one empty column

If I

$report = $table | select JobName,VMname,"Sunday 08-06-2014","Saturday 07-06-2014" |    ConvertTo-HTML -head $style

It outputs fine.

Any idea on how i can use a variable to define the table columns I would like to return?

OUTPUT of $table | get-member

Output of $table | get-member

TypeName: System.Data.DataRow

Name              MemberType            Definition
----              ----------            ----------
AcceptChanges     Method                void AcceptChanges()
BeginEdit         Method                void BeginEdit()
CancelEdit        Method                void CancelEdit()
ClearErrors       Method                void ClearErrors()
Delete            Method                void Delete()
EndEdit           Method                void EndEdit()
Equals            Method                bool Equals(System.Object obj)
GetChildRows      Method                System.Data.DataRow[] GetChildRows(string relationName), System.Data.DataRow...
GetColumnError    Method                string GetColumnError(int columnIndex), string GetColumnError(string columnN...
GetColumnsInError Method                System.Data.DataColumn[] GetColumnsInError()
GetHashCode       Method                int GetHashCode()
GetParentRow      Method                System.Data.DataRow GetParentRow(string relationName), System.Data.DataRow G...
GetParentRows     Method                System.Data.DataRow[] GetParentRows(string relationName), System.Data.DataRo...
GetType           Method                type GetType()
HasVersion        Method                bool HasVersion(System.Data.DataRowVersion version)
IsNull            Method                bool IsNull(int columnIndex), bool     IsNull(string columnName), bool IsNull(Sy...
RejectChanges     Method                void RejectChanges()
SetAdded          Method                void SetAdded()
SetColumnError    Method                void SetColumnError(int columnIndex, string error), void SetColumnError(stri...
SetModified       Method                void SetModified()
SetParentRow      Method                void SetParentRow(System.Data.DataRow parentRow), void SetParentRow(System.D...
ToString          Method                string ToString()
Item              ParameterizedProperty System.Object Item(int columnIndex) {get;set;}, System.Object Item(string co...
JobName           Property              string JobName {get;set;}
Monday 09-06-2014 Property              string Monday 09-06-2014 {get;set;}
Sunday 08-06-2014 Property              string Sunday 08-06-2014 {get;set;}
VMName            Property              string VMName {get;set;}

As mentioned here ( PowerShell Display Table In HTML Email ) use the ExcludeProperty

$DataSet.Tables[0] | select * -ExcludeProperty RowError, RowState, 
    HasErrors, Name, Table, ItemArray | ConvertTo-Html 

You want an array of strings here and not a single string that happens to have commas in it. :-)

$tcolumns = "JobName","VMname","Sunday 08-06-2014","Saturday 07-06-2014"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM