public object GetAutoCompletionPrefix(string guid, string command)
        {
            if (!IsLoggedInUserAuthorized ||
                !SessionElevationManager.IsSessionTokenElevated(ApplicationNames.ISE))
            {
                return(string.Empty);
            }

            PowerShellLog.Info($"Auto completion requested in session '{guid}' by user: '******'");

            var serializer = new JavaScriptSerializer();
            var session    = GetScriptSession(guid);

            try
            {
                var result = serializer.Serialize(CommandCompletion.GetPrefix(session, command));
                return(result);
            }
            finally
            {
                if (string.IsNullOrEmpty(guid))
                {
                    ScriptSessionManager.RemoveSession(session);
                }
            }
        }
        private static string[] GetHelpOutputs(string guid, string command)
        {
            var session = GetScriptSession(guid);

            try
            {
                var result = CommandHelp.GetHelp(session, command);
                return(result.ToArray());
            }
            finally
            {
                if (string.IsNullOrEmpty(guid))
                {
                    ScriptSessionManager.RemoveSession(session);
                }
            }
        }
        private static string[] GetTabCompletionOutputs(string guid, string command, bool lastTokenOnly)
        {
            var session = GetScriptSession(guid);

            try
            {
                var result = CommandCompletion.FindMatches(session, command, lastTokenOnly);
                return(result.ToArray());
            }
            finally
            {
                if (string.IsNullOrEmpty(guid))
                {
                    ScriptSessionManager.RemoveSession(session);
                }
            }
        }
        protected override void ProcessSession(ScriptSession session)
        {
            if (!ShouldProcess(session.ID, "Remove existing script session"))
            {
                return;
            }

            if (session.State == RunspaceAvailability.Busy)
            {
                WriteError(typeof(CmdletInvocationException),
                           $"The script session with Id '{session.ID}' cannot be unloaded because it is in the Busy state. Use Stop-ScriptSession or wait for the operation to complete.",
                           ErrorIds.ScriptSessionBusy, ErrorCategory.ResourceBusy, session.ID);
                return;
            }

            ScriptSessionManager.RemoveSession(session);
        }
Esempio n. 5
0
        public string DisposeScriptSession(string userName, string password, string sessionId)
        {
            if (!WebServiceSettings.ServiceEnabledRemoting)
            {
                return(string.Empty);
            }

            Login(userName, password);

            if (ScriptSessionManager.SessionExists(sessionId))
            {
                var session = ScriptSessionManager.GetSession(sessionId, ApplicationNames.RemoteAutomation, false);
                ScriptSessionManager.RemoveSession(session);
                return("removed");
            }

            return("not found");
        }
Esempio n. 6
0
        public string ExecuteScriptBlockinSite2(string userName, string password, string script, string cliXmlArgs,
                                                string siteName, string sessionId)
        {
            if (!WebServiceSettings.IsEnabled(WebServiceSettings.ServiceRemoting))
            {
                return(string.Empty);
            }
            if (!Login(userName, password))
            {
                return("<Objs xmlns=\"http://schemas.microsoft.com/powershell/2004/04\"><Obj RefId=\"0\"><S>login failed</S></Obj></Objs>");
            }

            PowerShellLog.Info($"Script executed in session {sessionId} through remoting by user: '******'");

            var scriptSession = ScriptSessionManager.GetSession(sessionId, ApplicationNames.RemoteAutomation, false);

            Sitecore.Context.SetActiveSite(siteName);

            if (!String.IsNullOrEmpty(cliXmlArgs))
            {
                scriptSession.SetVariable("cliXmlArgs", cliXmlArgs);
                scriptSession.ExecuteScriptPart("$params = ConvertFrom-CliXml -InputObject $cliXmlArgs", false, true);
                script = script.TrimEnd(' ', '\t', '\n');
            }
            var outObjects = scriptSession.ExecuteScriptPart(script, false, false, false);

            if (scriptSession.LastErrors != null && scriptSession.LastErrors.Any())
            {
                outObjects.AddRange(scriptSession.LastErrors);
            }
            scriptSession.SetVariable("results", outObjects);
            scriptSession.Output.Clear();
            scriptSession.ExecuteScriptPart("ConvertTo-CliXml -InputObject $results");
            var result = scriptSession.Output.Select(p => p.Text).Aggregate((current, next) => current + next);

            if (String.IsNullOrEmpty(sessionId))
            {
                ScriptSessionManager.RemoveSession(scriptSession);
            }
            return(result);
        }
Esempio n. 7
0
        public string DisposeScriptSession(string userName, string password, string sessionId)
        {
            if (!WebServiceSettings.IsEnabled(WebServiceSettings.ServiceRemoting))
            {
                return(string.Empty);
            }

            if (!Login(userName, password))
            {
                return("login failed");
            }

            PowerShellLog.Info($"Session '{sessionId}' disposed by user: '******'");

            if (ScriptSessionManager.GetSessionIfExists(sessionId) is ScriptSession session)
            {
                ScriptSessionManager.RemoveSession(session);
                return("removed");
            }

            return("not found");
        }
        public object GetAutoCompletionPrefix(string guid, string command)
        {
            if (!IsLoggedInUserAuthorized)
            {
                return(string.Empty);
            }
            var serializer = new JavaScriptSerializer();
            var session    = GetScriptSession(guid);

            try
            {
                var result = serializer.Serialize(CommandCompletion.GetPrefix(session, command));
                return(result);
            }
            finally
            {
                if (string.IsNullOrEmpty(guid))
                {
                    ScriptSessionManager.RemoveSession(session);
                }
            }
        }
Esempio n. 9
0
        public object GetAutoCompletionPrefix(string guid, string command)
        {
            if (!WebServiceSettings.ServiceEnabledClient || !Sitecore.Context.IsLoggedIn)
            {
                return(string.Empty);
            }
            var serializer = new JavaScriptSerializer();
            var session    = GetScriptSession(guid);

            try
            {
                var result = serializer.Serialize(CommandCompletion.GetPrefix(session, command));
                return(result);
            }
            finally
            {
                if (string.IsNullOrEmpty(guid))
                {
                    ScriptSessionManager.RemoveSession(session);
                }
            }
        }
Esempio n. 10
0
        protected override void ProcessSession(ScriptSession session)
        {
            if (!ShouldProcess(session.ID, "Receive results from existing script session"))
            {
                return;
            }

            if (session.State == RunspaceAvailability.Busy)
            {
                WriteError(typeof(CmdletInvocationException),
                           $"The script session with Id '{session.ID}' cannot be received because it is in the Busy state. Use Stop-ScriptSession or wait for the operation to complete.",
                           ErrorIds.ScriptSessionBusy,
                           ErrorCategory.ResourceBusy, session.ID);
                return;
            }

            if (HostOutput)
            {
                WriteObject(session.Output.ToString());
            }
            else
            {
                WriteObject(session.JobResultsStore);
            }

            if (KeepResult)
            {
                return;
            }
            session.JobResultsStore = null;

            if (KeepSession)
            {
                return;
            }
            ScriptSessionManager.RemoveSession(session);
        }
Esempio n. 11
0
        public string ExecuteScriptBlockinSite2(string userName, string password, string script, string cliXmlArgs,
                                                string siteName, string sessionId)
        {
            if (!WebServiceSettings.ServiceEnabledRemoting)
            {
                return(string.Empty);
            }
            Login(userName, password);

            var scriptSession = ScriptSessionManager.GetSession(sessionId, ApplicationNames.RemoteAutomation, false);

            Sitecore.Context.SetActiveSite(siteName);

            if (!String.IsNullOrEmpty(cliXmlArgs))
            {
                scriptSession.SetVariable("cliXmlArgs", cliXmlArgs);
                scriptSession.ExecuteScriptPart("$params = ConvertFrom-CliXml -InputObject $cliXmlArgs", false, true);
                script = script.TrimEnd(' ', '\t', '\n');
            }
            var outObjects = scriptSession.ExecuteScriptPart(script, false, false, false);

            if (scriptSession.LastErrors != null && scriptSession.LastErrors.Any())
            {
                outObjects.AddRange(scriptSession.LastErrors);
            }
            scriptSession.SetVariable("results", outObjects);
            scriptSession.Output.Clear();
            scriptSession.ExecuteScriptPart("ConvertTo-CliXml -InputObject $results");
            var result = scriptSession.Output.Select(p => p.Text).Aggregate((current, next) => current + next);

            if (String.IsNullOrEmpty(sessionId))
            {
                ScriptSessionManager.RemoveSession(scriptSession);
            }
            return(result);
        }
Esempio n. 12
0
        private static void ProcessScript(HttpContext context, string script, Dictionary <string, Stream> streams, string cliXmlArgs = null, bool rawOutput = false, string sessionId = null, bool persistentSession = false)
        {
            if (string.IsNullOrEmpty(script))
            {
                context.Response.StatusCode        = 404;
                context.Response.StatusDescription = "The specified script is invalid.";
                return;
            }

            var session = ScriptSessionManager.GetSession(sessionId, ApplicationNames.RemoteAutomation, false);

            if (Context.Database != null)
            {
                var item = Context.Database.GetRootItem();
                if (item != null)
                {
                    session.SetItemLocationContext(item);
                }
            }

            context.Response.ContentType = "text/plain";

            if (streams != null)
            {
                var scriptArguments = new Hashtable();
                foreach (var param in context.Request.QueryString.AllKeys)
                {
                    var paramValue = HttpContext.Current.Request.QueryString[param];
                    if (string.IsNullOrEmpty(param))
                    {
                        continue;
                    }
                    if (string.IsNullOrEmpty(paramValue))
                    {
                        continue;
                    }

                    scriptArguments[param] = paramValue;
                }

                foreach (var param in context.Request.Params.AllKeys)
                {
                    var paramValue = context.Request.Params[param];
                    if (string.IsNullOrEmpty(param))
                    {
                        continue;
                    }
                    if (string.IsNullOrEmpty(paramValue))
                    {
                        continue;
                    }

                    if (session.GetVariable(param) == null)
                    {
                        session.SetVariable(param, paramValue);
                    }
                }

                session.SetVariable("requestStreams", streams);
                session.SetVariable("scriptArguments", scriptArguments);
                session.ExecuteScriptPart(script, true);
                context.Response.Write(session.Output.ToString());
            }
            else
            {
                // Duplicate the behaviors of the original RemoteAutomation service.
                var requestUri = WebUtil.GetRequestUri();
                var site       = SiteContextFactory.GetSiteContext(requestUri.Host, Context.Request.FilePath,
                                                                   requestUri.Port);
                Context.SetActiveSite(site.Name);

                if (!string.IsNullOrEmpty(cliXmlArgs))
                {
                    session.SetVariable("cliXmlArgs", cliXmlArgs);
                    session.ExecuteScriptPart("$params = ConvertFrom-CliXml -InputObject $cliXmlArgs", false, true);
                    script = script.TrimEnd(' ', '\t', '\n');
                }

                var outObjects = session.ExecuteScriptPart(script, false, false, false) ?? new List <object>();
                var response   = context.Response;
                if (rawOutput)
                {
                    // In this output we want to give raw output data. No type information is needed. Error streams are lost.
                    if (outObjects.Any())
                    {
                        foreach (var outObject in outObjects)
                        {
                            response.Write(outObject.ToString());
                        }
                    }

                    if (session.LastErrors != null && session.LastErrors.Any())
                    {
                        var convertedObjects = new List <object>();
                        convertedObjects.AddRange(session.LastErrors);

                        session.SetVariable("results", convertedObjects);
                        session.Output.Clear();
                        session.ExecuteScriptPart("ConvertTo-CliXml -InputObject $results");

                        response.Write("<#messages#>");
                        foreach (var outputBuffer in session.Output)
                        {
                            response.Write(outputBuffer.Text);
                        }
                    }
                }
                else
                {
                    // In this output we want to preserve type information. Ideal for objects with a small output content.
                    if (session.LastErrors != null && session.LastErrors.Any())
                    {
                        outObjects.AddRange(session.LastErrors);
                    }

                    if (outObjects.Any())
                    {
                        session.SetVariable("results", outObjects);
                        session.Output.Clear();
                        session.ExecuteScriptPart("ConvertTo-CliXml -InputObject $results");

                        foreach (var outputBuffer in session.Output)
                        {
                            response.Write(outputBuffer.Text);
                        }
                    }
                }
            }

            if (session.Output.HasErrors)
            {
                context.Response.StatusCode        = 424;
                context.Response.StatusDescription = "Method Failure";
            }

            if (string.IsNullOrEmpty(sessionId) || !persistentSession)
            {
                ScriptSessionManager.RemoveSession(session);
            }
        }