protected override void ProcessRecord()
        {
            if (Id != null && Id.Length > 0)
            {
                foreach (var sessionId in Id)
                {
                    if (!string.IsNullOrEmpty(sessionId))
                    {
                        if (ScriptSessionManager.SessionExistsForAnyUserSession(sessionId))
                        {
                            var sessions = ScriptSessionManager.GetMatchingSessionsForAnyUserSession(sessionId).ToList();
                            foreach (var session in sessions)
                            {
                                ProcessSession(session);
                            }
                        }
                        else
                        {
                            WriteError(typeof(ObjectNotFoundException),
                                       $"The script session with Id '{sessionId}' cannot be found.",
                                       ErrorIds.ScriptSessionNotFound, ErrorCategory.ResourceUnavailable, sessionId);
                        }
                    }
                    else
                    {
                        WriteError(typeof(ObjectNotFoundException),
                                   "The script session Id cannot be null or empty.",
                                   ErrorIds.ScriptSessionNotFound, ErrorCategory.ResourceUnavailable, Id);
                    }
                }

                return;
            }

            if (Session == null || Session.Length == 0)
            {
                WriteError(typeof(ObjectNotFoundException), "Script session cannot be found.",
                           ErrorIds.ScriptSessionNotFound, ErrorCategory.ResourceUnavailable, string.Empty);
                return;
            }

            foreach (var session in Session)
            {
                ProcessSession(session);
            }
        }
Beispiel #2
0
        protected override void ProcessRecord()
        {
            if (ParameterSetName.Is("ID") || ParameterSetName.Is("Session"))
            {
                base.ProcessRecord();
                return;
            }

            if (Current.IsPresent)
            {
                var currentSessionId = CurrentSessionId;
                if (!string.IsNullOrEmpty(currentSessionId))
                {
                    WriteObject(ScriptSessionManager.GetAll().Where(s => currentSessionId.Equals(s.ID, StringComparison.OrdinalIgnoreCase)), true);
                }
            }
            else if (Id != null && Id.Length > 0)
            {
                foreach (var id in Id)
                {
                    if (ScriptSessionManager.SessionExistsForAnyUserSession(id))
                    {
                        ScriptSessionManager.GetMatchingSessionsForAnyUserSession(id).ForEach(ProcessSession);
                    }
                    else
                    {
                        WriteError(typeof(ObjectNotFoundException),
                                   $"The script session with with Id '{id}' cannot be found.", ErrorIds.ScriptSessionNotFound,
                                   ErrorCategory.ResourceBusy, Id);
                    }
                }
            }
            else
            {
                ScriptSessionManager.GetAll().ForEach(ProcessSession);
            }
        }
        protected override void ProcessRecord()
        {
            if (Interactive && !HostData.ScriptingHost.Interactive)
            {
                RecoverHttpContext();
                WriteError(typeof(CmdletInvocationException),
                           "An interactive script session cannot be started from non interactive script session.",
                           ErrorIds.OriginatingScriptSessionNotInteractive, ErrorCategory.InvalidOperation, HostData.ScriptingHost.SessionId);
                return;
            }

            var script = string.Empty;

            scriptItem = Item;

            if (Item != null)
            {
                scriptItem = Item;
                if (!IsPowerShellScriptItem(scriptItem))
                {
                    return;
                }
                script = Item[Templates.Script.Fields.ScriptBody];
            }
            else if (Path != null)
            {
                var drive = IsCurrentDriveSitecore ? CurrentDrive : ApplicationSettings.ScriptLibraryDb;

                scriptItem = PathUtilities.GetItem(Path, drive, ApplicationSettings.ScriptLibraryPath);

                if (scriptItem == null)
                {
                    WriteError(typeof(ItemNotFoundException), $"The script '{Path}' cannot be found.",
                               ErrorIds.ItemNotFound, ErrorCategory.ObjectNotFound, Path);
                    return;
                }

                if (!IsPowerShellScriptItem(scriptItem))
                {
                    return;
                }

                script = scriptItem[Templates.Script.Fields.ScriptBody];
            }

            if (!ShouldProcess(scriptItem?.GetProviderPath() ?? string.Empty, "Start new script session"))
            {
                return;
            }

            scriptBlock = ScriptBlock ?? InvokeCommand.NewScriptBlock(script);

            // sessions from IDs
            if (Id != null && Id.Length > 0)
            {
                foreach (var id in Id)
                {
                    // is id defined?
                    if (string.IsNullOrEmpty(id))
                    {
                        WriteError(typeof(ObjectNotFoundException),
                                   "The script session Id cannot be null or empty.",
                                   ErrorIds.ScriptSessionNotFound, ErrorCategory.ResourceUnavailable, Id);
                        break;
                    }

                    // is it a wildcard search for session?
                    if (id.Contains("*") || id.Contains("?"))
                    {
                        if (ScriptSessionManager.SessionExistsForAnyUserSession(id))
                        {
                            ScriptSessionManager.GetMatchingSessionsForAnyUserSession(id).ForEach(ProcessSession);
                        }
                        else
                        {
                            WriteError(typeof(ObjectNotFoundException),
                                       $"The script session with Id '{Id}' cannot be found.",
                                       ErrorIds.ScriptSessionNotFound, ErrorCategory.ResourceUnavailable, Id);
                        }
                        break;
                    }
                    // does session exist?
                    if (ScriptSessionManager.SessionExistsForAnyUserSession(id))
                    {
                        ScriptSessionManager.GetMatchingSessionsForAnyUserSession(id).ForEach(ProcessSession);
                    }
                    else // OK... fine... execute in a new persistent session!
                    {
                        ProcessSession(ScriptSessionManager.GetSession(id, ApplicationNames.BackgroundJob, false));
                    }
                }

                return;
            }

            if (Session != null)
            {
                if (Session.Length == 0)
                {
                    WriteError(typeof(ObjectNotFoundException), "Script session cannot be found.",
                               ErrorIds.ScriptSessionNotFound, ErrorCategory.ResourceUnavailable, string.Empty);
                    return;
                }
                foreach (var session in Session)
                {
                    ProcessSession(session);
                }
            }

            ProcessSession(ScriptSessionManager.GetSession(string.Empty, ApplicationNames.BackgroundJob, false));
        }