示例#1
0
        internal void Override(RemoteRunspace remoteRunspace, object syncObject, out bool isRunspacePushed)
        {
            lock (this._localSyncObject)
            {
                this._stopInvoke = false;
            }
            try
            {
                if (syncObject != null)
                {
                    lock (syncObject)
                    {
                        this._runspaceRef.Override(remoteRunspace);
                        isRunspacePushed = true;
                        goto Label_0063;
                    }
                }
                this._runspaceRef.Override(remoteRunspace);
                isRunspacePushed = true;
Label_0063:
                using (PowerShell shell = PowerShell.Create())
                {
                    shell.AddCommand("Get-Command");
                    shell.AddParameter("Name", new string[] { "Out-Default", "Exit-PSSession" });
                    shell.Runspace = this._runspaceRef.Value;
                    bool flag2 = this._runspaceRef.Value.GetRemoteProtocolVersion() == RemotingConstants.ProtocolVersionWin7RC;
                    shell.IsGetCommandMetadataSpecialPipeline = !flag2;
                    int num = flag2 ? 2 : 3;
                    shell.RemotePowerShell.HostCallReceived += new EventHandler <RemoteDataEventArgs <RemoteHostCall> >(this.HandleHostCall);
                    IAsyncResult asyncResult          = shell.BeginInvoke();
                    PSDataCollection <PSObject> datas = new PSDataCollection <PSObject>();
                    while (!this._stopInvoke)
                    {
                        asyncResult.AsyncWaitHandle.WaitOne(0x3e8);
                        if (asyncResult.IsCompleted)
                        {
                            datas = shell.EndInvoke(asyncResult);
                            break;
                        }
                    }
                    if ((shell.Streams.Error.Count > 0) || (datas.Count < num))
                    {
                        throw RemoteHostExceptions.NewRemoteRunspaceDoesNotSupportPushRunspaceException();
                    }
                    return;
                }
            }
            catch (Exception)
            {
                this._runspaceRef.Revert();
                isRunspacePushed = false;
                throw;
            }
        }
示例#2
0
        /// <summary>
        /// Override inside a safe lock.
        /// </summary>
        /// <param name="remoteRunspace">Runspace to override.</param>
        /// <param name="syncObject">Object to use in synchronization.</param>
        /// <param name="isRunspacePushed">Set is runspace pushed.</param>
        internal void Override(RemoteRunspace remoteRunspace, object syncObject, out bool isRunspacePushed)
        {
            lock (_localSyncObject)
            {
                _stopInvoke = false;
            }

            try
            {
                if (syncObject != null)
                {
                    lock (syncObject)
                    {
                        _runspaceRef.Override(remoteRunspace);
                        isRunspacePushed = true;
                    }
                }
                else
                {
                    _runspaceRef.Override(remoteRunspace);
                    isRunspacePushed = true;
                }

                if ((remoteRunspace.GetCurrentlyRunningPipeline() != null))
                {
                    // Don't execute command if pushed runspace is already running one.
                    return;
                }

                using (PowerShell powerShell = PowerShell.Create())
                {
                    powerShell.AddCommand("Get-Command");
                    powerShell.AddParameter("Name", new string[] { "Out-Default", "Exit-PSSession" });
                    powerShell.Runspace = _runspaceRef.Value;

                    bool isReleaseCandidateBackcompatibilityMode = _runspaceRef.Value.GetRemoteProtocolVersion() == RemotingConstants.ProtocolVersionWin7RC;
                    powerShell.IsGetCommandMetadataSpecialPipeline = !isReleaseCandidateBackcompatibilityMode;
                    int expectedNumberOfResults = isReleaseCandidateBackcompatibilityMode ? 2 : 3;

                    powerShell.RemotePowerShell.HostCallReceived += HandleHostCall;

                    IAsyncResult asyncResult            = powerShell.BeginInvoke();
                    PSDataCollection <PSObject> results = new PSDataCollection <PSObject>();

                    while (!_stopInvoke)
                    {
                        asyncResult.AsyncWaitHandle.WaitOne(1000);

                        if (asyncResult.IsCompleted)
                        {
                            results = powerShell.EndInvoke(asyncResult);
                            break;
                        }
                    }

                    if (powerShell.Streams.Error.Count > 0 || results.Count < expectedNumberOfResults)
                    {
                        throw RemoteHostExceptions.NewRemoteRunspaceDoesNotSupportPushRunspaceException();
                    }
                }
            }
            catch (Exception)
            {
                _runspaceRef.Revert();
                isRunspacePushed = false;
                throw;
            }
        }