No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

87 líneas
4.5KB

  1. param(
  2. [Parameter(Mandatory = $true)][int]$CampaignId,
  3. [string]$ConfigPath = (Join-Path (Split-Path -Parent $PSScriptRoot) 'config.ini'),
  4. [string]$OutputRoot
  5. )
  6. $ErrorActionPreference = 'Stop'
  7. . (Join-Path $PSScriptRoot 'campaign-common.ps1')
  8. if ($CampaignId -lt 1) { throw 'CampaignId must be positive' }
  9. $context = Get-MailContext $ConfigPath
  10. if (-not $OutputRoot) {
  11. $OutputRoot = if ($context.Settings['REPORT_DIR']) { $context.Settings['REPORT_DIR'] } else { 'reports' }
  12. }
  13. if (-not [System.IO.Path]::IsPathRooted($OutputRoot)) {
  14. $OutputRoot = Join-Path (Split-Path -Parent $ConfigPath) $OutputRoot
  15. }
  16. $OutputRoot = [System.IO.Path]::GetFullPath($OutputRoot)
  17. $null = New-Item -ItemType Directory -Path $OutputRoot -Force
  18. $db = Open-MailDatabase $context
  19. try {
  20. $command = New-DbCommand $db 'SELECT [Name] FROM [Campaign] WHERE [Id]=?'
  21. $null = $command.Parameters.Add('id', [System.Data.OleDb.OleDbType]::Integer)
  22. $command.Parameters[0].Value = $CampaignId
  23. $name = $command.ExecuteScalar()
  24. if ($null -eq $name -or $name -is [DBNull]) { throw 'Campaign not found' }
  25. $suppression = @{}
  26. $command = New-DbCommand $db 'SELECT [EmailKey],[Reason] FROM [Suppression]'
  27. $reader = $command.ExecuteReader()
  28. try {
  29. while ($reader.Read()) { $suppression[[string]$reader.GetValue(0)] = [string]$reader.GetValue(1) }
  30. }
  31. finally { $reader.Dispose() }
  32. $deliveries = New-Object System.Collections.ArrayList
  33. $command = New-DbCommand $db 'SELECT [Id],[EmailKey],[Status],[AttemptCount],[LastAttemptAtUtc],[LastErrorCode] FROM [Delivery] WHERE [CampaignId]=? ORDER BY [Id]'
  34. $null = $command.Parameters.Add('id', [System.Data.OleDb.OleDbType]::Integer)
  35. $command.Parameters[0].Value = $CampaignId
  36. $reader = $command.ExecuteReader()
  37. try {
  38. while ($reader.Read()) {
  39. $key = [string]$reader.GetValue(1)
  40. $at = if ($reader.IsDBNull(4)) { '' } else { ([DateTime]$reader.GetValue(4)).ToString('o') }
  41. $errorCode = if ($reader.IsDBNull(5)) { '' } else { [string]$reader.GetValue(5) }
  42. $reason = if ($suppression.ContainsKey($key)) { $suppression[$key] } else { '' }
  43. $null = $deliveries.Add([pscustomobject]@{
  44. CampaignId = $CampaignId; CampaignName = [string]$name;
  45. DeliveryId = [int]$reader.GetValue(0); Email = $key;
  46. Status = [string]$reader.GetValue(2); AttemptCount = [int]$reader.GetValue(3);
  47. LastAttemptUtc = $at; LastErrorCode = $errorCode; SuppressionReason = $reason
  48. })
  49. }
  50. }
  51. finally { $reader.Dispose() }
  52. $attempts = New-Object System.Collections.ArrayList
  53. $command = New-DbCommand $db 'SELECT a.[Id],a.[DeliveryId],a.[StartedAtUtc],a.[EndedAtUtc],a.[Outcome],a.[SanitizedError] FROM [Attempt] AS a INNER JOIN [Delivery] AS d ON a.[DeliveryId]=d.[Id] WHERE d.[CampaignId]=? ORDER BY a.[Id]'
  54. $null = $command.Parameters.Add('id', [System.Data.OleDb.OleDbType]::Integer)
  55. $command.Parameters[0].Value = $CampaignId
  56. $reader = $command.ExecuteReader()
  57. try {
  58. while ($reader.Read()) {
  59. $started = if ($reader.IsDBNull(2)) { '' } else { ([DateTime]$reader.GetValue(2)).ToString('o') }
  60. $ended = if ($reader.IsDBNull(3)) { '' } else { ([DateTime]$reader.GetValue(3)).ToString('o') }
  61. $errorText = if ($reader.IsDBNull(5)) { '' } else { [string]$reader.GetValue(5) }
  62. $null = $attempts.Add([pscustomobject]@{
  63. CampaignId = $CampaignId; AttemptId = [int]$reader.GetValue(0);
  64. DeliveryId = [int]$reader.GetValue(1); StartedUtc = $started;
  65. EndedUtc = $ended; Outcome = [string]$reader.GetValue(4);
  66. SanitizedError = $errorText
  67. })
  68. }
  69. }
  70. finally { $reader.Dispose() }
  71. $stamp = [DateTime]::UtcNow.ToString('yyyyMMddTHHmmssZ') + '-' + [guid]::NewGuid().ToString('N').Substring(0,8)
  72. $prefix = Join-Path $OutputRoot "campaign-$CampaignId-$stamp"
  73. $deliveryPath = $prefix + '-deliveries.csv'
  74. $attemptPath = $prefix + '-attempts.csv'
  75. $deliveries | Export-Csv -LiteralPath $deliveryPath -NoTypeInformation -Encoding UTF8
  76. if ($attempts.Count) {
  77. $attempts | Export-Csv -LiteralPath $attemptPath -NoTypeInformation -Encoding UTF8
  78. }
  79. else {
  80. 'CampaignId,AttemptId,DeliveryId,StartedUtc,EndedUtc,Outcome,SanitizedError' |
  81. Set-Content -LiteralPath $attemptPath -Encoding UTF8
  82. }
  83. Write-Output "Delivery report: $deliveryPath"
  84. Write-Output "Attempt report: $attemptPath"
  85. }
  86. finally { $db.Dispose() }

Powered by TurnKey Linux.