protected override void ProcessSession(ScriptSession session)
        {
            if (session.State == RunspaceAvailability.Busy)
            {
                WriteError(typeof(CmdletInvocationException),
                           $"The script session with Id '{session.ID}' cannot execute the script because it is Busy. Use Stop-ScriptSession or wait for the operation to complete.",
                           ErrorIds.ScriptSessionBusy, ErrorCategory.ResourceBusy, session.ID);
                return;
            }
            session.AutoDispose = AutoDispose;
            session.SetItemLocationContext(ContextItem);

            if (ArgumentList != null)
            {
                foreach (string argument in ArgumentList.Keys)
                {
                    session.SetVariable(argument, ArgumentList[argument]);
                }
            }

            var handle     = string.IsNullOrEmpty(JobName) ? ID.NewID.ToString() : JobName;
            var jobOptions = new JobOptions(GetJobId(session.ID, handle), "PowerShell", "shell", this, nameof(RunJob),
                                            new object[] { session, scriptBlock.ToString() })
            {
                AfterLife      = new TimeSpan(0, 0, 1),
                ContextUser    = Identity ?? Context.User,
                EnableSecurity = !DisableSecurity,
                ClientLanguage = Context.ContentLanguage
            };

            if (Interactive)
            {
                var appParams = new Hashtable();
                if (scriptItem != null)
                {
                    appParams.Add("scriptId", scriptItem.ID.ToString());
                    appParams.Add("scriptDb", scriptItem.Database.Name);
                }
                else
                {
                    session.JobScript  = scriptBlock.ToString();
                    session.JobOptions = jobOptions;
                }
                appParams.Add("cfs", "1");
                appParams.Add("sessionKey", session.Key);

                var message = new ShowApplicationMessage("PowerShell/PowerShell Runner", "PowerShell", "", "500", "360",
                                                         false, appParams)
                {
                    Title = " "
                };
                PutMessage(message);
            }
            else
            {
                Sitecore.Jobs.JobManager.Start(jobOptions);
            }
            WriteObject(session);
        }
Esempio n. 2
0
        private static void ProcessTaskItem(Item item, ScriptSession session)
        {
            var featureRoot = ModuleManager.GetItemModule(item)?
                              .GetFeatureRoot(IntegrationPoints.TasksFeature);

            if (!RulesUtils.EvaluateRules(featureRoot?[Templates.ScriptLibrary.Fields.EnableRule], item))
            {
                return;
            }

            var queue = new Queue <Item>();

            queue.Enqueue(item);

            Item currentItem;

            while (queue.Count > 0 && (currentItem = queue.Dequeue()) != null)
            {
                if (currentItem.IsPowerShellScript())
                {
                    if (string.IsNullOrWhiteSpace(currentItem[Templates.Script.Fields.ScriptBody]))
                    {
                        continue;
                    }
                    if (!RulesUtils.EvaluateRules(currentItem[Templates.Script.Fields.EnableRule], currentItem))
                    {
                        continue;
                    }

                    session.SetItemLocationContext(currentItem);
                    session.ExecuteScriptPart(currentItem, true);
                }
                else if (currentItem.IsPowerShellLibrary() && currentItem.HasChildren)
                {
                    if (!RulesUtils.EvaluateRules(currentItem[Templates.ScriptLibrary.Fields.EnableRule], currentItem))
                    {
                        continue;
                    }

                    var children = currentItem.Children.ToArray();
                    foreach (var child in children)
                    {
                        queue.Enqueue(child);
                    }
                }
            }
        }