将csv中的多行分组为一行并存储到变量
Grouping multiple lines in csv to one line and storing to variable
很可能非常简单,但我是新手,不确定如何搜索我想做的事情。到目前为止,我已经导入并排序了文件,但我不确定如何为客户合并多个故障并存储它们。我只是让他们写信给主机以确保我正在捕获故障。
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)"
}
}
}
您可以使用 Group-Object and Where-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
returns 一个 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
}
很可能非常简单,但我是新手,不确定如何搜索我想做的事情。到目前为止,我已经导入并排序了文件,但我不确定如何为客户合并多个故障并存储它们。我只是让他们写信给主机以确保我正在捕获故障。
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)"
}
}
}
您可以使用 Group-Object and Where-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
returns 一个 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
}