Skip to content

Commit

Permalink
Implemented Export-NonComplianceReports.ps1 (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
techlake authored Jul 16, 2023
1 parent d87eead commit 46db098
Show file tree
Hide file tree
Showing 4 changed files with 403 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Scripts/Helpers/Get-AzPolicyResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@ function Get-AzPolicyResources {
}
}
$numberPolicyResourcesProcessed++
if ($numberPolicyResourcesProcessed % 500 -eq 0) {
if ($numberPolicyResourcesProcessed % 1000 -eq 0) {
Write-Information "Processed $numberPolicyResourcesProcessed Policy resources"
}
}
if ($numberPolicyResourcesProcessed % 500 -ne 0) {
if ($numberPolicyResourcesProcessed % 1000 -ne 0) {
Write-Information "Processed $numberPolicyResourcesProcessed Policy resources"
}

Expand Down
10 changes: 8 additions & 2 deletions Scripts/Helpers/Search-AzGraphAllItems.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ function Search-AzGraphAllItems {
$null = $data.AddRange($result.Data)
while ($null -ne $result.SkipToken) {
# More data available, SkipToken will allow the next query in this loop to continue where the last invocation ended
Write-Information "Retrieved $($data.Count) $ProgressItemName"
$count = $data.Count
if ($count % 2000 -eq 0) {
Write-Information "Retrieved $count $ProgressItemName"
}
$result = Search-AzGraph $Query -First 1000 -SkipToken $result.SkipToken @scope
$null = $data.AddRange($result.Data)
}
Write-Information "Retrieved $($data.Count) $ProgressItemName"
$count = $data.Count
if ($count % 2000 -ne 0) {
Write-Information "Retrieved $($count) $ProgressItemName"
}
return $data
}
38 changes: 20 additions & 18 deletions Scripts/Operations/Create-AzRemediationTasks.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,28 @@ $result = @() + (Search-AzGraphAllItems -Query $query -Scope @{ UseTenantScope =
Write-Information ""

$remediationsList = [System.Collections.ArrayList]::new()
# Only create remediation task owned by this Policy as Code repo
$scopeTable = Get-AzScopeTree -PacEnvironment $pacEnvironment
$deployedPolicyResources = Get-AzPolicyResources -PacEnvironment $pacEnvironment -ScopeTable $scopeTable -SkipExemptions -SkipRoleAssignments
$managedAssignments = $deployedPolicyResources.policyassignments.managed
$allAssignments = $deployedPolicyResources.policyassignments.all
$strategy = $pacEnvironment.desiredState.strategy
foreach ($entry in $result) {
$policyAssignmentId = $entry.properties_policyAssignmentId
if ($OnlyCheckManagedAssignments) {
if ($managedAssignments.ContainsKey($policyAssignmentId)) {
$managedAssignment = $managedAssignments.$policyAssignmentId
$assignmentPacOwner = $managedAssignment.pacOwner
if ($assignmentPacOwner -eq "thisPaC" -or ($assignmentPacOwner -eq "unknownOwner" -and $strategy -eq "full")) {
$null = $remediationsList.Add($entry)
if ($result.Count -gt 0) {
# Only create remediation task owned by this Policy as Code repo
$scopeTable = Get-AzScopeTree -PacEnvironment $pacEnvironment
$deployedPolicyResources = Get-AzPolicyResources -PacEnvironment $pacEnvironment -ScopeTable $scopeTable -SkipExemptions -SkipRoleAssignments
$managedAssignments = $deployedPolicyResources.policyassignments.managed
$allAssignments = $deployedPolicyResources.policyassignments.all
$strategy = $pacEnvironment.desiredState.strategy
foreach ($entry in $result) {
$policyAssignmentId = $entry.properties_policyAssignmentId
if ($OnlyCheckManagedAssignments) {
if ($managedAssignments.ContainsKey($policyAssignmentId)) {
$managedAssignment = $managedAssignments.$policyAssignmentId
$assignmentPacOwner = $managedAssignment.pacOwner
if ($assignmentPacOwner -eq "thisPaC" -or ($assignmentPacOwner -eq "unknownOwner" -and $strategy -eq "full")) {
$null = $remediationsList.Add($entry)
}
}
}
}
else {
if ($allAssignments.ContainsKey($policyAssignmentId)) {
$null = $remediationsList.Add($entry)
else {
if ($allAssignments.ContainsKey($policyAssignmentId)) {
$null = $remediationsList.Add($entry)
}
}
}
}
Expand Down
Loading

0 comments on commit 46db098

Please sign in to comment.