Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 将csv中的多行分组为一行并存储为变量_Powershell_Csv_Import Csv - Fatal编程技术网

Powershell 将csv中的多行分组为一行并存储为变量

Powershell 将csv中的多行分组为一行并存储为变量,powershell,csv,import-csv,Powershell,Csv,Import Csv,很可能很简单,但我是新手,不知道如何搜索我正在尝试做的事情。到目前为止,我已经导入了文件并对其进行了排序,但我不确定如何为客户组合多个故障并存储它们。我只是让他们写信给主机,以确保我捕捉到了失败 foreach ($CSV in $CSVs) { $data = Import-Csv $CSV | Sort-Object Customer $FailGroups = $data | Where Result -eq 'FAIL' | Group-Object Customer

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

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}
    }
}
CSV示例

结果,系统ID,客户
成功,123,客户时代
失败,456,客户B
失败,456,客户B
失败,789,客户B
成功,111,顾客c
失败,321,客户

我需要存储故障,以便为每个客户分别发送电子邮件。
向客户B发送电子邮件
系统(456)和(789)出现故障
向客户发送电子邮件
系统(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)"
        }     
    }
}


您可以使用和根据客户故障对数据进行分组

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
}
您可以使用和根据客户故障对数据进行分组

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
}

谢谢@Adminof Things这很好用!我真的很感激。谢谢你们@Adminof thethings这真是太棒了!我真的很感激。