【问题标题】:Grouping multiple lines in csv to one line and storing to variable将csv中的多行分组为一行并存储到变量
【发布时间】:2020-07-31 10:41:27
【问题描述】:

很可能非常简单,但我是新手,不太确定如何搜索我想要做的事情。到目前为止,我已经导入并排序了文件,但我不确定如何为客户组合多个故障并存储它们。我只是让他们写信给主机,以确保我捕捉到了失败。

CSV 示例

结果、系统ID、客户
成功,123,客户A
失败,456,客户B
失败,456,客户B
失败,789,客户B
成功,111,客户C
失败,321,客户 D

我需要为每个客户分别存储要通过电子邮件发送的故障。
向客户 B 发送电子邮件
System/s (456) & (789) 失败
电子邮件客户 D
System/s (321) 失败

Write-Host "CSVs: $CSVs"
foreach ($CSV in $CSVs) {
    $data = Import-Csv $CSV | Sort-Object Customer
    foreach ($line in $data) {
        if ($line.Result -eq "fail") {
            Write-Host  " Action should be taken for $($line.customer)" -ForegroundColor Green -BackgroundColor Black
            Write-Host "$($line.Result) on SystemID's $($line.SystemID)"
        }     
    }
}


【问题讨论】:

    标签: powershell csv import-csv


    【解决方案1】:

    您可以使用Group-ObjectWhere-Object 根据客户失败对数据进行分组。

    foreach ($CSV in $CSVs) {
        $data = Import-Csv $CSV | Sort-Object Customer
        $FailGroups = $data | Where Result -eq 'FAIL' | Group-Object Customer
        foreach ($FailGroup in $FailGroups) {
            $FailedCustomer = $FailGroup.Name
            $FailedSystems = ($FailGroup.Group.SystemID | Foreach-Object { "($_)" }) -join ' & '
            $MailMessage = "Systems/s {0} have failed" -f $FailedSystems
            $FailedCustomer # Outputting customers with failures
            $MailMessage # Outputting failed systems in formatted message
            # Below is a Custom Object that can be exported to CSV or retrieved later
            # [pscustomobject]@{'Customer' = $FailedCustomer; 'Message' = $MailMessage}
        }
    }
    

    您现在可以使用 $FailedCustomer$MailMessage 在内部 foreach 迭代结束时制作您的电子邮件。

    由于Group-Object 返回GroupInfo 对象,您需要访问其Name 属性以获取用于确定分组的属性值。 Group 属性包含分组中包含的 CSV 行(对象)。


    如果您使用自定义对象路径,您可以将所有输出作为数组存储到单个变量中。然后,您可以将数组导出到 CSV 文件或遍历数组以完成电子邮件任务。

    $output = foreach ($CSV in $CSVs) {
        $data = Import-Csv $CSV | Sort-Object Customer
        $FailGroups = $data | Where Result -eq 'FAIL' | Group-Object Customer
        foreach ($FailGroup in $FailGroups) {
            $FailedCustomer = $FailGroup.Name
            $FailedSystems = ($FailGroup.Group.SystemID | Foreach-Object { "($_)" }) -join ' & '
            $MailMessage = "Systems/s {0} have failed" -f $FailedSystems
            [pscustomobject]@{'Customer' = $FailedCustomer; 'Message' = $MailMessage}
        }
    }
    # Export to CSV
    $output | Export-Csv -Path C:\Path\FailedCustomers.csv -NoType
    # Email customers example
    foreach ($customer in $output) {
        $MailParams = @{
            'SmtpServer' = 'smtp.domain.com'
            'Subject' = 'Failures'
            'To' = $customer.Customer
            'From' = you@domain.com
            'Body' = $customer.Message
        }
        Send-MailMessage @MailParams
    }
    

    【讨论】:

    • 谢谢@AdminOfThings 这很好用!我真的很感激。
    猜你喜欢
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    • 2016-04-03
    • 2013-11-23
    • 1970-01-01
    • 2021-02-18
    • 2018-02-07
    • 1970-01-01
    相关资源
    最近更新 更多