2014-04-19 56 views
0

我有一個.csv與幾百條記錄,我需要解剖到幾個不同的文件。有一部分代碼需要一個對象數組並基於數組過濾文件。它對於發現與數組中的內容相同的部分很有用,但是當我嘗試根據數組中未包含的內容進行過濾時,它會忽略任何我能找到的「不等於」運算符的版本。我認爲這與數據類型有關,但不能確定當平等運營商工作時爲什麼它會有所作爲。Powershell陣列導出不等於運營商不工作

CSV文件
"Number","Ticket","Title","Customer User","CustomerID","Accounted time","Billing" 
"1","2014041710000096","Calendar issues","george.jetson","Widget, Inc","0.25","Labor","" 
"2","2014041710000087","Redirected Folder permission","jane.smith","Mars Bars, Inc.","1","Labor","" 
"3","2014041610000203","Completed with No Files Changed ""QB Data""","will.smith","Dr. Smith","0","Labor","" 
PowerShell代碼
$msaClients = @("Widget, Inc","Johns Company") 
$billingList = import-csv "c:\billing\billed.csv" 
$idZero = "0" 

$msaArray = foreach ($msa in $msaClients) {$billingList | where-object {$_.CustomerID -eq $msa -and $_."Accounted time" -ne $idZero}} 
$laborArray = foreach ($msa in $msaClients) {$billingList | where-object {$_.CustomerID -ne $msa -and $_."Accounted time" -ne $idZero}} 

$msaArray | export-csv c:\billing\msa.csv -notypeinformation 
$laborArray | export-csv c:\billing\labor.csv -notypeinformation 

我已經嘗試了所有不同的邏輯運算符的不相等,它只是似乎忽略的那部分。如果事情看起來不正確,我還有更多的代碼。

我錯過了什麼,並預先感謝您的幫助!

回答

0

如果我理解這是正確的,你想在$ msaArray,其中$ billingList包含存在於$ msaClients,但其對應的佔時不應該(在這種情況下0)是eual至$ idzero customerIDs

PS C:\> $msaArray = ($billingList | where {(($msaclients -contains $_.customerid)) -and ($_.'accounted time' -ne $idzero)}) 
PS C:\> $msaArray | ft -auto 

Number Ticket   Title   Customer User CustomerID Accounted time Billing 
------ ------   -----   ------------- ---------- -------------- ------- 
1  2014041710000096 Calendar issues george.jetson Widget, Inc 0.25   Labor 

而對於$ laborArray,其中$ billingList不包含存在於$ msaClients及其相應的佔時不應該(在這種情況下0)是eual到idzero $以及customerIDs

PS C:\> $laborArray = ($billingList | where {(!($msaclients -contains $_.customerid)) -and ($_.'accounted time' -ne $idZero)}) 
PS C:\> $laborArray | ft -auto 

Number Ticket   Title      Customer User CustomerID  Accounted time Billing 
------ ------   -----      ------------- ----------  -------------- ------- 
2  2014041710000087 Redirected Folder permission jane.smith Mars Bars, Inc. 1    Labor 

你-ne操作員正在工作,但是你在$ msaclients中循環了太多次以獲得$ laborArray.i.e,當$ msa =「Widget,Inc」時,你得到了「Mars Bars,Inc.」作爲輸出,但foreach循環再次運行,$ msa值更改爲「Johns Company」,在這種情況下,您獲得了「Mars Bars,Inc.」和「Widget,Inc」作爲輸出。因此,你最終有三個輸出。

+0

你絕對搖滾,非常感謝你! – user3550556

+0

很高興我能幫忙:) – Nitesh