Powershell Export-Csv没有标题行

时间:2015-03-25 01:35:27

标签: powershell

我创建了输出到csv的自定义数据对象。一切正常,但如何从输出文件中删除标题行?我已尝试过" Select -Skip"的一些变体,但这通常会生成一个空白的csv文件。以下是相关的代码:

#populate data row with user properties
$DataObject | Add-Member -MemberType NoteProperty -Name 'EventID'-Value $EventID 
$DataObject | Add-Member -MemberType NoteProperty -Name 'Computer'-Value $env:COMPUTERNAME 
$DataObject | Add-Member -MemberType NoteProperty -Name 'EngineerID' -Value $EngineerID
$DataObject | Add-Member -MemberType NoteProperty -Name 'UserID' -Value $UserID
$DataObject | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value $DateTime
$DataObject | Add-Member -MemberType NoteProperty -Name 'StepCode' -Value $StepCode
$DataObject | Add-Member -MemberType NoteProperty -Name 'Result' -Value $Result
$DataObject | Add-Member -MemberType NoteProperty -Name 'Status' -Value $HomeDirectoryAccess

#export the dataobject to a CSV file
$OutputFile = ($OutFilePath + "\Status_" + $env:COMPUTERNAME + "_" + $DateStamp + "-" + $TimeStamp + ".csv")
$DataObject | Export-Csv -Path $OutputFile -NoTypeInformation

$ DataObject如下所示:

EventID    : Test
Computer   : MD-760R102
EngineerID : Jez
UserID     : laijer
DateTime   : 25/03/2015 12:24 PM
StepCode   : USH
Result     : Complete
Status     : Home Drive Access Allowed

我也尝试过这段代码来输出数据,但它将所有数据放入csv文件的第一个单元格而不是单行中的单个单元格:

$DataOutput = $DataObject.EventID + "," + $DataObject.Computer + "," + $DataObject.EngineerID + "," + $DataObject.UserID + "," + $DataObject.DateTime + "," + $DataObject.StepCode + "," + $DataObject.Result + "," + $DataObject.Status
$DataOutput | Out-File $OutputFile

1 个答案:

答案 0 :(得分:2)

您应该能够同时使用ConvertTo-CSVSelect -Skip 1来获得您想要的内容。转换为CSV后,您必须拆分新行,因为它会输出多行字符串,但这很简单。

($DataObject | Select EventID,Computer,EngineerID,UserID,DateTime,StepCode,Result,Status | ConvertTo-Csv -NoTypeInformation).split("`n") | select -skip 1 | set-content $OutputFile

这将为您提供以下内容:

"Test","MD-760R102","Jez","laijer","25/03/2015 12=24 PM","USH","Complete","Home Drive Access Allowed"