我正在開發自動化來觸發電子郵件,我需要向從 JSON 檔案讀取的每個電子郵件 ID(TestValue3)發送一封電子郵件,并且電子郵件的正文應該包含分別對應于電子郵件 ID 的 TestValue1、TestValue2 和 TestValue4 的值.
在這里,相同的電子郵件 ID 不止一次出現,因此我需要將各個電子郵件 ID(重復)的所有資訊分組,而不是發送多封電子郵件,并將其作為一封電子郵件發送。
{
"Data": [
{
"TestValue1": "MyCode35",
"TestValue2": "Some Test Value1",
"TestValue3": "[email protected]",
"TestValue4": "Account1"
},
{
"TestValue1": "MyCode38",
"TestValue2": "Some Test Value2",
"TestValue3": "[email protected]",
"TestValue4": "Account2"
},
{
"TestValue1": "MyCode56",
"TestValue2": "Some Test Value2",
"TestValue3": "[email protected]",
"TestValue4": "Account3"
},
{
"TestValue1": "MyCode35",
"TestValue2": "Some Test Value1",
"TestValue3": "[email protected]",
"TestValue4": "Account8"
},
{
"TestValue1": "MyCode35",
"TestValue2": "Some Test Value1",
"TestValue3": "[email protected]",
"TestValue4": "Account11"
},
{
"TestValue1": "MyCode88",
"TestValue2": "Some Test Value1",
"TestValue3": "[email protected]",
"TestValue4": "Account32"
},
{
"TestValue1": "MyCode45",
"TestValue2": "Some Test Value1",
"TestValue3": "[email protected]",
"TestValue4": "Account18"
}
]
}
更新代碼后,它回傳分組資料。
$json = Get-Content -Raw -Path .\filename.json | ConvertFrom-Json
$json | ForEach-Object Data | Group-Object -Property TestValue3
示例電子郵件正文,我將在其中使用這些 TestValue1、TestValue2 和 TestValue4
用戶詳情
代碼是:$TestValue1測驗值是:$TestValue2
帳戶是:$TestValue4
有什么想法或建議嗎?
uj5u.com熱心網友回復:
更改資料以更好地說明用法...
$json = @'
{
"Data": [
{
"Codes": "MyCode35",
"TestValue": "Some Test Value1",
"Recipient": "[email protected]",
"Account": "Account1"
},
{
"Codes": "MyCode38",
"TestValue": "Some Test Value2",
"Recipient": "[email protected]",
"Account": "Account2"
},
{
"Codes": "MyCode56",
"TestValue": "Some Test Value2",
"Recipient": "[email protected]",
"Account": "Account3"
},
{
"Codes": "MyCode35",
"TestValue": "Some Test Value1",
"Recipient": "[email protected]",
"Account": "Account8"
},
{
"Codes": "MyCode35",
"TestValue": "Some Test Value1",
"Recipient": "[email protected]",
"Account": "Account11"
},
{
"Codes": "MyCode88",
"TestValue": "Some Test Value1",
"Recipient": "[email protected]",
"Account": "Account32"
},
{
"Codes": "MyCode45",
"TestValue": "Some Test Value1",
"Recipient": "[email protected]",
"Account": "Account18"
}
]
}
'@
分組和遍歷...
$SmtpGateway = '127.0.0.1'
$GroupedRecipents = $json | ConvertFrom-Json | select -ExpandProperty Data | group Recipient
foreach ($GroupItem in $GroupedRecipents){
$body = $GroupItem.Group | Select * -ExcludeProperty Recipient | ConvertTo-Html -Fragment
Send-MailMessage `
-From '[email protected]' `
-SmtpServer $SmtpGateway `
-To $GroupItem.Name `
-Subject 'Here is the mail' `
-BodyAsHtml "$body"
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/525275.html
標籤:电源外壳
下一篇:在腳本中實作多執行緒/并行處理
