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