2016-12-12 91 views
1

我想要使用Azure資源管理器(ARM)和.NET C#來識別Azure虛擬機部署的成功或失敗結果。C#在使用Azure資源管理器(ARM)部署Azure虛擬機時獲取結果狀態

我發現下面的示例。

https://docs.microsoft.com/en-us/azure/virtual-machines/virtual-machines-windows-csharp-template

在這篇文章中,deploing時, 「返回等待」 語句中使用。

public static async Task<DeploymentExtended> CreateTemplateDeploymentAsync(
    TokenCredentials credential, 
    string groupName, 
    string deploymentName, 
    string subscriptionId){ 
Console.WriteLine("Creating the template deployment..."); 
var deployment = new Deployment(); 
deployment.Properties = new DeploymentProperties 

{ 
Mode = DeploymentMode.Incremental, 
Template = File.ReadAllText("..\\..\\VirtualMachineTemplate.json"), 
Parameters = File.ReadAllText("..\\..\\Parameters.json") 
}; 
var resourceManagementClient = new ResourceManagementClient(credential) 
    { SubscriptionId = subscriptionId }; 
return await resourceManagementClient.Deployments.CreateOrUpdateAsync(
groupName, 
deploymentName, 
deployment); 
} 

我該如何處理結果? 我想根據結果來劃分程序。

回答

0

我們可以使用Properties.ProvisioningState來獲得部署狀態。但是當它部署虛擬機失敗時,可能會拋出異常,所以我們需要用代碼來捕獲異常。

1.Code演示:

var token = GetAccessTokenAsync(); 
var credential = new TokenCredentials(token.Result.AccessToken); 
string provisoningStatus = "Failed"; 
try 
    { 
    var result =CreateTemplateDeploymentAsync(credential, "tom", "MyWindowsVM", "you subscription Id") 
        .Result; 
       provisoningStatus = result.Properties.ProvisioningState; 
    } 
catch (Exception) 
    { 

    //ToDo 
    } 
    if (provisoningStatus.Equals("Failed")) 
    { 
     //TODo 
    } 

} 
  • 創建VM成功
  • enter image description here

  • 檢查來自Azure Portal
  • enter image description here

    如果失敗,沒有趕上例外

    enter image description here

    +0

    你好Tom謝謝你repling!對不起,lating的答案.. –

    +0

    你的建議是非常可以理解,很好。我會盡快檢查並回復 –

    +0

    這個線程是否有更新? –

    相關問題