Skip to content

Commit ea19d53

Browse files
Nickul-MSFTCopilot
andcommitted
Refactor pagination to call Get-MessageTraceV2 directly instead of Invoke-Command
- Use StartingRecipientAddress and EndDate from last result for pagination - Remove warning message parsing and dynamic scriptblock execution - Move timeout check inside loop to only warn when data is actually truncated Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent fecae41 commit ea19d53

1 file changed

Lines changed: 13 additions & 15 deletions

File tree

Transport/Get-AllMessageTraceResults.ps1

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,31 +68,29 @@ function Get-AllMessageTraceResults {
6868

6969
$splatParams["ResultSize"] = $PageSize
7070

71-
$moreResultsPrefix = "There are more results, use the following command to get more. "
72-
7371
Write-Progress -Activity "Fetching message trace data" -Status "Page $page..."
74-
$results = Get-MessageTraceV2 @splatParams -WarningVariable moreAvailable 3>$null
72+
$results = Get-MessageTraceV2 @splatParams 3>$null
7573
if ($results) { $allResults.AddRange(@($results)) }
7674

77-
$nextCommand = "Get-MessageTraceV2 @splatParams"
78-
$moreResultsMessage = if ($moreAvailable.Count -gt 0) { $moreAvailable[-1].ToString() } else { "" }
79-
while ($results.Count -eq $PageSize -and $moreResultsMessage.StartsWith($moreResultsPrefix) -and (Get-Date) -lt $timeout) {
75+
$timedOut = $false
76+
while ($results.Count -eq $PageSize) {
77+
if ((Get-Date) -ge $timeout) {
78+
$timedOut = $true
79+
break
80+
}
8081
$page++
82+
$lastResult = $results[-1]
83+
$splatParams["StartingRecipientAddress"] = $lastResult.RecipientAddress
84+
$splatParams["EndDate"] = $lastResult.Received
8185
Write-Progress -Activity "Fetching message trace data" -Status "Retrieved $($allResults.Count) messages (page $page)..."
82-
$nextCommand = $moreResultsMessage.Substring($moreResultsPrefix.Length)
83-
$results = Invoke-Command -ScriptBlock ([ScriptBlock]::Create($nextCommand)) -WarningVariable moreAvailable 3>$null
86+
$results = Get-MessageTraceV2 @splatParams 3>$null
8487
if ($results) { $allResults.AddRange(@($results)) }
85-
$moreResultsMessage = if ($moreAvailable.Count -gt 0) { $moreAvailable[-1].ToString() } else { "" }
86-
}
87-
88-
if ($results.Count -eq $PageSize -and -not $moreResultsMessage.StartsWith($moreResultsPrefix)) {
89-
Write-Warning "Get-MessageTraceV2 pagination failed with unexpected warning message: '$moreResultsMessage'. Last command: '$nextCommand'"
9088
}
9189

9290
Write-Progress -Activity "Fetching message trace data" -Completed
9391

94-
if ((Get-Date) -ge $timeout) {
95-
Write-Warning "Timed out after $TimeoutMinutes minutes. Returning $($allResults.Count) results collected so far."
92+
if ($timedOut) {
93+
Write-Warning "Timed out after $TimeoutMinutes minutes with more results available. Returning $($allResults.Count) results collected so far."
9694
}
9795

9896
Write-Verbose "Total messages retrieved: $($allResults.Count)"

0 commit comments

Comments
 (0)