如何通过 azure fluent API 检查是否在虚拟机上应用了任何备份策略?

How to check if any backup policy applied on virtual machine via azure fluent API?

我想检查当前我正在使用 Azure Management ResourceManager Fluent API.

的虚拟机是否启用了任何备份策略

问题请参考以下代码

  1. 创建服务主体并将贡献者角色分配给 sp

  2. SDK

 <PackageReference Include="Microsoft.Azure.Management.Compute" Version="46.0.0" />
    <PackageReference Include="Microsoft.Azure.Management.RecoveryServices.Backup" Version="4.1.5-preview" />
    <PackageReference Include="Microsoft.Identity.Client" Version="4.30.1" />
  1. 代码
 var app = ConfidentialClientApplicationBuilder.Create(clientId)
                .WithClientSecret(clientSecret)
                .WithAuthority(String.Format(CultureInfo.InvariantCulture, "https://login.microsoftonline.com/{0}", tenantDomain))
                .Build();
            string[] scopes = new string[] { "https://management.azure.com/.default" };
            var result = await app.AcquireTokenForClient(scopes)
                     .ExecuteAsync();

            var cred = new TokenCredentials(result.AccessToken);

            ComputeManagementClient computeClient = new ComputeManagementClient(cred);
            computeClient.SubscriptionId = subscription;
            RecoveryServicesBackupClient backupClient = new RecoveryServicesBackupClient(cred);
            backupClient.SubscriptionId = subscription;
             foreach (var vm in await computeClient.VirtualMachines.ListAllAsync()) {
                BackupStatusResponse res = await backupClient.BackupStatus.GetAsync(vm.Location, new BackupStatusRequest()
                {
                    ResourceId = vm.Id,
                    ResourceType = "VM"
                });

                Console.WriteLine($"the vm {vm.Name} has been assiocated with backup policy {res.PolicyName}");


            }