Powershell数组导出不等于运算符不工作

时间:2014-04-19 00:28:53

标签: arrays powershell export-to-csv logical-operators

我有一个带有几百条记录的.csv,我需要将其分解成几个不同的文件。代码的一部分采用对象数组并根据数组过滤文件。它对于找到与数组中的whats相等的部分非常有用,但是当我尝试基于数组中未包含的内容进行过滤时,它会忽略我能找到的任何版本的“不相等”运算符。我认为它与数据类型有关,但无法解释为什么当等运算符工作时它会产生影响。

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

我已经尝试了所有不同的逻辑运算符,因为它们似乎忽略了那部分。如果某些事情看起来不对,我对代码有更多的了解。

我缺少什么,并提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

如果我理解这是正确的,你需要$ msaArray中的值,其中$ billingList包含$ msaClients中存在的customerID,但它们相应的Accounted time不应该是$ idzero(在这种情况下为0)

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中存在的customerID,它们相应的Accounted time也不应该是$ idzero(在这种情况下为0)

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”也作为输出。因此,您最终得到了三个输出。