예제 #1
0
 protected void ExecuteInternal(ScriptSession scriptSession, string script)
 {
     try
     {
         scriptSession.ExecuteScriptPart(script);
     }
     finally
     {
         if (!string.IsNullOrEmpty(scriptSession.DebugFile))
         {
             File.Delete(scriptSession.DebugFile);
             scriptSession.DebugFile = string.Empty;
         }
         scriptSession.Debugging = false;
         scriptSession.ExecuteScriptPart("Get-PSBreakpoint | Remove-PSBreakpoint");
     }
 }
예제 #2
0
 public void Invoke()
 {
     using (ScriptSession scriptSession = ScriptSessionManager.NewSession("Default", true))
     {
         Item   speScriptItem = Sitecore.Context.Database.GetItem("/path-or-id/to-spe-item");
         string script        = speScriptItem["Script"];
         if (!string.IsNullOrEmpty(script))
         {
             scriptSession.ExecuteScriptPart(script);
         }
     }
 }
        protected void RunJob(ScriptSession session, string command)
        {
            if (!IsLoggedInUserAuthorized)
            {
                return;
            }

            try
            {
                if (!string.IsNullOrEmpty(command))
                {
                    if (session.IsRunning)
                    {
                        session.TryInvokeInRunningSession(command);
                    }
                    else
                    {
                        session.ExecuteScriptPart(command);
                    }
                }
            }
            catch (Exception ex)
            {
                var jobManager = TypeResolver.ResolveFromCache <IJobManager>();
                var job        = jobManager.GetContextJob();
                if (job != null)
                {
                    job.StatusFailed = true;

                    var exceptionMessage = ScriptSession.GetExceptionString(ex);
                    if (job.Options.WriteToLog)
                    {
                        PowerShellLog.Error("Error while executing PowerShell Extensions script.", ex);
                    }
                    job.AddStatusMessage(exceptionMessage);
                    job.AddStatusMessage(
                        "Uh oh, looks like the command you ran is invalid or something else went wrong. Is it something we should know about?");
                    job.AddStatusMessage(
                        "Please submit a support ticket here https://git.io/spe with error details, screenshots, and anything else that might help.");
                    job.AddStatusMessage(
                        "We also have a user guide here https://doc.sitecorepowershell.com/.");
                }
                else
                {
                    PowerShellLog.Error("Script execution failed. Could not find command job.", ex);
                }
            }
        }
예제 #4
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);
                    }
                }
            }
        }
예제 #5
0
        /// <summary>
        /// Runs the healthcheck.
        /// </summary>
        public override void RunHealthcheck()
        {
            this.LastCheckTime = DateTime.UtcNow;
            var invokeScript = $"ExecuteHealthcheck -componentId {this.InnerItem.ID.ToString()} -params {GetParamsFromNameValue(this.Parameters)}";

            try
            {
                using (ScriptSession scriptSession = ScriptSessionManager.NewSession("Default", true))
                {
                    string script = this.ScriptItem["Script"];
                    script += Environment.NewLine + invokeScript;
                    if (!string.IsNullOrEmpty(script))
                    {
                        var output = scriptSession.ExecuteScriptPart(script, false);

                        if (output.Count > 0)
                        {
                            Hashtable result = (Hashtable)output[0];
                            this.Status = (HealthcheckStatus)Enum.Parse(typeof(HealthcheckStatus), result["Status"].ToString());

                            if (this.Status != HealthcheckStatus.Healthy)
                            {
                                this.ErrorList.Entries.Add(new ErrorEntry
                                {
                                    Created = DateTime.UtcNow,
                                    Reason  = result["Reason"].ToString()
                                });
                            }
                            else
                            {
                                this.HealthyMessage = result["HealthyMessage"].ToString();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.Status = HealthcheckStatus.Error;
                this.ErrorList.Entries.Add(new ErrorEntry
                {
                    Created   = DateTime.UtcNow,
                    Reason    = ex.Message,
                    Exception = ex
                });
            }
        }
예제 #6
0
        public void Run(PublishContext publishContext)
        {
            Item scriptItem = Factory.GetDatabase("master").GetItem(publishNotificationScriptID);

            try
            {
                using (ScriptSession scriptSession = ScriptSessionManager.NewSession(ApplicationNames.Default, true))
                {
                    scriptSession.SetVariable("publishContext", publishContext);
                    scriptSession.ExecuteScriptPart(scriptItem, true);
                    PowerShellLog.Info($"Job ended: Send Teams Notification");
                }
            }
            catch (Exception ex)
            {
                PowerShellLog.Error($"Error while invoking {scriptItem.Paths.FullPath} script from Send Teams Notification job", ex);
            }
        }
예제 #7
0
        protected void RunJob(ScriptSession session, string command)
        {
            if (!WebServiceSettings.ServiceEnabledClient || !Sitecore.Context.IsLoggedIn)
            {
                return;
            }
            try
            {
                if (!string.IsNullOrEmpty(command))
                {
                    if (session.IsRunning)
                    {
                        session.TryInvokeInRunningSession(command);
                    }
                    else
                    {
                        session.ExecuteScriptPart(command);
                    }
                }
            }
            catch (Exception ex)
            {
                var job = Sitecore.Context.Job;
                if (job != null)
                {
                    job.Status.Failed = true;

                    var exceptionMessage = ScriptSession.GetExceptionString(ex);
                    if (job.Options.WriteToLog)
                    {
                        LogUtils.Error(exceptionMessage, this);
                    }
                    job.Status.Messages.Add(exceptionMessage);
                    job.Status.Messages.Add("Uh oh, looks like the command you ran is invalid or something else went wrong. Is it something we should know about?");
                    job.Status.Messages.Add("Please submit a support ticket here https://git.io/spe with error details, screenshots, and anything else that might help.");
                    job.Status.Messages.Add("We also have a user guide here http://sitecorepowershell.gitbooks.io/sitecore-powershell-extensions/.");
                }
                else
                {
                    LogUtils.Error("Script execution failed. Could not find command job.", ex, this);
                }
            }
        }
예제 #8
0
        protected void RunJob(ScriptSession session, string command)
        {
            try
            {
                session.JobResultsStore = null;
                session.JobResultsStore = session.ExecuteScriptPart(command, false, false, false);
            }
            catch (Exception ex)
            {
                var job = Sitecore.Context.Job;
                if (job != null)
                {
                    job.Status.Failed = true;

                    var exceptionMessage = ScriptSession.GetExceptionString(ex);
                    if (job.Options.WriteToLog)
                    {
                        PowerShellLog.Error("Error while executing PowerShell Extensions script.", ex);
                    }
                    job.Status.Messages.Add(exceptionMessage);
                    job.Status.Messages.Add(
                        "Uh oh, looks like the command you ran is invalid or something else went wrong. Is it something we should know about?");
                    job.Status.Messages.Add(
                        "Please submit a support ticket here https://git.io/spe with error details, screenshots, and anything else that might help.");
                    job.Status.Messages.Add(
                        "We also have a user guide here http://doc.sitecorepowershell.com/.");
                }
                else
                {
                    PowerShellLog.Error("Script execution failed. Could not find command job.", ex);
                }
            }
            finally
            {
                if (session.AutoDispose)
                {
                    session.Dispose();
                }
            }
        }
예제 #9
0
 protected void ExecuteInternal(ScriptSession scriptSession, string script)
 {
     scriptSession.ExecuteScriptPart(script);
 }