【问题标题】:How to check if any backup policy applied on virtual machine via azure fluent API?如何通过 azure fluent API 检查是否在虚拟机上应用了任何备份策略?
【发布时间】:2021-04-27 14:21:47
【问题描述】:

我想检查当前我正在使用 Azure Management ResourceManager Fluent API 的虚拟机是否启用了任何备份策略。

【问题讨论】:

    标签: azure azure-sdk-.net


    【解决方案1】:

    关于问题,请参考以下代码

    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}");
    
    
                }
    

    【讨论】:

    • 非常感谢您的回复.....是的,它对我很有用....
    猜你喜欢
    • 2021-06-28
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    相关资源
    最近更新 更多