|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +[CmdletBinding()] |
| 5 | +[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseOutputTypeCorrectly", "")] |
| 6 | +[OutputType([object[]])] |
| 7 | +param( |
| 8 | + [Parameter(Mandatory)] |
| 9 | + [DateTime]$StartDate, |
| 10 | + |
| 11 | + [Parameter(Mandatory)] |
| 12 | + [DateTime]$EndDate, |
| 13 | + |
| 14 | + [Parameter()] |
| 15 | + [string]$FromIP, |
| 16 | + |
| 17 | + [Parameter()] |
| 18 | + [string[]]$MessageId, |
| 19 | + |
| 20 | + [Parameter()] |
| 21 | + [guid]$MessageTraceId, |
| 22 | + |
| 23 | + [Parameter()] |
| 24 | + [string[]]$RecipientAddress, |
| 25 | + |
| 26 | + [Parameter()] |
| 27 | + [string[]]$SenderAddress, |
| 28 | + |
| 29 | + [Parameter()] |
| 30 | + [ValidateSet("Delivered", "Expanded", "Failed", "FilteredAsSpam", "GettingStatus", "Pending", "Quarantined")] |
| 31 | + [string[]]$Status, |
| 32 | + |
| 33 | + [Parameter()] |
| 34 | + [string]$Subject, |
| 35 | + |
| 36 | + [Parameter()] |
| 37 | + [ValidateSet("Contains", "StartsWith", "EndsWith")] |
| 38 | + [string]$SubjectFilterType, |
| 39 | + |
| 40 | + [Parameter()] |
| 41 | + [string]$ToIP, |
| 42 | + |
| 43 | + [Parameter()] |
| 44 | + [ValidateRange(1, 5000)] |
| 45 | + [int]$PageSize = 5000, |
| 46 | + |
| 47 | + [Parameter()] |
| 48 | + [ValidateRange(1, 2147483647)] |
| 49 | + [int]$TimeoutMinutes = 30 |
| 50 | +) |
| 51 | + |
| 52 | +$timeout = (Get-Date).AddMinutes($TimeoutMinutes) |
| 53 | +$page = 1 |
| 54 | +$allResults = [System.Collections.Generic.List[object]]::new() |
| 55 | + |
| 56 | +# Build splat from bound parameters, excluding our custom ones |
| 57 | +$splatParams = @{} |
| 58 | +$excludeParams = @("TimeoutMinutes", "PageSize", "Verbose", "Debug", "ErrorAction", "WarningAction", |
| 59 | + "InformationAction", "ErrorVariable", "WarningVariable", |
| 60 | + "InformationVariable", "OutVariable", "OutBuffer", "PipelineVariable") |
| 61 | + |
| 62 | +foreach ($key in $PSBoundParameters.Keys) { |
| 63 | + if ($key -notin $excludeParams) { |
| 64 | + $splatParams[$key] = $PSBoundParameters[$key] |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +$splatParams["ResultSize"] = $PageSize |
| 69 | + |
| 70 | +Write-Progress -Activity "Fetching message trace data" -Status "Page $page..." |
| 71 | +$rawResults = @(Get-MessageTraceV2 @splatParams 3>&1) |
| 72 | +$results = @($rawResults | Where-Object { $_ -isnot [System.Management.Automation.WarningRecord] }) |
| 73 | +foreach ($w in ($rawResults | Where-Object { $_ -is [System.Management.Automation.WarningRecord] })) { |
| 74 | + Write-Verbose "Get-MessageTraceV2 warning (page $page): $w" |
| 75 | +} |
| 76 | +if ($results) { $allResults.AddRange($results) } |
| 77 | + |
| 78 | +$timedOut = $false |
| 79 | +while ($results.Count -eq $PageSize) { |
| 80 | + if ((Get-Date) -ge $timeout) { |
| 81 | + $timedOut = $true |
| 82 | + break |
| 83 | + } |
| 84 | + $page++ |
| 85 | + $lastResult = $results[-1] |
| 86 | + $splatParams["StartingRecipientAddress"] = $lastResult.RecipientAddress |
| 87 | + $splatParams["EndDate"] = $lastResult.Received |
| 88 | + Write-Progress -Activity "Fetching message trace data" -Status "Retrieved $($allResults.Count) messages (page $page)..." |
| 89 | + $rawResults = @(Get-MessageTraceV2 @splatParams 3>&1) |
| 90 | + $results = @($rawResults | Where-Object { $_ -isnot [System.Management.Automation.WarningRecord] }) |
| 91 | + foreach ($w in ($rawResults | Where-Object { $_ -is [System.Management.Automation.WarningRecord] })) { |
| 92 | + Write-Verbose "Get-MessageTraceV2 warning (page $page): $w" |
| 93 | + } |
| 94 | + if ($results) { $allResults.AddRange($results) } |
| 95 | +} |
| 96 | + |
| 97 | +Write-Progress -Activity "Fetching message trace data" -Completed |
| 98 | + |
| 99 | +if ($timedOut) { |
| 100 | + Write-Warning "Timed out after $TimeoutMinutes minutes with more results available. Returning $($allResults.Count) results collected so far." |
| 101 | +} |
| 102 | + |
| 103 | +Write-Verbose "Total messages retrieved: $($allResults.Count)" |
| 104 | +return $allResults |
| 105 | + |
0 commit comments