|
20 | 20 |
|
21 | 21 | process { |
22 | 22 | Set-AzOpsContext -ScopeObject $ScopeObject |
23 | | - switch ($ScopeObject.Resource) { |
24 | | - # Check if the resource exist |
25 | | - 'locks' { |
26 | | - $resource = Get-AzResourceLock -Scope "/subscriptions/$($ScopeObject.Subscription)" -ErrorAction SilentlyContinue | Where-Object { $_.ResourceID -eq $ScopeObject.Scope } |
27 | | - } |
28 | | - 'policyAssignments' { |
29 | | - $resource = Get-AzPolicyAssignment -Id $scopeObject.Scope -ErrorAction SilentlyContinue |
30 | | - } |
31 | | - 'policyDefinitions' { |
32 | | - $resource = Get-AzPolicyDefinition -Id $scopeObject.Scope -ErrorAction SilentlyContinue |
33 | | - } |
34 | | - 'policyExemptions' { |
35 | | - $resource = Get-AzPolicyExemption -Id $scopeObject.Scope -ErrorAction SilentlyContinue |
36 | | - } |
37 | | - 'policySetDefinitions' { |
38 | | - $resource = Get-AzPolicySetDefinition -Id $scopeObject.Scope -ErrorAction SilentlyContinue |
39 | | - } |
40 | | - 'roleAssignments' { |
41 | | - $resource = Invoke-AzRestMethod -Path "$($scopeObject.Scope)?api-version=2022-04-01" | Where-Object { $_.StatusCode -eq 200 } |
42 | | - } |
43 | | - 'resourceGroups' { |
44 | | - $resource = Get-AzResourceGroup -Id $scopeObject.Scope -ErrorAction SilentlyContinue |
45 | | - } |
46 | | - default { |
47 | | - $resource = Get-AzResource -ResourceId $ScopeObject.Scope -ErrorAction SilentlyContinue |
| 23 | + try { |
| 24 | + switch ($ScopeObject.Resource) { |
| 25 | + # Check if the resource exist |
| 26 | + 'locks' { |
| 27 | + $resource = Get-AzResourceLock -Scope "/subscriptions/$($ScopeObject.Subscription)" -ErrorAction SilentlyContinue | Where-Object { $_.ResourceID -eq $ScopeObject.Scope } |
| 28 | + } |
| 29 | + 'policyAssignments' { |
| 30 | + $resource = Get-AzPolicyAssignment -Id $scopeObject.Scope -ErrorAction SilentlyContinue |
| 31 | + } |
| 32 | + 'policyDefinitions' { |
| 33 | + $resource = Get-AzPolicyDefinition -Id $scopeObject.Scope -ErrorAction SilentlyContinue |
| 34 | + } |
| 35 | + 'policyExemptions' { |
| 36 | + $resource = Get-AzPolicyExemption -Id $scopeObject.Scope -ErrorAction SilentlyContinue |
| 37 | + } |
| 38 | + 'policySetDefinitions' { |
| 39 | + $resource = Get-AzPolicySetDefinition -Id $scopeObject.Scope -ErrorAction SilentlyContinue |
| 40 | + } |
| 41 | + 'roleAssignments' { |
| 42 | + $resource = Invoke-AzRestMethod -Path "$($scopeObject.Scope)?api-version=2022-04-01" | Where-Object { $_.StatusCode -eq 200 } |
| 43 | + } |
| 44 | + 'resourceGroups' { |
| 45 | + $resource = Get-AzResourceGroup -Id $scopeObject.Scope -ErrorAction SilentlyContinue |
| 46 | + } |
| 47 | + default { |
| 48 | + $resource = Get-AzResource -ResourceId $ScopeObject.Scope -ErrorAction SilentlyContinue |
| 49 | + } |
48 | 50 | } |
49 | 51 | } |
| 52 | + catch { |
| 53 | + Write-AzOpsMessage -LogLevel InternalComment -LogString 'Get-AzOpsResource.Failed' -LogStringValues $_ |
| 54 | + return |
| 55 | + } |
50 | 56 | if ($resource) { |
51 | 57 | return $resource |
52 | 58 | } |
|
0 commit comments