簡體   English   中英

使用c#更改Azure VM角色大小

[英]Change Azure VM role size using c#

我正在嘗試復制一些PowerShell代碼,以使用C#Management API(Microsoft.WindowsAzure.Management.Compute)更改現有Azure VM的角色大小。

這就是我想要完成的事情:

Get-AzureVM | Where-Object {$_.InstanceSize -ne 'Basic_A0'} | Set-AzureVMSize "Basic_A0" | Update-AzureVM

到目前為止,我的C#代碼嘗試降級特定的VM,但每當我嘗試更新VM設置時,我都會得到一個我不太了解的異常,因為PowerShell版本不需要在VM上安裝ProvisionGuestAgent來更改角色尺寸。

Microsoft.Threading.Tasks.dll中發生未處理的“Microsoft.WindowsAzure.CloudException”類型異常

其他信息:BadRequest:為了使用擴展引用,必須在虛擬機設置期間設置ProvisionGuestAgent。

public void DowngradeVm(ComputeManagementClient Client, string VmName) {
  var hostedServices = Client.HostedServices.List();
  foreach (var service in hostedServices) {
    if (service.ServiceName != VmName) { continue;  }
    var deployment = Client.Deployments.GetBySlot(service.ServiceName, DeploymentSlot.Production);
    if (deployment != null) {
      if (deployment.Roles.Count > 0) {
        foreach (var role in deployment.Roles) {
          if (role.RoleType == VirtualMachineRoleType.PersistentVMRole.ToString()) {
            if (role.RoleSize != "Basic_A0") {
              // attempt do downgrade VM
              var upd = new VirtualMachineUpdateParameters();
              upd.AvailabilitySetName = role.AvailabilitySetName;
              upd.ConfigurationSets = role.ConfigurationSets;
              upd.DataVirtualHardDisks = role.DataVirtualHardDisks;
              upd.Label = role.Label;
              upd.OSVirtualHardDisk = role.OSVirtualHardDisk;
              upd.ProvisionGuestAgent = role.ProvisionGuestAgent;
              upd.ResourceExtensionReferences = role.ResourceExtensionReferences;
              upd.RoleName = role.RoleName;
              upd.RoleSize = "Basic_A0";
              // Service, Deployment and VM have the same name
              // the next line throws an exception
              Client.VirtualMachines.Update(VmName, VmName, VmName, upd);
            }
          }
        }
      }
    }
  }
}

我找到了解決方案:role.ProvisionGuestAgent為null(可能是因為該角色不在線),但需要為更新生效。

          upd.ProvisionGuestAgent = true;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM