From 372cddbf4e5a82dc74990987495dc6212d7a3309 Mon Sep 17 00:00:00 2001 From: Jesper Fajers Date: Mon, 16 Dec 2024 12:27:34 +0000 Subject: [PATCH] Update --- src/internal/functions/Get-AzOpsResource.ps1 | 56 +++++++++++--------- src/localized/en-us/Strings.psd1 | 2 + 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/internal/functions/Get-AzOpsResource.ps1 b/src/internal/functions/Get-AzOpsResource.ps1 index d1e26963..17b1b95e 100644 --- a/src/internal/functions/Get-AzOpsResource.ps1 +++ b/src/internal/functions/Get-AzOpsResource.ps1 @@ -20,33 +20,39 @@ process { Set-AzOpsContext -ScopeObject $ScopeObject - switch ($ScopeObject.Resource) { - # Check if the resource exist - 'locks' { - $resource = Get-AzResourceLock -Scope "/subscriptions/$($ScopeObject.Subscription)" -ErrorAction SilentlyContinue | Where-Object { $_.ResourceID -eq $ScopeObject.Scope } - } - 'policyAssignments' { - $resource = Get-AzPolicyAssignment -Id $scopeObject.Scope -ErrorAction SilentlyContinue - } - 'policyDefinitions' { - $resource = Get-AzPolicyDefinition -Id $scopeObject.Scope -ErrorAction SilentlyContinue - } - 'policyExemptions' { - $resource = Get-AzPolicyExemption -Id $scopeObject.Scope -ErrorAction SilentlyContinue - } - 'policySetDefinitions' { - $resource = Get-AzPolicySetDefinition -Id $scopeObject.Scope -ErrorAction SilentlyContinue - } - 'roleAssignments' { - $resource = Invoke-AzRestMethod -Path "$($scopeObject.Scope)?api-version=2022-04-01" | Where-Object { $_.StatusCode -eq 200 } - } - 'resourceGroups' { - $resource = Get-AzResourceGroup -Id $scopeObject.Scope -ErrorAction SilentlyContinue - } - default { - $resource = Get-AzResource -ResourceId $ScopeObject.Scope -ErrorAction SilentlyContinue + try { + switch ($ScopeObject.Resource) { + # Check if the resource exist + 'locks' { + $resource = Get-AzResourceLock -Scope "/subscriptions/$($ScopeObject.Subscription)" -ErrorAction SilentlyContinue | Where-Object { $_.ResourceID -eq $ScopeObject.Scope } + } + 'policyAssignments' { + $resource = Get-AzPolicyAssignment -Id $scopeObject.Scope -ErrorAction SilentlyContinue + } + 'policyDefinitions' { + $resource = Get-AzPolicyDefinition -Id $scopeObject.Scope -ErrorAction SilentlyContinue + } + 'policyExemptions' { + $resource = Get-AzPolicyExemption -Id $scopeObject.Scope -ErrorAction SilentlyContinue + } + 'policySetDefinitions' { + $resource = Get-AzPolicySetDefinition -Id $scopeObject.Scope -ErrorAction SilentlyContinue + } + 'roleAssignments' { + $resource = Invoke-AzRestMethod -Path "$($scopeObject.Scope)?api-version=2022-04-01" | Where-Object { $_.StatusCode -eq 200 } + } + 'resourceGroups' { + $resource = Get-AzResourceGroup -Id $scopeObject.Scope -ErrorAction SilentlyContinue + } + default { + $resource = Get-AzResource -ResourceId $ScopeObject.Scope -ErrorAction SilentlyContinue + } } } + catch { + Write-AzOpsMessage -LogLevel InternalComment -LogString 'Get-AzOpsResource.Failed' -LogStringValues $_ + return + } if ($resource) { return $resource } diff --git a/src/localized/en-us/Strings.psd1 b/src/localized/en-us/Strings.psd1 index 98193377..a1e3628a 100644 --- a/src/localized/en-us/Strings.psd1 +++ b/src/localized/en-us/Strings.psd1 @@ -98,6 +98,8 @@ 'Get-AzOpsPolicyExemption.ResourceGroup' = 'Retrieving Policy Exemption for Resource Group {0}' # $ScopeObject.ResourceGroup 'Get-AzOpsPolicyExemption.Subscription' = 'Retrieving Policy Exemption for Subscription {0} ({1})' # $ScopeObject.SubscriptionDisplayName, $ScopeObject.Subscription + 'Get-AzOpsResource.Failed' = 'Failed retrieving resource with error: {0}' # $_ + 'Get-AzOpsResourceLock.ResourceGroup' = 'Retrieving Resource Locks for Resource Group {0}' # $ScopeObject.ResourceGroup 'Get-AzOpsResourceLock.Failed' = 'Failed retrieving Resource Locks {0}' # $_ 'Get-AzOpsResourceLock.Subscription' = 'Retrieving Resource Locks for Subscription {0} ({1})' # $ScopeObject.SubscriptionDisplayName, $ScopeObject.Subscription