Skip to content

Commit 4bf8717

Browse files
authored
Merge pull request #14 from mswantek68/fix/diagnostics
Update breakfix on diagnostics ps
2 parents 0c73ea3 + aa7bbf8 commit 4bf8717

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

scripts/defender/defenderForAI/07-Enable-Diagnostics.ps1

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@ foreach($cat in $cats){
1818
if(-not $logCategories){ $logCategories = @("AllLogs") }
1919
if(-not $metricCategories){ $metricCategories = @("AllMetrics") }
2020

21+
function Resolve-RequestedCategories {
22+
param(
23+
[string[]]$Requested,
24+
[string[]]$Available,
25+
[string]$AllToken,
26+
[string]$TypeName,
27+
[string]$ResourceLabel
28+
)
29+
30+
if(-not $Available -or $Available.Count -eq 0){ return @() }
31+
if($Requested -contains $AllToken){ return @($Available) }
32+
$resolved = @($Requested | Where-Object { $Available -contains $_ })
33+
$missing = @($Requested | Where-Object { $Available -notcontains $_ })
34+
if($missing.Count -gt 0){
35+
Write-Host "Skipping unsupported $TypeName categories on ${ResourceLabel}: $($missing -join ', ')" -ForegroundColor Yellow
36+
}
37+
return $resolved
38+
}
39+
2140
function Resolve-WorkspaceResourceId([string]$workspaceId, $specObj){
2241
if(-not $workspaceId){ throw "Log Analytics workspace identifier is empty." }
2342
if($workspaceId -match '^/'){ return $workspaceId }
@@ -73,8 +92,20 @@ foreach($r in $spec.foundry.resources){
7392
if($diagResourceId -ne $r.resourceId){
7493
Write-Host "Diagnostics not supported at project scope; using parent $diagResourceId" -ForegroundColor Yellow
7594
}
76-
$logSettings = foreach($log in $logCategories){ New-AzDiagnosticSettingLogSettingsObject -Enabled $true -Category $log }
77-
$metricSettings = foreach($metric in $metricCategories){ New-AzDiagnosticSettingMetricSettingsObject -Enabled $true -Category $metric }
95+
$availableCats = Get-AzDiagnosticSettingCategory -ResourceId $diagResourceId -ErrorAction SilentlyContinue
96+
$availableLogs = @($availableCats | Where-Object { $_.CategoryType -eq 'Logs' } | Select-Object -ExpandProperty Name)
97+
$availableMetrics = @($availableCats | Where-Object { $_.CategoryType -eq 'Metrics' } | Select-Object -ExpandProperty Name)
98+
99+
$resolvedLogs = Resolve-RequestedCategories -Requested $logCategories -Available $availableLogs -AllToken 'AllLogs' -TypeName 'log' -ResourceLabel $scopeName
100+
$resolvedMetrics = Resolve-RequestedCategories -Requested $metricCategories -Available $availableMetrics -AllToken 'AllMetrics' -TypeName 'metric' -ResourceLabel $scopeName
101+
102+
if((-not $resolvedLogs -or $resolvedLogs.Count -eq 0) -and (-not $resolvedMetrics -or $resolvedMetrics.Count -eq 0)){
103+
Write-Host "No supported diagnostic categories for $scopeName. Skipping." -ForegroundColor Yellow
104+
continue
105+
}
106+
107+
$logSettings = foreach($log in $resolvedLogs){ New-AzDiagnosticSettingLogSettingsObject -Enabled $true -Category $log }
108+
$metricSettings = foreach($metric in $resolvedMetrics){ New-AzDiagnosticSettingMetricSettingsObject -Enabled $true -Category $metric }
78109
New-AzDiagnosticSetting -Name $name -ResourceId $diagResourceId -WorkspaceId $workspaceResourceId -Log $logSettings -Metric $metricSettings | Out-Null
79110
Write-Host "Enabled diagnostics for $scopeName" -ForegroundColor Green
80111
}

0 commit comments

Comments
 (0)