public static async Task DownloadRunbook(AutomationRunbook runbook, AutomationManagementClient automationManagementClient, string workspace, string resourceGroupName, AutomationAccount account) { CancellationTokenSource cts = new CancellationTokenSource(); cts.CancelAfter(TIMEOUT_MS); RunbookGetResponse response = await automationManagementClient.Runbooks.GetAsync(resourceGroupName, account.Name, runbook.Name, cts.Token); RunbookDraftGetResponse draftResponse = null; RunbookContentResponse runbookContentResponse = null; cts = new CancellationTokenSource(); cts.CancelAfter(TIMEOUT_MS); if (response.Runbook.Properties.State == "Published") { runbookContentResponse = await automationManagementClient.Runbooks.ContentAsync(resourceGroupName, account.Name, runbook.Name, cts.Token); } else { runbookContentResponse = await automationManagementClient.RunbookDraft.ContentAsync(resourceGroupName, account.Name, runbook.Name, cts.Token); cts = new CancellationTokenSource(); cts.CancelAfter(TIMEOUT_MS); draftResponse = await automationManagementClient.RunbookDraft.GetAsync(resourceGroupName, account.Name, runbook.Name, cts.Token); } String runbookFilePath = System.IO.Path.Combine(workspace, runbook.Name + ".ps1"); try { File.WriteAllText(runbookFilePath, runbookContentResponse.Stream.ToString(), Encoding.UTF8); } catch (Exception Ex) { // Atempting to write the file while it is being read. Wait a second and retry. if (Ex.HResult == -2147024864) { Thread.Sleep(1000); File.WriteAllText(runbookFilePath, runbookContentResponse.Stream.ToString(), Encoding.UTF8); } } runbook.AuthoringState = AutomationRunbook.AuthoringStates.InEdit; runbook.localFileInfo = new FileInfo(runbookFilePath); /* This is the only way I can see to "check out" the runbook using the SDK. * Hopefully there's a better way but for now this works */ if (response.Runbook.Properties.State == "Published") { await UploadRunbookAsDraft(runbook, automationManagementClient, resourceGroupName, account); cts = new CancellationTokenSource(); cts.CancelAfter(TIMEOUT_MS); draftResponse = await automationManagementClient.RunbookDraft.GetAsync(resourceGroupName, account.Name, runbook.Name, cts.Token); } /* Ensures the correct sync status is detected */ if (draftResponse != null) { runbook.localFileInfo.LastWriteTime = draftResponse.RunbookDraft.LastModifiedTime.LocalDateTime; runbook.LastModifiedLocal = draftResponse.RunbookDraft.LastModifiedTime.LocalDateTime; runbook.LastModifiedCloud = draftResponse.RunbookDraft.LastModifiedTime.LocalDateTime; } }
public static async Task <RunbookDraft> GetRunbookDraft(string runbookName, AutomationManagementClient automationManagementClient, string resourceGroupName, string accountName) { CancellationTokenSource cts = new CancellationTokenSource(); cts.CancelAfter(TIMEOUT_MS); RunbookDraftGetResponse response = await automationManagementClient.RunbookDraft.GetAsync(resourceGroupName, accountName, runbookName, cts.Token); return(response.RunbookDraft); }
public static async Task DownloadRunbook(AutomationRunbook runbook, AutomationManagementClient automationManagementClient, string workspace, string resourceGroupName, AutomationAccount account) { RunbookGetResponse response = await automationManagementClient.Runbooks.GetAsync(resourceGroupName, account.Name, runbook.Name); RunbookDraftGetResponse draftResponse = null; RunbookContentResponse runbookContentResponse = null; if (response.Runbook.Properties.State == "Published") { runbookContentResponse = await automationManagementClient.Runbooks.ContentAsync(resourceGroupName, account.Name, runbook.Name); } else { runbookContentResponse = await automationManagementClient.RunbookDraft.ContentAsync(resourceGroupName, account.Name, runbook.Name); draftResponse = await automationManagementClient.RunbookDraft.GetAsync(resourceGroupName, account.Name, runbook.Name); } String runbookFilePath = System.IO.Path.Combine(workspace, runbook.Name + ".ps1"); File.WriteAllText(runbookFilePath, runbookContentResponse.Stream.ToString()); runbook.localFileInfo = new FileInfo(runbookFilePath); /* This is the only way I can see to "check out" the runbook using the SDK. * Hopefully there's a better way but for now this works */ if (response.Runbook.Properties.State == "Published") { await UploadRunbookAsDraft(runbook, automationManagementClient, resourceGroupName, account); draftResponse = await automationManagementClient.RunbookDraft.GetAsync(resourceGroupName, account.Name, runbook.Name); } /* Ensures the correct sync status is detected */ if (draftResponse != null) { runbook.localFileInfo.LastWriteTime = draftResponse.RunbookDraft.LastModifiedTime.LocalDateTime; runbook.LastModifiedLocal = draftResponse.RunbookDraft.LastModifiedTime.LocalDateTime; runbook.LastModifiedCloud = draftResponse.RunbookDraft.LastModifiedTime.LocalDateTime; } }
public static async Task <RunbookDraft> GetRunbookDraft(string runbookName, AutomationManagementClient automationManagementClient, string resourceGroupName, string accountName) { RunbookDraftGetResponse response = await automationManagementClient.RunbookDraft.GetAsync(resourceGroupName, accountName, runbookName); return(response.RunbookDraft); }