Ejemplo n.º 1
0
        protected override void BeginProcessing()
        {
            var          shouldExit = true;
            InternalHost host       = base.Host as InternalHost;

            if (host != null)
            {
                if (host.Runspace.InNestedPrompt)
                {
                    host.ExitNestedPrompt();
                    shouldExit = host.ShouldExit;
                }
                if (shouldExit)
                {
                    base.WriteVerbose("\r\n");
                    System.Management.Automation.Sqm.PSSQMAPI.LogAllDataSuppressExceptions();
                    Environment.Exit(0);
                }
            }
            else if (base.Context.IsSingleShell)
            {
                base.WriteVerbose("\r\n\r\nExiting PowerShell...\r\n");
                Environment.Exit(0);
            }

            //base.BeginProcessing ();
        }
Ejemplo n.º 2
0
        protected override void BeginProcessing()
        {
            InternalHost host = base.Host as InternalHost;

            if (host != null)
            {
                ConsoleHost externalHost = host.ExternalHost as ConsoleHost;
                if (externalHost != null)
                {
                    if (!externalHost.IsTranscribing)
                    {
                        base.WriteObject(TranscriptStrings.TranscriptionNotInProgress);
                    }
                    try
                    {
                        string str = externalHost.StopTranscribing();
                        base.WriteObject(StringUtil.Format(TranscriptStrings.TranscriptionStopped, str));
                    }
                    catch (Exception exception1)
                    {
                        Exception exception = exception1;
                        ConsoleHost.CheckForSevereException(exception);
                        object[] message = new object[1];
                        message[0] = exception.Message;
                        throw PSTraceSource.NewInvalidOperationException(exception, "TranscriptStrings", "ErrorStoppingTranscript", message);
                    }
                    return;
                }
                else
                {
                    throw PSTraceSource.NewNotSupportedException("TranscriptStrings", "HostDoesNotSupportTranscript", new object[0]);
                }
            }
            else
            {
                throw PSTraceSource.NewNotSupportedException("TranscriptStrings", "HostDoesNotSupportTranscript", new object[0]);
            }
        }
Ejemplo n.º 3
0
        protected override void ProcessRecord()
        {
            IHostSupportsInteractiveSession session = base.Host as IHostSupportsInteractiveSession;

            if (session == null)
            {
                base.WriteError(new ErrorRecord(new ArgumentException(base.GetMessage(RemotingErrorIdStrings.HostDoesNotSupportPushRunspace)), PSRemotingErrorId.HostDoesNotSupportPushRunspace.ToString(), ErrorCategory.InvalidArgument, null));
            }

            /*
             * else if (((base.Context != null) && (base.Context.EngineHostInterface != null)) && ((base.Context.EngineHostInterface.ExternalHost != null) && (base.Context.EngineHostInterface.ExternalHost is ServerRemoteHost)))
             * {
             * base.WriteError(new ErrorRecord(new ArgumentException(base.GetMessage(RemotingErrorIdStrings.RemoteHostDoesNotSupportPushRunspace)), PSRemotingErrorId.RemoteHostDoesNotSupportPushRunspace.ToString(), ErrorCategory.InvalidArgument, null));
             * }
             */
            else
            {
                InternalHost targetObject = base.Host as InternalHost;
                if ((targetObject != null) && targetObject.HostInNestedPrompt())
                {
                    base.ThrowTerminatingError(new ErrorRecord(new InvalidOperationException(PSRemotingErrorInvariants.FormatResourceString(RemotingErrorIdStrings.HostInNestedPrompt, new object[0])), "HostInNestedPrompt", ErrorCategory.InvalidOperation, targetObject));
                }
                RemoteRunspace runspaceMatchingRunspaceId = null;
                string         parameterSetName           = base.ParameterSetName;
                if (parameterSetName != null)
                {
                    if (!(parameterSetName == "ComputerName"))
                    {
                        if (parameterSetName == "Uri")
                        {
                            runspaceMatchingRunspaceId = this.CreateRunspaceWhenUriParameterSpecified();
                        }
                        else if (parameterSetName == "Session")
                        {
                            runspaceMatchingRunspaceId = (RemoteRunspace)this.remoteRunspaceInfo.Runspace;
                        }
                        else if (parameterSetName == "InstanceId")
                        {
                            runspaceMatchingRunspaceId = this.GetRunspaceMatchingRunspaceId(this.InstanceId);
                        }
                        else if (parameterSetName == "Id")
                        {
                            runspaceMatchingRunspaceId = this.GetRunspaceMatchingSessionId(this.Id);
                        }
                        else if (parameterSetName == "Name")
                        {
                            runspaceMatchingRunspaceId = this.GetRunspaceMatchingName(this.Name);
                        }
                    }
                    else
                    {
                        runspaceMatchingRunspaceId = this.CreateRunspaceWhenComputerNameParameterSpecified();
                    }
                }
                if (runspaceMatchingRunspaceId != null)
                {
                    if (runspaceMatchingRunspaceId.RunspaceStateInfo.State == RunspaceState.Disconnected)
                    {
                        if (!runspaceMatchingRunspaceId.CanConnect)
                        {
                            string message = StringUtil.Format(RemotingErrorIdStrings.SessionNotAvailableForConnection, new object[0]);
                            base.WriteError(new ErrorRecord(new RuntimeException(message), "EnterPSSessionCannotConnectDisconnectedSession", ErrorCategory.InvalidOperation, runspaceMatchingRunspaceId));
                            return;
                        }
                        Exception innerException = null;
                        try
                        {
                            runspaceMatchingRunspaceId.Connect();
                        }
                        catch (PSRemotingTransportException exception2)
                        {
                            innerException = exception2;
                        }
                        catch (PSInvalidOperationException exception3)
                        {
                            innerException = exception3;
                        }
                        catch (InvalidRunspacePoolStateException exception4)
                        {
                            innerException = exception4;
                        }
                        if (innerException != null)
                        {
                            string str2 = StringUtil.Format(RemotingErrorIdStrings.SessionConnectFailed, new object[0]);
                            base.WriteError(new ErrorRecord(new RuntimeException(str2, innerException), "EnterPSSessionConnectSessionFailed", ErrorCategory.InvalidOperation, runspaceMatchingRunspaceId));
                            return;
                        }
                        if (runspaceMatchingRunspaceId.RunspaceAvailability == RunspaceAvailability.Busy)
                        {
                            string str3 = StringUtil.Format(RemotingErrorIdStrings.EnterPSSessionDisconnected, runspaceMatchingRunspaceId.Name);
                            base.WriteError(new ErrorRecord(new RuntimeException(str3, innerException), "EnterPSSessionConnectSessionNotAvailable", ErrorCategory.InvalidOperation, this.remoteRunspaceInfo));
                            runspaceMatchingRunspaceId.DisconnectAsync();
                            return;
                        }
                    }
                    if (runspaceMatchingRunspaceId.RunspaceStateInfo.State != RunspaceState.Opened)
                    {
                        if (base.ParameterSetName == "Session")
                        {
                            string str4 = (this.remoteRunspaceInfo != null) ? this.remoteRunspaceInfo.Name : string.Empty;
                            base.WriteError(new ErrorRecord(new ArgumentException(base.GetMessage(RemotingErrorIdStrings.EnterPSSessionBrokenSession, new object[] { str4, runspaceMatchingRunspaceId.ConnectionInfo.ComputerName, runspaceMatchingRunspaceId.InstanceId })), PSRemotingErrorId.PushedRunspaceMustBeOpen.ToString(), ErrorCategory.InvalidArgument, null));
                        }
                        else
                        {
                            base.WriteError(new ErrorRecord(new ArgumentException(base.GetMessage(RemotingErrorIdStrings.PushedRunspaceMustBeOpen)), PSRemotingErrorId.PushedRunspaceMustBeOpen.ToString(), ErrorCategory.InvalidArgument, null));
                        }
                    }
                    else
                    {
                        if (runspaceMatchingRunspaceId.RunspaceAvailability != RunspaceAvailability.Available)
                        {
                            base.WriteWarning(base.GetMessage(RunspaceStrings.RunspaceNotReady));
                        }
                        try
                        {
                            session.PushRunspace(runspaceMatchingRunspaceId);
                        }
                        catch (Exception)
                        {
                            if ((runspaceMatchingRunspaceId != null) && runspaceMatchingRunspaceId.ShouldCloseOnPop)
                            {
                                runspaceMatchingRunspaceId.Close();
                            }
                            throw;
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        private void InvokeThreadProc()
        {
            bool     flag            = false;
            Runspace defaultRunspace = Runspace.DefaultRunspace;

            try
            {
                WindowsImpersonationContext context = null;
                try
                {
                    if ((base.InvocationSettings != null) && base.InvocationSettings.FlowImpersonationPolicy)
                    {
                        context = new WindowsIdentity(base.InvocationSettings.WindowsIdentityToImpersonate.Token).Impersonate();
                    }
                    if ((base.InvocationSettings != null) && (base.InvocationSettings.Host != null))
                    {
                        InternalHost host = base.InvocationSettings.Host as InternalHost;
                        if (host != null)
                        {
                            this.LocalRunspace.ExecutionContext.InternalHost.SetHostRef(host.ExternalHost);
                        }
                        else
                        {
                            this.LocalRunspace.ExecutionContext.InternalHost.SetHostRef(base.InvocationSettings.Host);
                        }
                    }
                    if (this.LocalRunspace.ExecutionContext.InternalHost.ExternalHost.ShouldSetThreadUILanguageToZero)
                    {
                        NativeCultureResolver.SetThreadUILanguage(0);
                    }
                    Runspace.DefaultRunspace = this.LocalRunspace;
                    this.InvokeHelper();
                    base.SetPipelineState(PipelineState.Completed);
                }
                finally
                {
                    if (context != null)
                    {
                        try
                        {
                            context.Undo();
                            context.Dispose();
                            context = null;
                        }
                        catch (SecurityException)
                        {
                        }
                    }
                }
            }
            catch (PipelineStoppedException exception)
            {
                base.SetPipelineState(PipelineState.Stopped, exception);
            }
            catch (RuntimeException exception2)
            {
                flag = exception2 is IncompleteParseException;
                base.SetPipelineState(PipelineState.Failed, exception2);
                base.SetHadErrors(true);
            }
            catch (ScriptCallDepthException exception3)
            {
                base.SetPipelineState(PipelineState.Failed, exception3);
                base.SetHadErrors(true);
            }
            catch (SecurityException exception4)
            {
                base.SetPipelineState(PipelineState.Failed, exception4);
                base.SetHadErrors(true);
            }
            catch (ThreadAbortException exception5)
            {
                base.SetPipelineState(PipelineState.Failed, exception5);
                base.SetHadErrors(true);
            }
            catch (HaltCommandException)
            {
                base.SetPipelineState(PipelineState.Completed);
            }
            finally
            {
                if (((base.InvocationSettings != null) && (base.InvocationSettings.Host != null)) && this.LocalRunspace.ExecutionContext.InternalHost.IsHostRefSet)
                {
                    this.LocalRunspace.ExecutionContext.InternalHost.RevertHostRef();
                }
                Runspace.DefaultRunspace = defaultRunspace;
                if (!flag)
                {
                    try
                    {
                        bool inBreakpoint = this.LocalRunspace.ExecutionContext.Debugger.InBreakpoint;
                        if (this._historyIdForThisPipeline == -1L)
                        {
                            this.AddHistoryEntry(inBreakpoint);
                        }
                        else
                        {
                            this.UpdateHistoryEntryAddedByAddHistoryCmdlet(inBreakpoint);
                        }
                    }
                    catch (TerminateException)
                    {
                    }
                }
                if (base.OutputStream.IsOpen && !this.IsChild)
                {
                    try
                    {
                        base.OutputStream.Close();
                    }
                    catch (ObjectDisposedException)
                    {
                    }
                }
                if (base.ErrorStream.IsOpen && !this.IsChild)
                {
                    try
                    {
                        base.ErrorStream.Close();
                    }
                    catch (ObjectDisposedException)
                    {
                    }
                }
                if (base.InputStream.IsOpen && !this.IsChild)
                {
                    try
                    {
                        base.InputStream.Close();
                    }
                    catch (ObjectDisposedException)
                    {
                    }
                }
                this.ClearStreams();
                this.LocalRunspace.RemoveFromRunningPipelineList(this);
                if (!base.SyncInvokeCall)
                {
                    base.RaisePipelineStateEvents();
                }
            }
        }
Ejemplo n.º 5
0
        protected override void BeginProcessing()
        {
            InternalHost host = base.Host as InternalHost;

            if (host != null)
            {
                ConsoleHost externalHost = host.ExternalHost as ConsoleHost;
                if (externalHost != null)
                {
                    if (!externalHost.IsTranscribing)
                    {
                        if (!this.isFilenameSet)
                        {
                            object variableValue = base.GetVariableValue("global:TRANSCRIPT", null);
                            if (variableValue != null)
                            {
                                this.outFilename = (string)variableValue;
                            }
                            else
                            {
                                this.outFilename = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), StringUtil.Format("PowerShell_transcript.{0:yyyyMMddHHmmss}.txt", DateTime.Now));
                            }
                        }
                        try
                        {
                            string str = this.ResolveFilePath(this.Path, this.isLiteralPath);
                            if (base.ShouldProcess(this.Path))
                            {
                                if (File.Exists(str))
                                {
                                    if (this.NoClobber && !this.Append)
                                    {
                                        string      str1 = StringUtil.Format(TranscriptStrings.TranscriptFileExistsNoClobber, str, "NoClobber");
                                        Exception   unauthorizedAccessException = new UnauthorizedAccessException(str1);
                                        ErrorRecord errorRecord = new ErrorRecord(unauthorizedAccessException, "NoClobber", ErrorCategory.ResourceExists, str);
                                        base.ThrowTerminatingError(errorRecord);
                                    }
                                    FileInfo fileInfo = new FileInfo(str);
                                    if ((fileInfo.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                                    {
                                        if (!this.Force)
                                        {
                                            object[] objArray = new object[1];
                                            objArray[0] = str;
                                            Exception exception = PSTraceSource.NewArgumentException(str, "TranscriptStrings", "TranscriptFileReadOnly", objArray);
                                            base.ThrowTerminatingError(new ErrorRecord(exception, "FileReadOnly", ErrorCategory.InvalidArgument, str));
                                        }
                                        else
                                        {
                                            FileInfo attributes = fileInfo;
                                            attributes.Attributes = attributes.Attributes & (FileAttributes.Hidden | FileAttributes.System | FileAttributes.Directory | FileAttributes.Archive | FileAttributes.Device | FileAttributes.Normal | FileAttributes.Temporary | FileAttributes.SparseFile | FileAttributes.ReparsePoint | FileAttributes.Compressed | FileAttributes.Offline | FileAttributes.NotContentIndexed | FileAttributes.Encrypted
#if !MONO
                                                                                             | FileAttributes.IntegrityStream | FileAttributes.NoScrubData
#endif
                                                                                             );
                                        }
                                    }
                                }
                                externalHost.StartTranscribing(str, this.Append);
                                base.WriteObject(StringUtil.Format(TranscriptStrings.TranscriptionStarted, this.Path));
                            }
                        }
                        catch (Exception exception2)
                        {
                            Exception   exception1   = exception2;
                            string      str2         = "CannotStartTranscription";
                            ErrorRecord errorRecord1 = new ErrorRecord(PSTraceSource.NewInvalidOperationException(exception1, "TranscriptStrings", str2, new object[0]), str2, ErrorCategory.InvalidOperation, null);
                            base.ThrowTerminatingError(errorRecord1);
                        }
                        return;
                    }
                    else
                    {
                        throw new InvalidOperationException(TranscriptStrings.TranscriptionInProgress);
                    }
                }
                else
                {
                    throw new PSNotSupportedException(StringUtil.Format(TranscriptStrings.HostDoesNotSupportTranscript, new object[0]));
                }
            }
            else
            {
                throw new PSNotSupportedException(StringUtil.Format(TranscriptStrings.HostDoesNotSupportTranscript, new object[0]));
            }
        }