Skip to content

Commit 127bdc6

Browse files
committed
BEC and false network timeout fixes
1 parent 1f0a3c4 commit 127bdc6

3 files changed

Lines changed: 55 additions & 27 deletions

File tree

Modules/CIPPActivityTriggers/Public/Entrypoints/Activity Triggers/BEC/Push-BECRun.ps1

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,41 @@ function Push-BECRun {
2020
$startDate = (Get-Date).AddDays(-7).ToUniversalTime()
2121
$endDate = (Get-Date)
2222
Write-Information 'Getting audit logs'
23-
$auditLog = (New-ExoRequest -tenantid $TenantFilter -cmdlet 'Get-AdminAuditLogConfig').UnifiedAuditLogIngestionEnabled
24-
$7dayslog = if ($auditLog -eq $false) {
25-
$ExtractResult = 'AuditLog is disabled. Cannot perform full analysis'
26-
} else {
27-
$sessionid = Get-Random -Minimum 10000 -Maximum 99999
28-
$operations = @(
29-
'Remove-MailboxPermission',
30-
'Add-MailboxPermission',
31-
'UpdateCalendarDelegation',
32-
'AddFolderPermissions',
33-
'MailboxLogin',
34-
'UserLoggedIn'
35-
)
36-
$startDate = (Get-Date).AddDays(-7)
37-
$endDate = (Get-Date)
38-
$SearchParam = @{
39-
SessionCommand = 'ReturnLargeSet'
40-
Operations = $operations
41-
sessionid = $sessionid
42-
startDate = $startDate
43-
endDate = $endDate
23+
try {
24+
$auditLog = (New-ExoRequest -tenantid $TenantFilter -cmdlet 'Get-AdminAuditLogConfig').UnifiedAuditLogIngestionEnabled
25+
$7dayslog = if ($auditLog -eq $false) {
26+
$ExtractResult = 'AuditLog is disabled. Cannot perform full analysis'
27+
} else {
28+
$sessionid = Get-Random -Minimum 10000 -Maximum 99999
29+
$operations = @(
30+
'Remove-MailboxPermission',
31+
'Add-MailboxPermission',
32+
'UpdateCalendarDelegation',
33+
'AddFolderPermissions',
34+
'MailboxLogin',
35+
'UserLoggedIn'
36+
)
37+
$startDate = (Get-Date).AddDays(-7)
38+
$endDate = (Get-Date)
39+
$SearchParam = @{
40+
SessionCommand = 'ReturnLargeSet'
41+
Operations = $operations
42+
sessionid = $sessionid
43+
startDate = $startDate
44+
endDate = $endDate
45+
}
46+
do {
47+
$logsTenant = New-ExoRequest -tenantid $TenantFilter -cmdlet 'Search-unifiedAuditLog' -cmdParams $SearchParam -Anchor $Username
48+
Write-Information "Retrieved $($logsTenant.count) logs"
49+
$logsTenant
50+
} while ($LogsTenant.count % 5000 -eq 0 -and $LogsTenant.count -ne 0)
51+
$ExtractResult = 'Successfully extracted logs from auditlog'
4452
}
45-
do {
46-
New-ExoRequest -tenantid $TenantFilter -cmdlet 'Search-unifiedAuditLog' -cmdParams $SearchParam -Anchor $Username
47-
Write-Information "Retrieved $($logsTenant.count) logs"
48-
$logsTenant
49-
} while ($LogsTenant.count % 5000 -eq 0 -and $LogsTenant.count -ne 0)
50-
$ExtractResult = 'Successfully extracted logs from auditlog'
53+
} catch {
54+
$7dayslog = @()
55+
$CippAuditError = Get-CippException -Exception $_
56+
$ExtractResult = "Could not retrieve audit logs: $($CippAuditError.NormalizedError)"
57+
Write-LogMessage -API 'BECRun' -message "Failed to retrieve audit logs for $($UserName): $($CippAuditError.NormalizedError)" -tenant $TenantFilter -sev Warning -LogData $CippAuditError
5158
}
5259
Write-Information 'Getting last sign-in'
5360
try {

Shared/CIPPSharp/CIPPRestClient.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,27 @@ public static async Task<HttpResult> SendAsync(
684684
{
685685
response = await client.SendAsync(request, token).ConfigureAwait(false);
686686
}
687+
catch (OperationCanceledException) when (cts is not null && cts.IsCancellationRequested)
688+
{
689+
// Our per-request timeout CTS actually fired — this is a genuine
690+
// client-side timeout after timeoutSec. Let it propagate as an
691+
// OperationCanceledException so the PowerShell wrapper reports it
692+
// as a timeout (and the "timed out after {timeoutSec}s" message is true).
693+
TrackTransportError(selection.Pool);
694+
throw;
695+
}
696+
catch (OperationCanceledException ex)
697+
{
698+
// Cancellation was NOT triggered by our timeout token. The server
699+
// reset/closed the request before our timeout elapsed — common for
700+
// slow EXO InvokeCommand cmdlets (Search-UnifiedAuditLog,
701+
// Get-MessageTraceV2) which the service cuts off well under 100s.
702+
// Surface it as a transport error so it is not mislabeled as a
703+
// client timeout and is correctly treated as a retryable failure.
704+
TrackTransportError(selection.Pool);
705+
throw new HttpRequestException(
706+
$"The request to '{uri}' was canceled by the server before completing.", ex);
707+
}
687708
catch
688709
{
689710
TrackTransportError(selection.Pool);

Shared/CIPPSharp/bin/CIPPSharp.dll

512 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)