Exemplo n.º 1
0
 private void AddConnectionRetryHandler(PSInvokeExpressionSyncJob job)
 {
     if (job != null)
     {
         foreach (PowerShell shell in job.GetPowerShells())
         {
             if (shell.RemotePowerShell != null)
             {
                 shell.RemotePowerShell.RCConnectionNotification += new EventHandler<PSConnectionRetryStatusEventArgs>(this.RCConnectionNotificationHandler);
             }
         }
     }
 }
Exemplo n.º 2
0
 private List<PSSession> GetDisconnectedSessions(PSInvokeExpressionSyncJob job)
 {
     List<PSSession> list = new List<PSSession>();
     foreach (PowerShell shell in job.GetPowerShells())
     {
         string cmdStr = ((shell.Commands != null) && (shell.Commands.Commands.Count > 0)) ? shell.Commands.Commands[0].CommandText : string.Empty;
         ConnectCommandInfo info = new ConnectCommandInfo(shell.InstanceId, cmdStr);
         RunspacePool runspacePool = null;
         if (shell.RunspacePool != null)
         {
             runspacePool = shell.RunspacePool;
         }
         else
         {
             object runspaceConnection = shell.GetRunspaceConnection();
             RunspacePool pool2 = runspaceConnection as RunspacePool;
             if (pool2 != null)
             {
                 runspacePool = pool2;
             }
             else
             {
                 RemoteRunspace runspace = runspaceConnection as RemoteRunspace;
                 if (runspace != null)
                 {
                     runspacePool = runspace.RunspacePool;
                 }
             }
         }
         if (runspacePool != null)
         {
             if (runspacePool.RunspacePoolStateInfo.State != RunspacePoolState.Disconnected)
             {
                 if (!base.InvokeAndDisconnect || (runspacePool.RunspacePoolStateInfo.State != RunspacePoolState.Opened))
                 {
                     continue;
                 }
                 runspacePool.Disconnect();
             }
             string name = runspacePool.RemoteRunspacePoolInternal.Name;
             if (string.IsNullOrEmpty(name))
             {
                 int num;
                 name = PSSession.GenerateRunspaceName(out num);
             }
             RunspacePool pool3 = new RunspacePool(true, runspacePool.RemoteRunspacePoolInternal.InstanceId, name, new ConnectCommandInfo[] { info }, runspacePool.RemoteRunspacePoolInternal.ConnectionInfo, base.Host, base.Context.TypeTable);
             RemoteRunspace remoteRunspace = new RemoteRunspace(pool3);
             list.Add(new PSSession(remoteRunspace));
         }
     }
     return list;
 }
Exemplo n.º 3
0
 private void CreateAndRunSyncJob()
 {
     lock (this.jobSyncObject)
     {
         if (!this.nojob)
         {
             this.throttleManager.ThrottleLimit = this.ThrottleLimit;
             this.throttleManager.ThrottleComplete += new EventHandler<EventArgs>(this.HandleThrottleComplete);
             this.operationsComplete.Reset();
             this.disconnectComplete = new ManualResetEvent(false);
             this.job = new PSInvokeExpressionSyncJob(base.Operations, this.throttleManager);
             this.job.HideComputerName = this.hideComputerName;
             this.job.StateChanged += new EventHandler<JobStateEventArgs>(this.HandleJobStateChanged);
             this.AddConnectionRetryHandler(this.job);
             this.job.StartOperations(base.Operations);
         }
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a disconnected session for each disconnected PowerShell object in
        /// PSInvokeExpressionSyncJob.
        /// </summary>
        /// <param name="job"></param>
        /// <returns></returns>
        private List<PSSession> GetDisconnectedSessions(PSInvokeExpressionSyncJob job)
        {
            List<PSSession> discSessions = new List<PSSession>();

            Collection<System.Management.Automation.PowerShell> powershells = job.GetPowerShells();
            foreach (System.Management.Automation.PowerShell ps in powershells)
            {
                // Get the command information from the PowerShell object.
                string commandText = (ps.Commands != null && ps.Commands.Commands.Count > 0) ?
                    ps.Commands.Commands[0].CommandText : string.Empty;
                ConnectCommandInfo cmdInfo = new ConnectCommandInfo(ps.InstanceId, commandText);

                // Get the old RunspacePool object that the command was initially run on.
                RunspacePool oldRunspacePool = null;
                if (ps.RunspacePool != null)
                {
                    oldRunspacePool = ps.RunspacePool;
                }
                else
                {
                    object rsConnection = ps.GetRunspaceConnection();
                    RunspacePool rsPool = rsConnection as RunspacePool;
                    if (rsPool != null)
                    {
                        oldRunspacePool = rsPool;
                    }
                    else
                    {
                        RemoteRunspace remoteRs = rsConnection as RemoteRunspace;
                        if (remoteRs != null)
                        {
                            oldRunspacePool = remoteRs.RunspacePool;
                        }
                    }
                }

                // Create a new disconnected PSSession object and return to the user.
                // The user can use this object to connect to the command on the server
                // and retrieve data.
                if (oldRunspacePool != null)
                {
                    if (oldRunspacePool.RunspacePoolStateInfo.State != RunspacePoolState.Disconnected)
                    {
                        // InvokeAndDisconnect starts the command and immediately disconnects the command,
                        // but we need to disconnect the associated runspace/pool here.
                        if (InvokeAndDisconnect && oldRunspacePool.RunspacePoolStateInfo.State == RunspacePoolState.Opened)
                        {
                            oldRunspacePool.Disconnect();
                        }
                        else
                        {
                            // Skip runspace pools that have not been disconnected.
                            continue;
                        }
                    }

                    // Auto-generate a session name if one was not provided.
                    string sessionName = oldRunspacePool.RemoteRunspacePoolInternal.Name;
                    if (string.IsNullOrEmpty(sessionName))
                    {
                        int id;
                        sessionName = PSSession.GenerateRunspaceName(out id);
                    }

                    RunspacePool runspacePool = new RunspacePool(
                                                        true,
                                                        oldRunspacePool.RemoteRunspacePoolInternal.InstanceId,
                                                        sessionName,
                                                        new ConnectCommandInfo[1] { cmdInfo },
                                                        oldRunspacePool.RemoteRunspacePoolInternal.ConnectionInfo,
                                                        this.Host,
                                                        this.Context.TypeTable);
                    runspacePool.RemoteRunspacePoolInternal.IsRemoteDebugStop = oldRunspacePool.RemoteRunspacePoolInternal.IsRemoteDebugStop;

                    RemoteRunspace remoteRunspace = new RemoteRunspace(runspacePool);
                    discSessions.Add(new PSSession(remoteRunspace));
                }
            }

            return discSessions;
        }
Exemplo n.º 5
0
        private void RemoveConnectionRetryHandler(PSInvokeExpressionSyncJob job)
        {
            // Ensure progress bar is removed.
            StopProgressBar(0);

            if (job == null)
            {
                return;
            }
            Collection<System.Management.Automation.PowerShell> powershells = job.GetPowerShells();
            foreach (var ps in powershells)
            {
                if (ps.RemotePowerShell != null)
                {
                    ps.RemotePowerShell.RCConnectionNotification -=
                        new EventHandler<PSConnectionRetryStatusEventArgs>(RCConnectionNotificationHandler);
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Sets the throttle limit, creates the invoke expression
        /// sync job and executes the same
        /// </summary>
        private void CreateAndRunSyncJob()
        {
            lock (_jobSyncObject)
            {
                if (!_nojob)
                {
                    _throttleManager.ThrottleLimit = ThrottleLimit;
                    _throttleManager.ThrottleComplete += new EventHandler<EventArgs>(HandleThrottleComplete);

                    _operationsComplete.Reset();
                    Dbg.Assert(_disconnectComplete == null, "disconnectComplete event should only be used once.");
                    _disconnectComplete = new ManualResetEvent(false);
                    _job = new PSInvokeExpressionSyncJob(Operations, _throttleManager);
                    _job.HideComputerName = _hideComputerName;
                    _job.StateChanged += new EventHandler<JobStateEventArgs>(HandleJobStateChanged);

                    // Add robust connection retry notification handler.
                    AddConnectionRetryHandler(_job);

                    _job.StartOperations(Operations);
                }
            }
        }