我运行了一个MySQL查询,它给了我一个可以使用的结果集。
但是,我现在需要将其导出为CSV文件。
如果我跑
$result1 | export-csv "c:\path\to\csv.csv"
我只在第一个单元格中获得一个包含单个条目的文件:
#TYPE System.Int32
但是应该有apx 6000行。
如果我这样做
$result1 | Get-member
我明白了:
TypeName: System.Int32
Name MemberType Definition
---- ---------- ----------
CompareTo Method int CompareTo(System.Object value), int CompareTo(int value)
Equals Method bool Equals(System.Object obj), bool Equals(int obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
GetTypeCode Method System.TypeCode GetTypeCode()
ToString Method string ToString(), string ToString(string format), string ToString(System.IFormatP...
TypeName: System.Data.DataRow
Name MemberType Definition
---- ---------- ----------
AcceptChanges Method System.Void AcceptChanges()
BeginEdit Method System.Void BeginEdit()
CancelEdit Method System.Void CancelEdit()
ClearErrors Method System.Void ClearErrors()
Delete Method System.Void Delete()
EndEdit Method System.Void EndEdit()
Equals Method bool Equals(System.Object obj)
GetChildRows Method System.Data.DataRow[] GetChildRows(string relationName), System.D...
GetColumnError Method string GetColumnError(int columnIndex), string GetColumnError(str...
GetColumnsInError Method System.Data.DataColumn[] GetColumnsInError()
GetHashCode Method int GetHashCode()
GetParentRow Method System.Data.DataRow GetParentRow(string relationName), System.Dat...
GetParentRows Method System.Data.DataRow[] GetParentRows(string relationName), System....
GetType Method type GetType()
HasVersion Method bool HasVersion(System.Data.DataRowVersion version)
IsNull Method bool IsNull(int columnIndex), bool IsNull(string columnName), boo...
RejectChanges Method System.Void RejectChanges()
SetAdded Method System.Void SetAdded()
SetColumnError Method System.Void SetColumnError(int columnIndex, string error), System...
SetModified Method System.Void SetModified()
SetParentRow Method System.Void SetParentRow(System.Data.DataRow parentRow), System.V...
ToString Method string ToString()
Item ParameterizedProperty System.Object Item(int columnIndex) {get;set;}, System.Object Ite...
CJ Property System.UInt64 CustomerJobId {get;set;}
End Property System.DateTime Endtime {get;set;}
Name Property System.String Engineer {get;set;}
JN Property System.String Jobname {get;set;}
Start Property System.DateTime Starttime {get;set;}
将此转换为CSV文件的正确方法是什么?
write-host $result1
为我提供了类似下面的数据:
CJ :
JN :
Name : Mr Smith
Start :
End :
CJ : 987654321
JN :
Name : Mr Jones
Starttime : 29/09/2015 08:00:00
Endtime : 29/09/2015 08:30:00
答案 0 :(得分:2)
通过创建PSObject,然后将其转换为CSV,我找到了一种方法来完成这项工作。
$a = 0
$data = @()
foreach($res in $result1)
{
if($a -eq 0)
{
#Do Nothing
}
else
{
$cj = $res.CJ
$jn = $res.JN
$en = $res.Name
$st = $res.Start
$et = $res.End
#write-host "$cj,$jn,$en,$st,$et"
$row = New-Object PSObject
$row | Add-Member -MemberType NoteProperty -Name "CJ" -Value $cj -force
$row | Add-Member -MemberType NoteProperty -Name "JN" -Value $jn -force
$row | Add-Member -MemberType NoteProperty -Name "Name" -Value $en -force
$row | Add-Member -MemberType NoteProperty -Name "Start" -Value $st -force
$row | Add-Member -MemberType NoteProperty -Name "End" -Value $et -force
$data += $row
}
$a++
}
$data | Export-Csv "c:\path\to\csv.csv" -NoTypeInformation