Skip to content

Commit 1085fc3

Browse files
authored
Merge pull request #584 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents 5d02168 + f42343e commit 1085fc3

File tree

15 files changed

+276
-258
lines changed

15 files changed

+276
-258
lines changed

Modules/CIPPCore/Public/Entrypoints/Activity Triggers/Push-ExecScheduledCommand.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ function Push-ExecScheduledCommand {
77
$item = $Item | ConvertTo-Json -Depth 100 | ConvertFrom-Json
88
Write-Information "We are going to be running a scheduled task: $($Item.TaskInfo | ConvertTo-Json -Depth 10)"
99

10-
$script:ScheduledTaskId = $Item.TaskInfo.RowKey
10+
# Initialize AsyncLocal storage for thread-safe per-invocation context
11+
if (-not $script:CippScheduledTaskIdStorage) {
12+
$script:CippScheduledTaskIdStorage = [System.Threading.AsyncLocal[string]]::new()
13+
}
14+
$script:CippScheduledTaskIdStorage.Value = $Item.TaskInfo.RowKey
1115

1216
$Table = Get-CippTable -tablename 'ScheduledTasks'
1317
$task = $Item.TaskInfo

Modules/CIPPCore/Public/Entrypoints/Activity Triggers/Standards/Push-CIPPStandard.ps1

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ function Push-CIPPStandard {
3838
$StandardInfo.ConditionalAccessTemplateId = $Item.Settings.TemplateList.value
3939
}
4040

41-
$Script:StandardInfo = $StandardInfo
41+
# Initialize AsyncLocal storage for thread-safe per-invocation context
42+
if (-not $script:CippStandardInfoStorage) {
43+
$script:CippStandardInfoStorage = [System.Threading.AsyncLocal[object]]::new()
44+
}
45+
$script:CippStandardInfoStorage.Value = $StandardInfo
4246

4347
try {
4448
# Convert settings to JSON, replace %variables%, then convert back to object
@@ -51,11 +55,11 @@ function Push-CIPPStandard {
5155

5256
# Prepare telemetry metadata for standard execution
5357
$metadata = @{
54-
Standard = $Standard
55-
Tenant = $Tenant
56-
TemplateId = $Item.templateId
57-
FunctionName = $FunctionName
58-
TriggerType = 'Standard'
58+
Standard = $Standard
59+
Tenant = $Tenant
60+
TemplateId = $Item.templateId
61+
FunctionName = $FunctionName
62+
TriggerType = 'Standard'
5963
}
6064

6165
# Add template-specific metadata
@@ -78,6 +82,8 @@ function Push-CIPPStandard {
7882
Write-Information $_.InvocationInfo.PositionMessage
7983
throw $_.Exception.Message
8084
} finally {
81-
Remove-Variable -Name StandardInfo -Scope Script -ErrorAction SilentlyContinue
85+
if ($script:CippStandardInfoStorage) {
86+
$script:CippStandardInfoStorage.Value = $null
87+
}
8288
}
8389
}

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Core/Invoke-ListApiTest.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function Invoke-ListApiTest {
1010

1111
$Response = @{}
1212
$Response.Request = $Request
13+
$Response.TriggerMetadata = $TriggerMetadata
1314
if ($env:DEBUG_ENV_VARS -eq 'true') {
1415
$BlockedKeys = @('ApplicationSecret', 'RefreshToken', 'AzureWebJobsStorage', 'DEPLOYMENT_STORAGE_CONNECTION_STRING')
1516
$EnvironmentVariables = [PSCustomObject]@{}
@@ -18,8 +19,8 @@ function Invoke-ListApiTest {
1819
}
1920
$Response.EnvironmentVariables = $EnvironmentVariables
2021
}
21-
$Response.AllowedTenants = $script:AllowedTenants
22-
$Response.AllowedGroups = $script:AllowedGroups
22+
$Response.AllowedTenants = $script:CippAllowedTenantsStorage.Value
23+
$Response.AllowedGroups = $script:CippAllowedGroupsStorage.Value
2324

2425
return ([HttpResponseContext]@{
2526
StatusCode = [HttpStatusCode]::OK

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/New-CippCoreRequest.ps1

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@ function New-CippCoreRequest {
1212
[CmdletBinding(SupportsShouldProcess = $true)]
1313
param($Request, $TriggerMetadata)
1414

15+
# Initialize AsyncLocal storage for thread-safe per-invocation context
16+
if (-not $script:CippInvocationIdStorage) {
17+
$script:CippInvocationIdStorage = [System.Threading.AsyncLocal[string]]::new()
18+
}
19+
if (-not $script:CippAllowedTenantsStorage) {
20+
$script:CippAllowedTenantsStorage = [System.Threading.AsyncLocal[object]]::new()
21+
}
22+
if (-not $script:CippAllowedGroupsStorage) {
23+
$script:CippAllowedGroupsStorage = [System.Threading.AsyncLocal[object]]::new()
24+
}
25+
26+
# Set InvocationId in AsyncLocal storage for console logging correlation
27+
if ($global:TelemetryClient -and $TriggerMetadata.InvocationId) {
28+
$script:CippInvocationIdStorage.Value = $TriggerMetadata.InvocationId
29+
}
30+
1531
$FunctionName = 'Invoke-{0}' -f $Request.Params.CIPPEndpoint
1632
Write-Information "API Endpoint: $($Request.Params.CIPPEndpoint) | Frontend Version: $($Request.Headers.'X-CIPP-Version' ?? 'Not specified')"
1733

@@ -58,11 +74,11 @@ function New-CippCoreRequest {
5874

5975
if ($AllowedTenants -notcontains 'AllTenants') {
6076
Write-Warning 'Limiting tenant access'
61-
$script:AllowedTenants = $AllowedTenants
77+
$script:CippAllowedTenantsStorage.Value = $AllowedTenants
6278
}
6379
if ($AllowedGroups -notcontains 'AllGroups') {
6480
Write-Warning 'Limiting group access'
65-
$script:AllowedGroups = $AllowedGroups
81+
$script:CippAllowedGroupsStorage.Value = $AllowedGroups
6682
}
6783

6884
try {

Modules/CIPPCore/Public/Functions/Get-CIPPTenantAlignment.ps1

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ function Get-CIPPTenantAlignment {
2424
[Parameter(Mandatory = $false)]
2525
[string]$TemplateId
2626
)
27-
27+
$TemplateTable = Get-CippTable -tablename 'templates'
28+
$TemplateFilter = "PartitionKey eq 'StandardsTemplateV2'"
2829
try {
2930
# Get all standard templates
30-
$TemplateTable = Get-CippTable -tablename 'templates'
31-
$TemplateFilter = "PartitionKey eq 'StandardsTemplateV2'"
32-
3331
$Templates = (Get-CIPPAzDataTableEntity @TemplateTable -Filter $TemplateFilter) | ForEach-Object {
3432
$JSON = $_.JSON -replace '"Action":', '"action":'
3533
try {
@@ -51,19 +49,30 @@ function Get-CIPPTenantAlignment {
5149
}
5250

5351
# Get standards comparison data
54-
$StandardsTable = Get-CIPPTable -TableName 'CippStandardsReports'
55-
$AllStandards = Get-CIPPAzDataTableEntity @StandardsTable -Filter "PartitionKey ne 'StandardReport' and PartitionKey ne ''"
52+
$AllStandards = Measure-CippTask -TaskName 'LoadStandardsData' -EventName 'CIPP.AlignmentStatus' -Metadata @{
53+
Tenant = $TenantFilter
54+
Section = 'LoadStandardsData'
55+
} -Script {
56+
$StandardsTable = Get-CippTable -TableName 'CippStandardsReports'
57+
#this if statement is to bring down performance when running scheduled checks, we have to revisit this to a better query due to the extreme size this can get.
58+
if ($TenantFilter) {
59+
$filter = "PartitionKey eq '$TenantFilter'"
60+
} else {
61+
$filter = "PartitionKey ne 'StandardReport' and PartitionKey ne ''"
62+
}
63+
Get-CIPPAzDataTableEntity @StandardsTable -Filter $filter
64+
}
5665

5766
# Filter by tenant if specified
5867
$Standards = if ($TenantFilter) {
59-
$AllStandards | Where-Object { $_.PartitionKey -eq $TenantFilter }
68+
$AllStandards
6069
} else {
6170
$Tenants = Get-Tenants -IncludeErrors
6271
$AllStandards | Where-Object { $_.PartitionKey -in $Tenants.defaultDomainName }
6372
}
6473

6574
# Build tenant standards data structure
66-
$TenantStandards = @{}
75+
$tenantData = @{}
6776
foreach ($Standard in $Standards) {
6877
$FieldName = $Standard.RowKey
6978
$FieldValue = $Standard.Value
@@ -86,14 +95,15 @@ function Get-CIPPTenantAlignment {
8695
$FieldValue = [string]$FieldValue
8796
}
8897

89-
if (-not $TenantStandards.ContainsKey($Tenant)) {
90-
$TenantStandards[$Tenant] = @{}
98+
if (-not $tenantData.ContainsKey($Tenant)) {
99+
$tenantData[$Tenant] = @{}
91100
}
92-
$TenantStandards[$Tenant][$FieldName] = @{
101+
$tenantData[$Tenant][$FieldName] = @{
93102
Value = $FieldValue
94103
LastRefresh = $Standard.TimeStamp.ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ')
95104
}
96105
}
106+
$TenantStandards = $tenantData
97107

98108
$Results = [System.Collections.Generic.List[object]]::new()
99109

0 commit comments

Comments
 (0)