Ejemplo n.º 1
0
 internal static void Dispatch(BaseClientTransportManager transportManager, PSHost clientHost, PSDataCollectionStream<ErrorRecord> errorStream, ObjectStream methodExecutorStream, bool isMethodExecutorStreamEnabled, RemoteRunspacePoolInternal runspacePool, Guid clientPowerShellId, System.Management.Automation.Remoting.RemoteHostCall remoteHostCall)
 {
     ClientMethodExecutor executor = new ClientMethodExecutor(transportManager, clientHost, runspacePool.InstanceId, clientPowerShellId, remoteHostCall);
     if (clientPowerShellId == Guid.Empty)
     {
         executor.Execute(errorStream);
     }
     else
     {
         bool flag = false;
         if (clientHost != null)
         {
             PSObject privateData = clientHost.PrivateData;
             if (privateData != null)
             {
                 PSNoteProperty property = privateData.Properties["AllowSetShouldExitFromRemote"] as PSNoteProperty;
                 flag = ((property != null) && (property.Value is bool)) ? ((bool) property.Value) : false;
             }
         }
         if ((remoteHostCall.IsSetShouldExit && isMethodExecutorStreamEnabled) && !flag)
         {
             runspacePool.Close();
         }
         else if (isMethodExecutorStreamEnabled)
         {
             methodExecutorStream.Write(executor);
         }
         else
         {
             executor.Execute(errorStream);
         }
     }
 }
Ejemplo n.º 2
0
 internal ClientRemotePowerShell(System.Management.Automation.PowerShell shell, RemoteRunspacePoolInternal runspacePool)
 {
     this.shell = shell;
     this.clientRunspacePoolId = runspacePool.InstanceId;
     this.runspacePool         = runspacePool;
     this.computerName         = runspacePool.ConnectionInfo.ComputerName;
 }
Ejemplo n.º 3
0
 internal ClientRemotePowerShell(System.Management.Automation.PowerShell shell, RemoteRunspacePoolInternal runspacePool)
 {
     this.shell = shell;
     this.clientRunspacePoolId = runspacePool.InstanceId;
     this.runspacePool = runspacePool;
     this.computerName = runspacePool.ConnectionInfo.ComputerName;
 }
Ejemplo n.º 4
0
 internal ClientRemotePowerShell(
     PowerShell shell,
     ObjectStreamBase inputstream,
     ObjectStreamBase outputstream,
     ObjectStreamBase errorstream,
     PSInformationalBuffers informationalBuffers,
     PSInvocationSettings settings,
     RemoteRunspacePoolInternal runspacePool)
 {
     using (ClientRemotePowerShell.tracer.TraceConstructor((object)this))
     {
         this.shell = shell;
         this.informationalBuffers = informationalBuffers;
         this.InputStream          = inputstream;
         this.errorstream          = errorstream;
         this.outputstream         = outputstream;
         this.settings             = settings;
         this.clientRunspacePoolId = runspacePool.InstanceId;
         this.hostToUse            = settings == null || settings.Host == null ? runspacePool.Host : settings.Host;
         this.computerName         = runspacePool.ConnectionInfo.ComputerName;
         this.dataStructureHandler = runspacePool.DataStructureHandler.CreatePowerShellDataStructureHandler(this);
         this.dataStructureHandler.InvocationStateInfoReceived += new EventHandler <RemoteDataEventArgs <PSInvocationStateInfo> >(this.HandleInvocationStateInfoReceived);
         this.dataStructureHandler.OutputReceived += new EventHandler <RemoteDataEventArgs <object> >(this.HandleOutputReceived);
         this.dataStructureHandler.ErrorReceived  += new EventHandler <RemoteDataEventArgs <ErrorRecord> >(this.HandleErrorReceived);
         this.dataStructureHandler.InformationalMessageReceived       += new EventHandler <RemoteDataEventArgs <InformationalMessage> >(this.HandleInformationalMessageReceived);
         this.dataStructureHandler.HostCallReceived                   += new EventHandler <RemoteDataEventArgs <RemoteHostCall> >(this.HandleHostCallReceived);
         this.dataStructureHandler.ClosedNotificationFromRunspacePool += new EventHandler <RemoteDataEventArgs <Exception> >(this.HandleCloseNotificationFromRunspacePool);
         this.dataStructureHandler.BrokenNotificationFromRunspacePool += new EventHandler <RemoteDataEventArgs <Exception> >(this.HandleBrokenNotificationFromRunspacePool);
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Constructor which creates a client remote powershell
        /// </summary>
        /// <param name="shell">powershell instance.</param>
        /// <param name="runspacePool">The runspace pool associated with
        /// this shell</param>
        internal ClientRemotePowerShell(PowerShell shell, RemoteRunspacePoolInternal runspacePool)
        {
            this.shell           = shell;
            clientRunspacePoolId = runspacePool.InstanceId;
            this.runspacePool    = runspacePool;

            // retrieve the computer name from the runspacepool
            // information so that it can be used in adding
            // warning to host messages
            computerName = runspacePool.ConnectionInfo.ComputerName;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Constructor which creates a client remote powershell 
        /// </summary>
        /// <param name="shell">powershell instance </param>
        /// <param name="runspacePool">The runspace pool associated with
        /// this shell</param>
        internal ClientRemotePowerShell(PowerShell shell, RemoteRunspacePoolInternal runspacePool)
        {
            this.shell = shell;
            clientRunspacePoolId = runspacePool.InstanceId;
            this.runspacePool = runspacePool;

            // retrieve the computer name from the runspacepool
            // information so that it can be used in adding
            // warning to host messages
            computerName = runspacePool.ConnectionInfo.ComputerName;
        }
Ejemplo n.º 7
0
 internal ClientRemoteSessionImpl(RemoteRunspacePoolInternal rsPool, ClientRemoteSession.URIDirectionReported uriRedirectionHandler)
 {
     base.RemoteRunspacePoolInternal = rsPool;
     base.Context.RemoteAddress = WSManConnectionInfo.ExtractPropertyAsWsManConnectionInfo<Uri>(rsPool.ConnectionInfo, "ConnectionUri", null);
     this._cryptoHelper = new PSRemotingCryptoHelperClient();
     this._cryptoHelper.Session = this;
     base.Context.ClientCapability = RemoteSessionCapability.CreateClientCapability();
     base.Context.UserCredential = rsPool.ConnectionInfo.Credential;
     base.Context.ShellName = WSManConnectionInfo.ExtractPropertyAsWsManConnectionInfo<string>(rsPool.ConnectionInfo, "ShellUri", string.Empty);
     this._mySelf = RemotingDestination.InvalidDestination | RemotingDestination.Client;
     base.SessionDataStructureHandler = new ClientRemoteSessionDSHandlerImpl(this, this._cryptoHelper, rsPool.ConnectionInfo, uriRedirectionHandler);
     base.BaseSessionDataStructureHandler = base.SessionDataStructureHandler;
     this._waitHandleForConfigurationReceived = new ManualResetEvent(false);
     base.SessionDataStructureHandler.NegotiationReceived += new EventHandler<RemoteSessionNegotiationEventArgs>(this.HandleNegotiationReceived);
     base.SessionDataStructureHandler.ConnectionStateChanged += new EventHandler<RemoteSessionStateEventArgs>(this.HandleConnectionStateChanged);
     base.SessionDataStructureHandler.EncryptedSessionKeyReceived += new EventHandler<RemoteDataEventArgs<string>>(this.HandleEncryptedSessionKeyReceived);
     base.SessionDataStructureHandler.PublicKeyRequestReceived += new EventHandler<RemoteDataEventArgs<string>>(this.HandlePublicKeyRequestReceived);
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Creates a new instance of ClientRemoteSessionImpl
        /// </summary>
        /// <param name="rsPool">
        /// The RunspacePool object this session should map to.
        /// </param>
        /// <param name="uriRedirectionHandler">
        /// </param>
        internal ClientRemoteSessionImpl(RemoteRunspacePoolInternal rsPool,
                                       URIDirectionReported uriRedirectionHandler)
        {
            Dbg.Assert(null != rsPool, "RunspacePool cannot be null");
            base.RemoteRunspacePoolInternal = rsPool;
            Context.RemoteAddress = WSManConnectionInfo.ExtractPropertyAsWsManConnectionInfo<Uri>(rsPool.ConnectionInfo,
                "ConnectionUri", null);
            _cryptoHelper = new PSRemotingCryptoHelperClient();
            _cryptoHelper.Session = this;
            Context.ClientCapability = RemoteSessionCapability.CreateClientCapability();
            Context.UserCredential = rsPool.ConnectionInfo.Credential;

            // shellName validation is not performed on the client side.
            // This is recommended by the WinRS team: for the reason that the rules may change in the future.
            Context.ShellName = WSManConnectionInfo.ExtractPropertyAsWsManConnectionInfo<string>(rsPool.ConnectionInfo,
                "ShellUri", string.Empty);

            MySelf = RemotingDestination.Client;
            //Create session data structure handler for this session
            SessionDataStructureHandler = new ClientRemoteSessionDSHandlerImpl(this,
                _cryptoHelper,
                rsPool.ConnectionInfo,
                uriRedirectionHandler);
            BaseSessionDataStructureHandler = SessionDataStructureHandler;
            _waitHandleForConfigurationReceived = new ManualResetEvent(false);

            //Register handlers for various ClientSessiondata structure handler events
            SessionDataStructureHandler.NegotiationReceived += HandleNegotiationReceived;
            SessionDataStructureHandler.ConnectionStateChanged += HandleConnectionStateChanged;
            SessionDataStructureHandler.EncryptedSessionKeyReceived +=
                new EventHandler<RemoteDataEventArgs<string>>(HandleEncryptedSessionKeyReceived);
            SessionDataStructureHandler.PublicKeyRequestReceived +=
                new EventHandler<RemoteDataEventArgs<string>>(HandlePublicKeyRequestReceived);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Create a new ClientMethodExecutor object and then dispatch it.
        /// </summary>
        internal static void Dispatch(
            BaseClientTransportManager transportManager,
            PSHost clientHost,
            PSDataCollectionStream<ErrorRecord> errorStream,
            ObjectStream methodExecutorStream,
            bool isMethodExecutorStreamEnabled,
            RemoteRunspacePoolInternal runspacePool,
            Guid clientPowerShellId,
            RemoteHostCall remoteHostCall)
        {
            ClientMethodExecutor methodExecutor =
                new ClientMethodExecutor(transportManager, clientHost, runspacePool.InstanceId,
                    clientPowerShellId, remoteHostCall);

            // If the powershell id is not specified, this message is for the runspace pool, execute
            // it immediately and return
            if (clientPowerShellId == Guid.Empty)
            {
                methodExecutor.Execute(errorStream);
                return;
            }

            // Check client host to see if SetShouldExit should be allowed
            bool hostAllowSetShouldExit = false;
            if (clientHost != null)
            {
                PSObject hostPrivateData = clientHost.PrivateData as PSObject;
                if (hostPrivateData != null)
                {
                    PSNoteProperty allowSetShouldExit = hostPrivateData.Properties["AllowSetShouldExitFromRemote"] as PSNoteProperty;
                    hostAllowSetShouldExit = (allowSetShouldExit != null && allowSetShouldExit.Value is bool) ?
                        (bool)allowSetShouldExit.Value : false;
                }
            }

            // Should we kill remote runspace? Check if "SetShouldExit" and if we are in the
            // cmdlet case. In the API case (when we are invoked from an API not a cmdlet) we
            // should not interpret "SetShouldExit" but should pass it on to the host. The
            // variable IsMethodExecutorStreamEnabled is only true in the cmdlet case. In the
            // API case it is false.

            if (remoteHostCall.IsSetShouldExit && isMethodExecutorStreamEnabled && !hostAllowSetShouldExit)
            {
                runspacePool.Close();
                return;
            }

            // Cmdlet case: queue up the executor in the pipeline stream.
            if (isMethodExecutorStreamEnabled)
            {
                Dbg.Assert(!isMethodExecutorStreamEnabled ||
                           (isMethodExecutorStreamEnabled && methodExecutorStream != null),
                           "method executor stream can't be null when enabled");
                methodExecutorStream.Write(methodExecutor);
            }

            // API case: execute it immediately.
            else
            {
                methodExecutor.Execute(errorStream);
            }
        }