protected void RunPlugin(ClientPipelineArgs args) { ScriptSession scriptSession = ScriptSessionManager.NewSession(ApplicationNames.IseConsole, true); string scriptDb = args.Parameters["scriptDb"]; string scriptItem = args.Parameters["scriptId"]; Item script = Factory.GetDatabase(scriptDb).GetItem(scriptItem); scriptSession.SetVariable("scriptText", Editor.Value); scriptSession.SetVariable("selectionText", SelectionText.Value.Trim()); scriptSession.SetVariable("scriptItem", ScriptItem); scriptSession.Interactive = true; JobExecuteScript(args, script[ScriptItemFieldNames.Script], scriptSession, true, false); }
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); }
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); } }
private void SetVariables(ScriptSession session, Message message) { //visible objects in string form for export var export = ListViewer.FilteredItems.Select(p => { var psobj = new PSObject(); foreach (var property in p.Display) { psobj.Properties.Add(new PSNoteProperty(property.Key, property.Value)); } return(psobj); }).ToList(); //selected original objects var results = ListViewer.SelectedItems.Select(p => { var id = Int32.Parse(p.Value); return(ListViewer.Data.Data.Where(d => d.Id == id).Select(d => d.Original).First()); }).ToList(); session.SetVariable("filteredData", ListViewer.FilteredItems.Select(p => p.Original).ToList()); session.SetVariable("resultSet", results); session.SetVariable("selectedData", results); session.SetVariable("exportData", export); session.SetVariable("allData", ListViewer.Data.Data.Select(p => p.Original).ToList()); session.SetVariable("allDataInternal", ListViewer.Data.Data); session.SetVariable("actionData", ListViewer.Data.ActionData); session.SetVariable("title", ListViewer.Data.Title); session.SetVariable("infoTitle", ListViewer.Data.InfoTitle); session.SetVariable("infoDescription", ListViewer.Data.InfoDescription); session.SetVariable("formatProperty", ListViewer.Data.Property.Cast <object>().ToArray()); session.SetVariable("formatPropertyStr", ListViewer.Data.FormatProperty); session.SetVariable("exportProperty", ListViewer.Data.ExportProperty); if (message != null) { foreach (var key in message.Arguments.AllKeys) { session.SetVariable(key, message.Arguments[key]); } } }