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;
        }
        private async Task <TestJobCreateResponse> createTestJob()
        {
            RunbookDraft draft = await AutomationRunbookManager.GetRunbookDraft(runbookName, iseClient.automationManagementClient,
                                                                                iseClient.accountResourceGroups[iseClient.currAccount].Name, iseClient.currAccount.Name);

            if (draft.InEdit == false)
            {
                throw new Exception("This runbook has no draft to test because it is in a 'Published' state.");
            }

            HybridRunbookWorkerGroupsListResponse hybridGroupResponse = await iseClient.automationManagementClient.HybridRunbookWorkerGroups.ListAsync(
                iseClient.accountResourceGroups[iseClient.currAccount].Name, iseClient.currAccount.Name,
                new CancellationToken());

            TestJobCreateParameters jobCreationParams = new TestJobCreateParameters();

            jobCreationParams.RunbookName = runbookName;
            if (draft.Parameters.Count > 0 || hybridGroupResponse.HybridRunbookWorkerGroups.Count > 0)
            {
                /* User needs to specify some things */
                var existingParams = await GetLastTestJobParams();

                RunbookParamDialog paramDialog = new RunbookParamDialog(draft.Parameters, existingParams, hybridGroupResponse.HybridRunbookWorkerGroups);
                if (paramDialog.ShowDialog() == true)
                {
                    if (draft.Parameters.Count > 0)
                    {
                        jobCreationParams.Parameters = paramDialog.paramValues;
                    }
                    if (!String.IsNullOrEmpty(paramDialog.runOnSelection) && !paramDialog.runOnSelection.Equals("Azure"))
                    {
                        jobCreationParams.RunOn = paramDialog.runOnSelection;
                    }
                }
                else
                {
                    return(null);
                }
            }
            /* start the test job */
            CancellationTokenSource cts = new CancellationTokenSource();

            cts.CancelAfter(TIMEOUT_MS);
            TestJobCreateResponse jobResponse = await iseClient.automationManagementClient.TestJobs.CreateAsync(
                iseClient.accountResourceGroups[iseClient.currAccount].Name,
                iseClient.currAccount.Name, jobCreationParams, cts.Token);

            if (jobResponse == null || jobResponse.StatusCode != System.Net.HttpStatusCode.Created)
            {
                throw new Exception("The test job could not be created: received HTTP status code " + jobResponse.StatusCode);
            }
            return(jobResponse);
        }
        /* 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;
            }
        }
예제 #4
0
 //Runbook exists both on disk and in the cloud. But are they in sync?
 public AutomationRunbook(FileInfo localFile, Runbook cloudRunbook, RunbookDraft cloudRunbookDraft)
     : base(cloudRunbook.Name, localFile.LastWriteTime, cloudRunbook.Properties.LastModifiedTime.LocalDateTime)
 {
     this.AuthoringState = cloudRunbook.Properties.State;
     this.localFileInfo  = localFile;
     this.Description    = cloudRunbook.Properties.Description;
     this.Parameters     = cloudRunbook.Properties.Parameters;
     if (cloudRunbookDraft != null)
     {
         this.LastModifiedCloud = cloudRunbookDraft.LastModifiedTime.LocalDateTime;
         UpdateSyncStatus();
     }
 }
        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;
        }
예제 #6
0
        private async Task <TestJobCreateResponse> createTestJob()
        {
            RunbookDraft draft = await AutomationRunbookManager.GetRunbookDraft(runbookName, iseClient.automationManagementClient,
                                                                                iseClient.accountResourceGroups[iseClient.currAccount].Name, iseClient.currAccount.Name);

            if (draft.InEdit == false)
            {
                throw new Exception("This runbook has no draft to test because it is in a 'Published' state.");
            }
            TestJobCreateParameters jobCreationParams = new TestJobCreateParameters();

            jobCreationParams.RunbookName = runbookName;
            if (draft.Parameters.Count > 0)
            {
                /* User needs to specify values for them */
                var existingParams = await GetTestJobParams();

                RunbookParamDialog paramDialog = new RunbookParamDialog(draft.Parameters, existingParams);
                if (paramDialog.ShowDialog() == true)
                {
                    jobCreationParams.Parameters = paramDialog.paramValues;
                }
                else
                {
                    return(null);
                }
            }
            /* start the test job */
            TestJobCreateResponse jobResponse = await iseClient.automationManagementClient.TestJobs.CreateAsync(
                iseClient.accountResourceGroups[iseClient.currAccount].Name,
                iseClient.currAccount.Name, jobCreationParams, new System.Threading.CancellationToken());

            if (jobResponse == null || jobResponse.StatusCode != System.Net.HttpStatusCode.Created)
            {
                throw new Exception("The test job could not be created: received HTTP status code " + jobResponse.StatusCode);
            }
            return(jobResponse);
        }
        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;
        }