79313365

Date: 2024-12-28 07:10:15
Score: 0.5
Natty:
Report link

try out the c# code below below. it will get all resources in the RG and see if the resource is a cert. If it is a cert, you can invoke the DeleteAsync() to delete the cert. MSFT documentation here for AppCertificateResource. https://learn.microsoft.com/en-us/dotnet/api/azure.resourcemanager.appservice.appcertificateresource?view=azure-dotnet

using System;
using System.Threading.Tasks;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Resources;
using Azure.Core;
using Azure.Security.KeyVault.Certificates;
using Azure.ResourceManager.AppService;

public class AzureResourceGroupExample
{
    public static async Task Main(string[] args)
    {
        string subscriptionId = "xxxx";
        string resourceGroupName = "rg-xxx";

        ArmClient armClient = new ArmClient(new DefaultAzureCredential());
        ResourceIdentifier subscriptionResourceId = new ResourceIdentifier($"/subscriptions/{subscriptionId}");
        SubscriptionResource subscription = armClient.GetSubscriptionResource(subscriptionResourceId);
        ResourceGroupResource _resourceGroupResource = await subscription.GetResourceGroupAsync(resourceGroupName);
        Console.WriteLine($"Resource group retrieved: {_resourceGroupResource.Data.Name}");

        await foreach (var resource in _resourceGroupResource.GetGenericResourcesAsync())
        {
            if (resource.Data.ResourceType == "Microsoft.Web/certificates")
            {
                var certificateResource = await _resourceGroupResource.GetAppCertificateAsync(resource.Data.Name);
                Console.WriteLine($"- Certificate Resource: {certificateResource.Value.Id}");
                // certificateResource.Value.DeleteAsync();
            }
            else
            {
                Console.WriteLine($"- Resource: {resource.Data.Name}, Type: {resource.Data.ResourceType}");
            }
        }

    }
}
Reasons:
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: qkfang