|
7 | 7 | Discover all custom policy exemptions at the provided scope (Management Groups, subscriptions or resource groups)
|
8 | 8 | .PARAMETER ScopeObject
|
9 | 9 | The scope object representing the azure entity to retrieve excemptions for.
|
| 10 | + .PARAMETER Subscription |
| 11 | + Complete Subscription list |
| 12 | + .PARAMETER SubscriptionsToIncludeResourceGroups |
| 13 | + Scoped Subscription list |
| 14 | + .PARAMETER ResourceGroup |
| 15 | + ResourceGroup switch indicating desired scope condition |
10 | 16 | .EXAMPLE
|
11 | 17 | > Get-AzOpsPolicyExemption -ScopeObject (New-AzOpsScope -Scope /providers/Microsoft.Management/managementGroups/contoso -StatePath $StatePath)
|
12 | 18 | Discover all custom policy exemptions deployed at Management Group scope
|
13 | 19 | #>
|
14 | 20 |
|
15 |
| - [OutputType([Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.Policy.PsPolicyExemption])] |
| 21 | + [OutputType([Microsoft.Azure.PowerShell.Cmdlets.Policy.Models.IPolicyExemption])] |
16 | 22 | [CmdletBinding()]
|
17 | 23 | param (
|
18 | 24 | [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
|
19 | 25 | [Object]
|
20 |
| - $ScopeObject |
| 26 | + $ScopeObject, |
| 27 | + [Parameter(Mandatory = $false)] |
| 28 | + [object] |
| 29 | + $Subscription, |
| 30 | + [Parameter(Mandatory = $false)] |
| 31 | + [object] |
| 32 | + $SubscriptionsToIncludeResourceGroups, |
| 33 | + [Parameter(Mandatory = $false)] |
| 34 | + [bool] |
| 35 | + $ResourceGroup |
21 | 36 | )
|
22 | 37 |
|
23 | 38 | process {
|
24 | 39 | if ($ScopeObject.Type -notin 'resourceGroups', 'subscriptions', 'managementGroups') {
|
25 | 40 | return
|
26 | 41 | }
|
27 |
| - |
28 |
| - switch ($ScopeObject.Type) { |
29 |
| - managementGroups { |
30 |
| - Write-AzOpsMessage -LogLevel Debug -LogString 'Get-AzOpsPolicyExemption.ManagementGroup' -LogStringValues $ScopeObject.ManagementGroupDisplayName, $ScopeObject.ManagementGroup -Target $ScopeObject |
| 42 | + if ($ScopeObject.Type -eq 'managementGroups') { |
| 43 | + Write-AzOpsMessage -LogLevel Debug -LogString 'Get-AzOpsPolicyExemption.ManagementGroup' -LogStringValues $ScopeObject.ManagementGroupDisplayName, $ScopeObject.ManagementGroup -Target $ScopeObject |
| 44 | + if ((-not $SubscriptionsToIncludeResourceGroups) -or (-not $ResourceGroups)) { |
| 45 | + $query = "policyresources | where type == 'microsoft.authorization/policyexemptions' and resourceGroup == '' and subscriptionId == '' | order by ['id'] asc" |
| 46 | + Search-AzOpsAzGraph -ManagementGroupName $ScopeObject.Name -Query $query -ErrorAction Stop |
31 | 47 | }
|
32 |
| - subscriptions { |
| 48 | + } |
| 49 | + if ($Subscription) { |
| 50 | + if ($SubscriptionsToIncludeResourceGroups -and $ResourceGroup) { |
33 | 51 | Write-AzOpsMessage -LogLevel Debug -LogString 'Get-AzOpsPolicyExemption.Subscription' -LogStringValues $ScopeObject.SubscriptionDisplayName, $ScopeObject.Subscription -Target $ScopeObject
|
| 52 | + $query = "policyresources | where type == 'microsoft.authorization/policyexemptions' and resourceGroup != '' | order by ['id'] asc" |
| 53 | + Search-AzOpsAzGraph -Subscription $SubscriptionsToIncludeResourceGroups -Query $query -ErrorAction Stop |
34 | 54 | }
|
35 |
| - resourcegroups { |
| 55 | + elseif ($ResourceGroup) { |
36 | 56 | Write-AzOpsMessage -LogLevel Debug -LogString 'Get-AzOpsPolicyExemption.ResourceGroup' -LogStringValues $ScopeObject.ResourceGroup -Target $ScopeObject
|
| 57 | + $query = "policyresources | where type == 'microsoft.authorization/policyexemptions' and resourceGroup != '' | order by ['id'] asc" |
| 58 | + Search-AzOpsAzGraph -Subscription $Subscription -Query $query -ErrorAction Stop |
37 | 59 | }
|
38 |
| - } |
39 |
| - try { |
40 |
| - $parameters = @{ |
41 |
| - Scope = $ScopeObject.Scope |
| 60 | + else { |
| 61 | + Write-AzOpsMessage -LogLevel Debug -LogString 'Get-AzOpsPolicyExemption.Subscription' -LogStringValues $ScopeObject.SubscriptionDisplayName, $ScopeObject.Subscription -Target $ScopeObject |
| 62 | + $query = "policyresources | where type == 'microsoft.authorization/policyexemptions' and resourceGroup == '' | order by ['id'] asc" |
| 63 | + Search-AzOpsAzGraph -Subscription $Subscription -Query $query -ErrorAction Stop |
42 | 64 | }
|
43 |
| - # Gather policyExemption with retry and backoff support from Invoke-AzOpsScriptBlock |
44 |
| - Invoke-AzOpsScriptBlock -ArgumentList $parameters -ScriptBlock { |
45 |
| - Get-AzPolicyExemption @parameters -WarningAction SilentlyContinue -ErrorAction Stop | Where-Object ResourceId -match $parameters.Scope |
46 |
| - } -RetryCount 3 -RetryWait 5 -RetryType Exponential -ErrorAction Stop |
47 |
| - } |
48 |
| - catch { |
49 |
| - Write-AzOpsMessage -LogLevel Warning -LogString 'Get-AzOpsPolicyExemption.Failed' -LogStringValues $ScopeObject.Scope |
50 | 65 | }
|
51 | 66 | }
|
52 | 67 |
|
|
0 commit comments