【问题标题】:Powershell Array Export not equal operator not workingPowershell 数组导出不等于运算符不起作用
【发布时间】:2014-04-19 00:28:52
【问题描述】:

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

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

我已经为不等于尝试了所有不同的逻辑运算符,但它似乎忽略了那部分。如果有些东西看起来不对,我还有更多的代码。

我错过了什么,提前感谢您的帮助!

【问题讨论】:

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


    【解决方案1】:

    如果我理解正确,您需要 $msaArray 中的值,其中 $billingList 包含存在于 $msaClients 中的客户 ID,但它们对应的计费时间不应与 $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 中的客户 ID,并且它们对应的计费时间也不应该与 $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。即,当 $msa = "Widget, Inc" 时,您获得了 "Mars Bars, Inc."。作为输出,但是 foreach 循环再次运行并且 $msa 值更改为“Johns Company”,在这种情况下,您得到了“Mars Bars, Inc.”。和“Widget, Inc”作为输出。因此,您最终得到了三个输出。

    【讨论】:

      猜你喜欢
      • 2021-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 1970-01-01
      • 2020-04-12
      相关资源
      最近更新 更多