2012-05-21 48 views
0

所以這裏是背景故事,我試圖以幾種不同的方式驗證數據。我查詢了一個數據庫並將這些數據導出到csv。精梳CSV文件powershell

然後使用PowerShell我運行下面的命令

$staleDates = Import-Csv DataAudit.csv | Where-Object { $_.asOfDate -ne "" } | Foreach-Object { $_.AsOfDate = $_.AsOfDate -as [datetime]; $_ } | Where-Object {$_.asOfDate -lt $measuredDate} 

$nullDates = Import-Csv DataAudit.csv | Where-Object {$_.asOfDate -eq ""} 

$percentTooLarge = Import-Csv DataAudit.csv | Foreach-Object { $_.SumHoldingPercent = $_.SumHoldingPercent -as [float]; $_ } | Where-Object { $_.SumHoldingPercent -gt 100 } 

$percentTooSmall = Import-Csv DataAudit.csv | Where-Object {$_.SumHoldingPercent -ne "" } | Foreach-Object { $_.SumHoldingPercent = $_.SumHoldingPercent -as [float]; $_ } | Where-Object {$_.SumHoldingPercent -lt 99.99999 } 

有沒有一種方法,我可以從這些4個變量的結果組合成一個CSV,我可以然後通過電子郵件發送出來的壞數據的報告?

也,我使用PowerShell 1.0

回答

2

下面應該工作:

$badData = Import-Csv DataAudit.csv | Where-Object { $_.asOfDate -ne "" } | Foreach-Object { $_.AsOfDate = $_.AsOfDate -as [datetime]; $_ } | Where-Object {$_.asOfDate -lt $measuredDate} 
$badData += Import-Csv DataAudit.csv | Where-Object {$_.asOfDate -eq ""} 
$badData += Import-Csv DataAudit.csv | Foreach-Object { $_.SumHoldingPercent = $_.SumHoldingPercent -as [float]; $_ } | Where-Object { $_.SumHoldingPercent -gt 100 } 
$badData += Import-Csv DataAudit.csv | Where-Object {$_.SumHoldingPercent -ne "" } | Foreach-Object { $_.SumHoldingPercent = $_.SumHoldingPercent -as [float]; $_ } | Where-Object {$_.SumHoldingPercent -lt 99.99999 } 

$badData | Export-CSV -NoTypeInformation -Path allBadData.csv 
+0

我目前得到出口-CSV的」錯誤:無法綁定參數parament‘inputobject’,因爲它是空的',這是有道理的,因爲這是用來專門定位空值,我該如何解決這個問題? – mhopkins321

+0

這就是說$ badData沒有值,如果你說這是預期的,那可能是好的catch( ) If($ badData -eq $ null){Write-Host No Bad Data。Woohoo。} Else {$ badData | Export-Csv ....} –