【问题标题】:Powershell: compare and filter SQL-Reply/Array with filecontentPowershell:将 SQL-Reply/Array 与文件内容进行比较和过滤
【发布时间】:2020-11-02 22:35:10
【问题描述】:

我有一个预设的 SQL 查询和存储在一个数组中的结果集,我想过滤该数组以使用来自单独文件内容 (*.txt) 的 documentID 进行进一步处理
结果集包含一个 documentID 和一个 dateStored 属性。 seapare 文件的每一行都包含文档 ID。

    # compare filecontent and sql reply
if(![string]::IsNullOrEmpty($filecontent)){
    "INFO:`tReporting File is not empty." | Write-Logfile
    if($sql_reply.Length -gt 0) {
        $missing_in_db = [string[]](Compare-Object $filecontent $sql_reply.documentId | Where {$_.sideindicator -eq "<="} | % {$_.inputobject})   
        echo Missing: `t $missing_in_db

        $exist_in_db = [string[]](Compare-Object $filecontent $sql_reply.documentId  -IncludeEqual -ExcludeDifferent | Where {$_.sideindicator -eq "=="} | % {$_.inputobject})   
        echo Exists: `t$exist_in_db

        #Need adivce/help with this part:
        $filteredResultSet =  $sql_reply | where{$sql_reply.documentID in $exist_in_db}
     }
 }

要么我需要 $exist_in_db 数组来拥有所有 $sql_reply 属性,要么从 $filecontent 中过滤出具有匹配文档 ID 的原始 $sql_reply。 关于如何实现这一点的任何提示?

编辑:感谢 MBo 的提示!这就是我按预期工作的方式:

$mailbody += "<table>
                <tr>
                    <td><b>DocumentID</b></td>
                    <td><b>ItemName</b></td>
                    <td><b>ImportDate</b></td>
                </tr>"

 foreach($e in $exist_in_db) {
    $mailbody += "<tr>
                    <td>" + $sql_reply.Where({$_.documentID -eq $e}).documentId + "</td>
                    <td>" + $sql_reply.Where({$_.documentID -eq $e}).itemname + "</td>
                    <td>" + $sql_reply.Where({$_.documentID -eq $e}).datestored.toString("dd.MM.yyyy HH:mm:ss") + "</td>
                  </tr>"
 }

【问题讨论】:

  • 看来您已经编写了代码来实现这一点。它有什么问题?
  • $filteredResultSet 的行实际上是“Pseuco-Code”,分别是“我希望它如何工作”,但管道“| where ...”后面的部分实际上是错误的

标签: sql powershell filter resultset


【解决方案1】:

例如循环遍历db中的存在并在sql回复中找到相应的对象,例如:

    $sql_reply = ConvertFrom-Csv @'
    Name,Article,Size
    David,TShirt,M
    Eduard,Trouwsers,S
    Marc,Trouwsers,L
    Reto,Trouwsers,XS
    '@

    $exist_in_db=@("S","L","M")

    foreach ($s in $exist_in_db)
    {
        $sql_reply.Where({$_.Size -eq $s}).Name
        $sql_reply.Where({$_.Size -eq $s}).Article
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多