Skip to content

Commit 8bb0212

Browse files
authored
Merge pull request #104 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents cfbbb40 + b317465 commit 8bb0212

File tree

2 files changed

+61
-22
lines changed

2 files changed

+61
-22
lines changed

Modules/CIPPCore/Public/AuditLogs/New-CIPPAuditLogSearchResultsCache.ps1

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,54 @@ function New-CIPPAuditLogSearchResultsCache {
1313
[string]$TenantFilter,
1414
[string]$SearchId
1515
)
16+
try {
17+
$FailedDownloadsTable = Get-CippTable -TableName 'FailedAuditLogDownloads'
18+
$fourHoursAgo = (Get-Date).AddHours(-4).ToUniversalTime()
19+
$failedEntity = Get-CIPPAzDataTableEntity @FailedDownloadsTable -Filter "PartitionKey eq '$TenantFilter' and SearchId eq '$SearchId' and Timestamp ge datetime'$($fourHoursAgo.ToString('yyyy-MM-ddTHH:mm:ssZ'))'"
20+
21+
if ($failedEntity) {
22+
$message = "Skipping search ID: $SearchId for tenant: $TenantFilter - Previous attempt failed within the last 4 hours"
23+
Write-LogMessage -API 'AuditLog' -tenant $TenantFilter -message $message -Sev 'Info'
24+
Write-Information $message
25+
exit 0
26+
}
27+
} catch {
28+
Write-Information "Error checking for failed downloads: $($_.Exception.Message)"
29+
# Continue with the process even if the rerun protection check fails
30+
}
1631

1732
try {
1833
Write-Information "Starting audit log cache process for tenant: $TenantFilter"
1934
$CacheWebhooksTable = Get-CippTable -TableName 'CacheWebhooks'
2035
$CacheWebhookStatsTable = Get-CippTable -TableName 'CacheWebhookStats'
21-
# Start tracking download time
36+
# Check if we haven't already downloaded this search by checking the cache table
37+
$searchEntity = Get-CIPPAzDataTableEntity @CacheWebhooksTable -Filter "PartitionKey eq '$TenantFilter' and SearchId eq '$SearchId'"
38+
if ($searchEntity) {
39+
Write-Information "Search ID: $SearchId already cached for tenant: $TenantFilter"
40+
exit 0
41+
}
42+
43+
# Record this attempt in the FailedAuditLogDownloads table BEFORE starting the download
44+
# This way, if the function is killed before completion, the record will remain
45+
try {
46+
$FailedDownloadsTable = Get-CippTable -TableName 'FailedAuditLogDownloads'
47+
$attemptId = [guid]::NewGuid().ToString()
48+
$failedEntity = @{
49+
RowKey = $attemptId
50+
PartitionKey = $TenantFilter
51+
SearchId = $SearchId
52+
ErrorMessage = 'Download attempt in progress'
53+
}
54+
Add-CIPPAzDataTableEntity @FailedDownloadsTable -Entity $failedEntity -Force
55+
Write-Information "Recorded download attempt for search ID: $SearchId, tenant: $TenantFilter"
56+
} catch {
57+
Write-Information "Failed to record download attempt: $($_.Exception.Message)"
58+
}
59+
2260
$downloadStartTime = Get-Date
23-
# Process each search and store results in cache
2461
try {
2562
Write-Information "Processing search ID: $($SearchId) for tenant: $TenantFilter"
26-
# Get the search results
27-
#check if we haven't already downloaded this search by checking the cache table, if there are items with the same search id and tenant, we skip this search
28-
$searchEntity = Get-CIPPAzDataTableEntity @CacheWebhooksTable -Filter "PartitionKey eq '$TenantFilter' and SearchId eq '$SearchId'"
29-
if ($searchEntity) {
30-
Write-Information "Search ID: $SearchId already cached for tenant: $TenantFilter"
31-
exit 0
32-
}
3363
$searchResults = Get-CippAuditLogSearchResults -TenantFilter $TenantFilter -QueryId $SearchId
34-
# Store the results in the cache table
3564
foreach ($searchResult in $searchResults) {
3665
$cacheEntity = @{
3766
RowKey = $searchResult.id
@@ -41,28 +70,33 @@ function New-CIPPAuditLogSearchResultsCache {
4170
}
4271
Add-CIPPAzDataTableEntity @CacheWebhooksTable -Entity $cacheEntity -Force
4372
}
44-
Write-Information "Successfully cached search ID: $($item.id) for tenant: $TenantFilter"
73+
Write-Information "Successfully cached search ID: $($SearchId) for tenant: $TenantFilter"
74+
try {
75+
$FailedDownloadsTable = Get-CippTable -TableName 'FailedAuditLogDownloads'
76+
$failedEntities = Get-CIPPAzDataTableEntity @FailedDownloadsTable -Filter "PartitionKey eq '$TenantFilter' and SearchId eq '$SearchId'"
77+
if ($failedEntities) {
78+
Remove-CIPPAzDataTableEntity @FailedDownloadsTable -Entity $entity
79+
Write-Information "Removed failed download records for search ID: $SearchId, tenant: $TenantFilter"
80+
}
81+
} catch {
82+
Write-Information "Failed to remove download attempt record: $($_.Exception.Message)"
83+
}
4584
} catch {
4685
throw $_
4786
}
4887

49-
# Calculate download time
5088
$downloadEndTime = Get-Date
5189
$downloadSeconds = ($downloadEndTime - $downloadStartTime).TotalSeconds
5290

53-
# Store performance metrics
5491
$statsEntity = @{
5592
RowKey = $TenantFilter
5693
PartitionKey = 'Stats'
5794
DownloadSecs = [string]$downloadSeconds
58-
SearchCount = [string]$logSearches.Count
95+
SearchCount = [string]($searchResults ? $searchResults.Count : 0)
5996
}
60-
6197
Add-CIPPAzDataTableEntity @CacheWebhookStatsTable -Entity $statsEntity -Force
62-
6398
Write-Information "Completed audit log cache process for tenant: $TenantFilter. Download time: $downloadSeconds seconds"
64-
65-
return $logSearches.Count
99+
return ($searchResults ? $searchResults.Count : 0)
66100
} catch {
67101
Write-Information "Error in New-CIPPAuditLogSearchResultsCache for tenant: $TenantFilter. Error: $($_.Exception.Message)"
68102
throw $_

Modules/CIPPCore/Public/Set-CIPPSAMAdminRoles.ps1

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@ function Set-CIPPSAMAdminRoles {
2020
$SAMRolesTable = Get-CIPPTable -tablename 'SAMRoles'
2121
$Roles = Get-CIPPAzDataTableEntity @SAMRolesTable
2222

23-
$SAMRoles = $Roles.Roles | ConvertFrom-Json
24-
$Tenants = $Roles.Tenants | ConvertFrom-Json
25-
if ($Tenants.value) {
26-
$Tenants = $Tenants.value
23+
try {
24+
$SAMRoles = $Roles.Roles | ConvertFrom-Json -ErrorAction Stop
25+
$Tenants = $Roles.Tenants | ConvertFrom-Json -ErrorAction Stop
26+
if ($Tenants.value) {
27+
$Tenants = $Tenants.value
28+
}
29+
} catch {
30+
$ActionLogs.Add('CIPP-SAM roles not configured')
31+
return $ActionLogs
2732
}
2833

2934
if (($SAMRoles | Measure-Object).count -gt 0 -and $Tenants -contains $TenantFilter -or $Tenants -contains 'AllTenants') {

0 commit comments

Comments
 (0)