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 UploadRunbookAsDraft(AutomationRunbook runbook, AutomationManagementClient automationManagementClient, string resourceGroupName, AutomationAccount account)
        {
            RunbookCreateOrUpdateDraftProperties draftProperties = new RunbookCreateOrUpdateDraftProperties("Script", new RunbookDraft());

            draftProperties.Description = runbook.Description;
            RunbookCreateOrUpdateDraftParameters draftParams = new RunbookCreateOrUpdateDraftParameters(draftProperties);

            draftParams.Name     = runbook.Name;
            draftParams.Location = account.Location;
            await automationManagementClient.Runbooks.CreateOrUpdateWithDraftAsync(resourceGroupName, account.Name, draftParams);

            /* Update the runbook content from .ps1 file */
            RunbookDraftUpdateParameters draftUpdateParams = new RunbookDraftUpdateParameters()
            {
                Name   = runbook.Name,
                Stream = File.ReadAllText(runbook.localFileInfo.FullName)
            };
            await automationManagementClient.RunbookDraft.UpdateAsync(resourceGroupName, account.Name, draftUpdateParams);

            /* Ensure the correct sync status is detected */
            RunbookDraft draft = await GetRunbookDraft(runbook.Name, automationManagementClient, resourceGroupName, account.Name);

            runbook.localFileInfo.LastWriteTime = draft.LastModifiedTime.LocalDateTime;
            runbook.LastModifiedLocal           = draft.LastModifiedTime.LocalDateTime;
            runbook.LastModifiedCloud           = draft.LastModifiedTime.LocalDateTime;
        }
Beispiel #3
0
        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 <LongRunningOperationResultResponse> PublishRunbook(AutomationRunbook runbook, AutomationManagementClient automationManagementClient, string resourceGroupName, string accountName)
        {
            RunbookDraftPublishParameters publishParams = new RunbookDraftPublishParameters
            {
                Name        = runbook.Name,
                PublishedBy = "ISE User: " + System.Security.Principal.WindowsIdentity.GetCurrent().Name
            };
            CancellationTokenSource cts = new CancellationTokenSource();

            cts.CancelAfter(TIMEOUT_MS);
            LongRunningOperationResultResponse resultResponse = await automationManagementClient.RunbookDraft.PublishAsync(resourceGroupName, accountName, publishParams, cts.Token);

            /* Ensure the correct sync status is detected */
            if (runbook.localFileInfo != null)
            {
                cts = new CancellationTokenSource();
                cts.CancelAfter(TIMEOUT_MS);
                RunbookGetResponse response = await automationManagementClient.Runbooks.GetAsync(resourceGroupName, accountName, runbook.Name, cts.Token);

                runbook.localFileInfo.LastWriteTime = response.Runbook.Properties.LastModifiedTime.LocalDateTime;
                runbook.LastModifiedLocal           = response.Runbook.Properties.LastModifiedTime.LocalDateTime;
                runbook.LastModifiedCloud           = response.Runbook.Properties.LastModifiedTime.LocalDateTime;
            }
            /* Return the publish response */
            return(resultResponse);
        }
        public static async Task DeleteCloudRunbook(AutomationRunbook runbook, AutomationManagementClient automationManagementClient, string resourceGroupName, string accountName)
        {
            CancellationTokenSource cts = new CancellationTokenSource();

            cts.CancelAfter(TIMEOUT_MS);
            await automationManagementClient.Runbooks.DeleteAsync(resourceGroupName, accountName, runbook.Name, cts.Token);
        }
        public static async Task UploadRunbookAsDraft(AutomationRunbook runbook, AutomationManagementClient automationManagementClient, string resourceGroupName, AutomationAccount account)
        {
            RunbookCreateOrUpdateDraftProperties draftProperties;

            // Parse the script to determine if it is a PS workflow or native script
            String PSScriptText = File.ReadAllText(runbook.localFileInfo.FullName);
            System.Management.Automation.Language.Token[] AST;
            System.Management.Automation.Language.ParseError[] ASTError = null;
            var ASTScript = System.Management.Automation.Language.Parser.ParseInput(PSScriptText, out AST, out ASTError);

            // If the script starts with workflow, then create a PS Workflow script runbook or else create a native PS script runbook
            if (ASTScript.EndBlock.Extent.Text.ToLower().StartsWith("workflow"))
            {
                draftProperties = new RunbookCreateOrUpdateDraftProperties(Constants.RunbookType.Workflow, new RunbookDraft());
            }
            else
                draftProperties = new RunbookCreateOrUpdateDraftProperties(Constants.RunbookType.PowerShellScript, new RunbookDraft());

            // Get current properties if is not a new runbook and set these on the draft also so they are preserved.
            RunbookGetResponse response = null;
            CancellationTokenSource cts = new CancellationTokenSource();
            cts.CancelAfter(TIMEOUT_MS);
            if (runbook.SyncStatus != AutomationAuthoringItem.Constants.SyncStatus.LocalOnly)
            {
                response = await automationManagementClient.Runbooks.GetAsync(resourceGroupName, account.Name, runbook.Name, cts.Token);
                draftProperties.Description = response.Runbook.Properties.Description;
            }

            // Create draft properties
            RunbookCreateOrUpdateDraftParameters draftParams = new RunbookCreateOrUpdateDraftParameters(draftProperties);
            draftParams.Name = runbook.Name;
            draftParams.Location = account.Location;

            // If this is not a new runbook, set the existing properties of the runbook
            if (response != null)
            {
                draftParams.Tags = response.Runbook.Tags;
                draftParams.Properties.LogProgress = response.Runbook.Properties.LogProgress;
                draftParams.Properties.LogVerbose = response.Runbook.Properties.LogVerbose;
            }
            cts = new CancellationTokenSource();
            cts.CancelAfter(TIMEOUT_MS);
            await automationManagementClient.Runbooks.CreateOrUpdateWithDraftAsync(resourceGroupName, account.Name, draftParams, cts.Token);
            /* Update the runbook content from .ps1 file */

            RunbookDraftUpdateParameters draftUpdateParams = new RunbookDraftUpdateParameters()
            {
                Name = runbook.Name,
                Stream = PSScriptText
            };
            cts = new CancellationTokenSource();
            cts.CancelAfter(TIMEOUT_MS);
            await automationManagementClient.RunbookDraft.UpdateAsync(resourceGroupName, account.Name, draftUpdateParams, cts.Token);
            /* Ensure the correct sync status is detected */
            RunbookDraft draft = await GetRunbookDraft(runbook.Name, automationManagementClient, resourceGroupName, account.Name);
            runbook.localFileInfo.LastWriteTime = draft.LastModifiedTime.LocalDateTime;
            runbook.LastModifiedLocal = draft.LastModifiedTime.LocalDateTime;
            runbook.LastModifiedCloud = draft.LastModifiedTime.LocalDateTime;
        }
 public static async Task<LongRunningOperationResultResponse> PublishRunbook(AutomationRunbook runbook, AutomationManagementClient automationManagementClient, string resourceGroupName, string accountName)
 {
     RunbookDraftPublishParameters publishParams = new RunbookDraftPublishParameters
     {
         Name = runbook.Name,
         PublishedBy = "ISE User: " + System.Security.Principal.WindowsIdentity.GetCurrent().Name
     };
     LongRunningOperationResultResponse resultResponse = await automationManagementClient.RunbookDraft.PublishAsync(resourceGroupName, accountName, publishParams);
     return resultResponse;
 }
        public static async Task <LongRunningOperationResultResponse> PublishRunbook(AutomationRunbook runbook, AutomationManagementClient automationManagementClient, string resourceGroupName, string accountName)
        {
            RunbookDraftPublishParameters publishParams = new RunbookDraftPublishParameters
            {
                Name        = runbook.Name,
                PublishedBy = "ISE User: " + System.Security.Principal.WindowsIdentity.GetCurrent().Name
            };
            LongRunningOperationResultResponse resultResponse = await automationManagementClient.RunbookDraft.PublishAsync(resourceGroupName, accountName, publishParams);

            return(resultResponse);
        }
        /* This is the only way I can see to "check out" a runbook (get it from Published to Edit state) using the SDK. */
        public static async Task CheckOutRunbook(AutomationRunbook runbook, AutomationManagementClient automationManagementClient, 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);

            if (response.Runbook.Properties.State != "Published")
            {
                return;
            }
            cts = new CancellationTokenSource();
            cts.CancelAfter(TIMEOUT_MS);
            RunbookContentResponse runbookContentResponse = await automationManagementClient.Runbooks.ContentAsync(resourceGroupName, account.Name, runbook.Name, cts.Token);

            // Create draft properties
            RunbookCreateOrUpdateDraftParameters draftParams = new RunbookCreateOrUpdateDraftParameters();

            draftParams.Properties             = new RunbookCreateOrUpdateDraftProperties();
            draftParams.Properties.Description = response.Runbook.Properties.Description;
            draftParams.Properties.LogProgress = response.Runbook.Properties.LogProgress;
            draftParams.Properties.LogVerbose  = response.Runbook.Properties.LogVerbose;
            draftParams.Properties.RunbookType = response.Runbook.Properties.RunbookType;
            draftParams.Properties.Draft       = new RunbookDraft();
            draftParams.Tags     = response.Runbook.Tags;
            draftParams.Name     = runbook.Name;
            draftParams.Location = account.Location;

            cts = new CancellationTokenSource();
            cts.CancelAfter(TIMEOUT_MS);
            await automationManagementClient.Runbooks.CreateOrUpdateWithDraftAsync(resourceGroupName, account.Name, draftParams, cts.Token);

            RunbookDraftUpdateParameters draftUpdateParams = new RunbookDraftUpdateParameters()
            {
                Name   = runbook.Name,
                Stream = runbookContentResponse.Stream.ToString()
            };

            cts = new CancellationTokenSource();
            cts.CancelAfter(TIMEOUT_MS);
            await automationManagementClient.RunbookDraft.UpdateAsync(resourceGroupName, account.Name, draftUpdateParams, cts.Token);

            /* Ensure the correct sync status is detected */
            if (runbook.localFileInfo != null)
            {
                RunbookDraft draft = await GetRunbookDraft(runbook.Name, automationManagementClient, resourceGroupName, account.Name);

                runbook.localFileInfo.LastWriteTime = draft.LastModifiedTime.LocalDateTime;
                runbook.LastModifiedLocal           = draft.LastModifiedTime.LocalDateTime;
                runbook.LastModifiedCloud           = draft.LastModifiedTime.LocalDateTime;
            }
        }
        public static async Task UploadRunbookAsDraft(AutomationRunbook runbook, AutomationManagementClient automationManagementClient, string resourceGroupName, AutomationAccount account)
        {
            RunbookCreateOrUpdateDraftProperties draftProperties;

            // Parse the script to determine if it is a PS workflow or native script
            String PSScriptText = File.ReadAllText(runbook.localFileInfo.FullName);

            System.Management.Automation.Language.Token[]      AST;
            System.Management.Automation.Language.ParseError[] ASTError = null;
            var ASTScript = System.Management.Automation.Language.Parser.ParseInput(PSScriptText, out AST, out ASTError);

            // If the script starts with workflow, then create a PS Workflow script runbook or else create a native PS script runbook
            if (ASTScript.EndBlock.Extent.Text.ToLower().StartsWith("workflow"))
            {
                draftProperties = new RunbookCreateOrUpdateDraftProperties(Constants.RunbookType.Workflow, new RunbookDraft());
            }
            else
            {
                draftProperties = new RunbookCreateOrUpdateDraftProperties(Constants.RunbookType.PowerShellScript, new RunbookDraft());
            }

            draftProperties.Description = runbook.Description;
            RunbookCreateOrUpdateDraftParameters draftParams = new RunbookCreateOrUpdateDraftParameters(draftProperties);

            draftParams.Name     = runbook.Name;
            draftParams.Location = account.Location;
            await automationManagementClient.Runbooks.CreateOrUpdateWithDraftAsync(resourceGroupName, account.Name, draftParams);

            /* Update the runbook content from .ps1 file */

            RunbookDraftUpdateParameters draftUpdateParams = new RunbookDraftUpdateParameters()
            {
                Name   = runbook.Name,
                Stream = PSScriptText
            };
            await automationManagementClient.RunbookDraft.UpdateAsync(resourceGroupName, account.Name, draftUpdateParams);

            /* Ensure the correct sync status is detected */
            RunbookDraft draft = await GetRunbookDraft(runbook.Name, automationManagementClient, resourceGroupName, account.Name);

            runbook.localFileInfo.LastWriteTime = draft.LastModifiedTime.LocalDateTime;
            runbook.LastModifiedLocal           = draft.LastModifiedTime.LocalDateTime;
            runbook.LastModifiedCloud           = draft.LastModifiedTime.LocalDateTime;
        }
        /* This is the only way I can see to "check out" a runbook (get it from Published to Edit state) using the SDK. */
        public static async Task CheckOutRunbook(AutomationRunbook runbook, AutomationManagementClient automationManagementClient, 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);
            if (response.Runbook.Properties.State != "Published")
                return;
            cts = new CancellationTokenSource();
            cts.CancelAfter(TIMEOUT_MS);
            RunbookContentResponse runbookContentResponse = await automationManagementClient.Runbooks.ContentAsync(resourceGroupName, account.Name, runbook.Name, cts.Token);
            // Create draft properties
            RunbookCreateOrUpdateDraftParameters draftParams = new RunbookCreateOrUpdateDraftParameters();
            draftParams.Properties = new RunbookCreateOrUpdateDraftProperties();
            draftParams.Properties.Description = response.Runbook.Properties.Description;
            draftParams.Properties.LogProgress = response.Runbook.Properties.LogProgress;
            draftParams.Properties.LogVerbose = response.Runbook.Properties.LogVerbose;
            draftParams.Properties.RunbookType = response.Runbook.Properties.RunbookType;
            draftParams.Properties.Draft = new RunbookDraft();
            draftParams.Tags = response.Runbook.Tags;
            draftParams.Name = runbook.Name;
            draftParams.Location = account.Location;

            cts = new CancellationTokenSource();
            cts.CancelAfter(TIMEOUT_MS);
            await automationManagementClient.Runbooks.CreateOrUpdateWithDraftAsync(resourceGroupName, account.Name, draftParams, cts.Token);
            RunbookDraftUpdateParameters draftUpdateParams = new RunbookDraftUpdateParameters()
            {
                Name = runbook.Name,
                Stream = runbookContentResponse.Stream.ToString()
            };
            cts = new CancellationTokenSource();
            cts.CancelAfter(TIMEOUT_MS);
            await automationManagementClient.RunbookDraft.UpdateAsync(resourceGroupName, account.Name, draftUpdateParams, cts.Token);
            /* Ensure the correct sync status is detected */
            if (runbook.localFileInfo != null)
            {
                RunbookDraft draft = await GetRunbookDraft(runbook.Name, automationManagementClient, resourceGroupName, account.Name);
                runbook.localFileInfo.LastWriteTime = draft.LastModifiedTime.LocalDateTime;
                runbook.LastModifiedLocal = draft.LastModifiedTime.LocalDateTime;
                runbook.LastModifiedCloud = draft.LastModifiedTime.LocalDateTime;
            }
        }
 public static async Task UploadRunbookAsDraft(AutomationRunbook runbook, AutomationManagementClient automationManagementClient, string resourceGroupName, AutomationAccount account)
 {
     RunbookCreateOrUpdateDraftProperties draftProperties = new RunbookCreateOrUpdateDraftProperties("Script", new RunbookDraft());
     draftProperties.Description = runbook.Description;
     RunbookCreateOrUpdateDraftParameters draftParams = new RunbookCreateOrUpdateDraftParameters(draftProperties);
     draftParams.Name = runbook.Name;
     draftParams.Location = account.Location;
     await automationManagementClient.Runbooks.CreateOrUpdateWithDraftAsync(resourceGroupName, account.Name, draftParams);
     /* Update the runbook content from .ps1 file */
     RunbookDraftUpdateParameters draftUpdateParams = new RunbookDraftUpdateParameters()
     {
         Name = runbook.Name,
         Stream = File.ReadAllText(runbook.localFileInfo.FullName)
     };
     await automationManagementClient.RunbookDraft.UpdateAsync(resourceGroupName, account.Name, draftUpdateParams);
     /* Ensure the correct sync status is detected */
     RunbookDraft draft = await GetRunbookDraft(runbook.Name, automationManagementClient, resourceGroupName, account.Name);
     runbook.localFileInfo.LastWriteTime = draft.LastModifiedTime.LocalDateTime;
     runbook.LastModifiedLocal = draft.LastModifiedTime.LocalDateTime;
     runbook.LastModifiedCloud = draft.LastModifiedTime.LocalDateTime;
 }
        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 UploadRunbookAsDraft(AutomationRunbook runbook, AutomationManagementClient automationManagementClient, string resourceGroupName, AutomationAccount account)
        {
            RunbookCreateOrUpdateDraftProperties draftProperties;

            // Parse the script to determine if it is a PS workflow or native script
            String PSScriptText = File.ReadAllText(runbook.localFileInfo.FullName);
            System.Management.Automation.Language.Token[] AST;
            System.Management.Automation.Language.ParseError[] ASTError = null;
            var ASTScript = System.Management.Automation.Language.Parser.ParseInput(PSScriptText, out AST, out ASTError);

            // If the script starts with workflow, then create a PS Workflow script runbook or else create a native PS script runbook
            if (ASTScript.EndBlock.Extent.Text.ToLower().StartsWith("workflow"))
            {
                draftProperties = new RunbookCreateOrUpdateDraftProperties(Constants.RunbookType.Workflow, new RunbookDraft());
            }
            else
                draftProperties = new RunbookCreateOrUpdateDraftProperties(Constants.RunbookType.PowerShellScript, new RunbookDraft());

            draftProperties.Description = runbook.Description;
            RunbookCreateOrUpdateDraftParameters draftParams = new RunbookCreateOrUpdateDraftParameters(draftProperties);
            draftParams.Name = runbook.Name;
            draftParams.Location = account.Location;
            await automationManagementClient.Runbooks.CreateOrUpdateWithDraftAsync(resourceGroupName, account.Name, draftParams);
            /* Update the runbook content from .ps1 file */

            RunbookDraftUpdateParameters draftUpdateParams = new RunbookDraftUpdateParameters()
            {
                Name = runbook.Name,
                Stream = PSScriptText
            };
            await automationManagementClient.RunbookDraft.UpdateAsync(resourceGroupName, account.Name, draftUpdateParams);
            /* Ensure the correct sync status is detected */
            RunbookDraft draft = await GetRunbookDraft(runbook.Name, automationManagementClient, resourceGroupName, account.Name);
            runbook.localFileInfo.LastWriteTime = draft.LastModifiedTime.LocalDateTime;
            runbook.LastModifiedLocal = draft.LastModifiedTime.LocalDateTime;
            runbook.LastModifiedCloud = draft.LastModifiedTime.LocalDateTime;
        }
 /// <summary>
 /// Checks if a file is unsaved in the ISE and shows a dialog to ask user to confirm if they want to continue
 /// </summary>
 /// <param name="runbook"></param>
 /// <returns>false if the user clicks cancel or else returns true to continue with upload of unsaved file</returns>
 private Boolean checkIfFileIsSaved(AutomationRunbook runbook)
 {
     var iseFiles = HostObject.CurrentPowerShellTab.Files;
     foreach (var file in iseFiles)
     {
         if ((file.DisplayName == (runbook.Name + ".ps1*")) && (file.IsSaved == false))
         {
             String message = "The file " + runbook.Name + ".ps1 is currently unsaved in the ISE";
             message += "\r\nCancel and save the file or click OK to upload the unsaved file";
             String header = "Upload Warning";
             System.Windows.Forms.DialogResult dialogResult = System.Windows.Forms.MessageBox.Show(message, header,
                 System.Windows.Forms.MessageBoxButtons.OKCancel, System.Windows.Forms.MessageBoxIcon.Warning);
             if (dialogResult == System.Windows.Forms.DialogResult.Cancel)
                 return false;
         }
     }
     return true;
 }
Beispiel #16
0
 public RunbookTransferJob(AutomationRunbook rb, TransferOperation t)
 {
     this.Runbook   = rb;
     this.Operation = t;
 }
 public static void DeleteLocalRunbook(AutomationRunbook runbook)
 {
     File.Delete(runbook.localFileInfo.FullName);
 }
        public static async Task UploadRunbookAsDraft(AutomationRunbook runbook, AutomationManagementClient automationManagementClient, string resourceGroupName, AutomationAccount account)
        {
            RunbookCreateOrUpdateDraftProperties draftProperties;

            // Parse the script to determine if it is a PS workflow or native script
            String PSScriptText = File.ReadAllText(runbook.localFileInfo.FullName);

            System.Management.Automation.Language.Token[]      AST;
            System.Management.Automation.Language.ParseError[] ASTError = null;
            var ASTScript = System.Management.Automation.Language.Parser.ParseInput(PSScriptText, out AST, out ASTError);

            // If the script starts with workflow, then create a PS Workflow script runbook or else create a native PS script runbook
            if (ASTScript.EndBlock.Extent.Text.ToLower().StartsWith("workflow"))
            {
                draftProperties = new RunbookCreateOrUpdateDraftProperties(Constants.RunbookType.Workflow, new RunbookDraft());
            }
            else
            {
                draftProperties = new RunbookCreateOrUpdateDraftProperties(Constants.RunbookType.PowerShellScript, new RunbookDraft());
            }

            // Get current properties if is not a new runbook and set these on the draft also so they are preserved.
            RunbookGetResponse      response = null;
            CancellationTokenSource cts      = new CancellationTokenSource();

            cts.CancelAfter(TIMEOUT_MS);
            if (runbook.SyncStatus != AutomationAuthoringItem.Constants.SyncStatus.LocalOnly)
            {
                response = await automationManagementClient.Runbooks.GetAsync(resourceGroupName, account.Name, runbook.Name, cts.Token);

                draftProperties.Description = response.Runbook.Properties.Description;
                draftProperties.RunbookType = response.Runbook.Properties.RunbookType;
            }

            // Create draft properties
            RunbookCreateOrUpdateDraftParameters draftParams = new RunbookCreateOrUpdateDraftParameters(draftProperties);

            draftParams.Name     = runbook.Name;
            draftParams.Location = account.Location;

            // If this is not a new runbook, set the existing properties of the runbook
            if (response != null)
            {
                draftParams.Tags = response.Runbook.Tags;
                draftParams.Properties.LogProgress = response.Runbook.Properties.LogProgress;
                draftParams.Properties.LogVerbose  = response.Runbook.Properties.LogVerbose;
            }
            cts = new CancellationTokenSource();
            cts.CancelAfter(TIMEOUT_MS);
            await automationManagementClient.Runbooks.CreateOrUpdateWithDraftAsync(resourceGroupName, account.Name, draftParams, cts.Token);

            /* Update the runbook content from .ps1 file */

            RunbookDraftUpdateParameters draftUpdateParams = new RunbookDraftUpdateParameters()
            {
                Name   = runbook.Name,
                Stream = PSScriptText
            };

            cts = new CancellationTokenSource();
            cts.CancelAfter(TIMEOUT_MS);
            await automationManagementClient.RunbookDraft.UpdateAsync(resourceGroupName, account.Name, draftUpdateParams, cts.Token);

            /* Ensure the correct sync status is detected */
            RunbookDraft draft = await GetRunbookDraft(runbook.Name, automationManagementClient, resourceGroupName, account.Name);

            runbook.localFileInfo.LastWriteTime = draft.LastModifiedTime.LocalDateTime;
            runbook.LastModifiedLocal           = draft.LastModifiedTime.LocalDateTime;
            runbook.LastModifiedCloud           = draft.LastModifiedTime.LocalDateTime;
        }
 public static async Task DeleteCloudRunbook(AutomationRunbook runbook, AutomationManagementClient automationManagementClient, string resourceGroupName, string accountName)
 {
     CancellationTokenSource cts = new CancellationTokenSource();
     cts.CancelAfter(TIMEOUT_MS);
     await automationManagementClient.Runbooks.DeleteAsync(resourceGroupName, accountName, runbook.Name, cts.Token);
 }
 public static void DeleteLocalRunbook(AutomationRunbook runbook)
 {
     File.Delete(runbook.localFileInfo.FullName);
 }
 public RunbookTransferJob(AutomationRunbook rb, TransferOperation t)
 {
     this.Runbook = rb;
     this.Operation = t;
 }
 public static async Task<LongRunningOperationResultResponse> PublishRunbook(AutomationRunbook runbook, AutomationManagementClient automationManagementClient, string resourceGroupName, string accountName)
 {
     RunbookDraftPublishParameters publishParams = new RunbookDraftPublishParameters
     {
         Name = runbook.Name,
         PublishedBy = "ISE User: " + System.Security.Principal.WindowsIdentity.GetCurrent().Name
     };
     CancellationTokenSource cts = new CancellationTokenSource();
     cts.CancelAfter(TIMEOUT_MS);
     LongRunningOperationResultResponse resultResponse = await automationManagementClient.RunbookDraft.PublishAsync(resourceGroupName, accountName, publishParams, cts.Token);
     /* Ensure the correct sync status is detected */
     if (runbook.localFileInfo != null)
     {
         cts = new CancellationTokenSource();
         cts.CancelAfter(TIMEOUT_MS);
         RunbookGetResponse response = await automationManagementClient.Runbooks.GetAsync(resourceGroupName, accountName, runbook.Name, cts.Token);
         runbook.localFileInfo.LastWriteTime = response.Runbook.Properties.LastModifiedTime.LocalDateTime;
         runbook.LastModifiedLocal = response.Runbook.Properties.LastModifiedTime.LocalDateTime;
         runbook.LastModifiedCloud = response.Runbook.Properties.LastModifiedTime.LocalDateTime;
     }
     /* Return the publish response */
     return resultResponse;
 }
 /// <summary>
 /// Checks if a file is unsaved in the ISE and shows a dialog to ask user to confirm if they want to continue
 /// </summary>
 /// <param name="runbook"></param>
 /// <returns>false if the user clicks cancel or else returns true to continue with upload of unsaved file</returns>
 private Boolean checkIfFileIsSaved(AutomationRunbook runbook)
 {
     var currentFile = HostObject.CurrentPowerShellTab.Files.Where(x => x.FullPath == runbook.localFileInfo.FullName);
     if (currentFile.Count() != 0)
         {
         if (currentFile.First().IsSaved == false)
         {
             String message = "The file " + runbook.localFileInfo.Name + " has unsaved changes.";
             message += "\r\nPlease save your changes before uploading.";
             System.Windows.Forms.DialogResult dialogResult = System.Windows.Forms.MessageBox.Show(message, "Upload Warning",
                 System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
             return false;
         }
     }
     return true;
 }