private void RemovePSEditFunction(RunspaceDetails runspaceDetails)
        {
            if (runspaceDetails.Location == RunspaceLocation.Remote &&
                runspaceDetails.Context == RunspaceContext.Original)
            {
                try
                {
                    if (runspaceDetails.Runspace.Events != null)
                    {
                        runspaceDetails.Runspace.Events.ReceivedEvents.PSEventReceived -= HandlePSEventReceived;
                    }

                    if (runspaceDetails.Runspace.RunspaceStateInfo.State == RunspaceState.Opened)
                    {
                        using (var powerShell = System.Management.Automation.PowerShell.Create())
                        {
                            powerShell.Runspace = runspaceDetails.Runspace;
                            powerShell.Commands.AddScript(RemovePSEditFunctionScript);
                            powerShell.Invoke();
                        }
                    }
                }
                catch (Exception e) when(e is RemoteException || e is PSInvalidOperationException)
                {
                    this.logger.WriteException("Could not remove psedit function.", e);
                }
            }
        }
 public RemotePathMappings(
     RunspaceDetails runspaceDetails,
     RemoteFileManager remoteFileManager)
 {
     this.runspaceDetails   = runspaceDetails;
     this.remoteFileManager = remoteFileManager;
 }
示例#3
0
        private void RegisterPSEditFunction(RunspaceDetails runspaceDetails)
        {
            if (runspaceDetails.Location == RunspaceLocation.Remote &&
                runspaceDetails.Context == RunspaceContext.Original)
            {
                try
                {
                    runspaceDetails.Runspace.Events.ReceivedEvents.PSEventReceived += HandlePSEventReceived;

                    PSCommand createCommand = new PSCommand();
                    createCommand
                    .AddScript(CreatePSEditFunctionScript)
                    .AddParameter("PSEditModule", PSEditModule);

                    if (runspaceDetails.Context == RunspaceContext.DebuggedRunspace)
                    {
                        this.powerShellContext.ExecuteCommand(createCommand).Wait();
                    }
                    else
                    {
                        using (var powerShell = System.Management.Automation.PowerShell.Create())
                        {
                            powerShell.Runspace = runspaceDetails.Runspace;
                            powerShell.Commands = createCommand;
                            powerShell.Invoke();
                        }
                    }
                }
                catch (RemoteException e)
                {
                    this.logger.WriteException("Could not create psedit function.", e);
                }
            }
        }
        /// <summary>
        /// For a remote or local cache path, get the corresponding local or
        /// remote file path.
        /// </summary>
        /// <param name="filePath">
        /// The remote or local file path.
        /// </param>
        /// <param name="runspaceDetails">
        /// The runspace from which the remote file was fetched.
        /// </param>
        /// <returns>The mapped file path.</returns>
        public string GetMappedPath(
            string filePath,
            RunspaceDetails runspaceDetails)
        {
            RemotePathMappings remotePathMappings = this.GetPathMappings(runspaceDetails);

            return(remotePathMappings.GetMappedPath(filePath));
        }
示例#5
0
        /// <summary>
        /// Creates a new instance of the RunspaceChangedEventArgs class.
        /// </summary>
        /// <param name="changeAction">The action which caused the runspace to change.</param>
        /// <param name="previousRunspace">The previously active runspace.</param>
        /// <param name="newRunspace">The newly active runspace.</param>
        public RunspaceChangedEventArgs(
            RunspaceChangeAction changeAction,
            RunspaceDetails previousRunspace,
            RunspaceDetails newRunspace)
        {
            Validate.IsNotNull(nameof(previousRunspace), previousRunspace);

            this.ChangeAction     = changeAction;
            this.PreviousRunspace = previousRunspace;
            this.NewRunspace      = newRunspace;
        }
        private RemotePathMappings GetPathMappings(RunspaceDetails runspaceDetails)
        {
            RemotePathMappings remotePathMappings = null;

            if (!this.filesPerRunspace.TryGetValue(runspaceDetails, out remotePathMappings))
            {
                remotePathMappings = new RemotePathMappings(runspaceDetails, this);
                this.filesPerRunspace.Add(runspaceDetails, remotePathMappings);
            }

            return(remotePathMappings);
        }
        /// <summary>
        /// Opens a remote file, fetching its contents if necessary.
        /// </summary>
        /// <param name="remoteFilePath">
        /// The remote file path to be opened.
        /// </param>
        /// <param name="runspaceDetails">
        /// The runspace from which where the remote file will be fetched.
        /// </param>
        /// <returns>
        /// The local file path where the remote file's contents have been stored.
        /// </returns>
        public async Task <string> FetchRemoteFile(
            string remoteFilePath,
            RunspaceDetails runspaceDetails)
        {
            string localFilePath = null;

            if (!string.IsNullOrEmpty(remoteFilePath))
            {
                try
                {
                    RemotePathMappings pathMappings = this.GetPathMappings(runspaceDetails);
                    localFilePath = this.GetMappedPath(remoteFilePath, runspaceDetails);

                    if (!pathMappings.IsRemotePathOpened(remoteFilePath))
                    {
                        // Does the local file already exist?
                        if (!File.Exists(localFilePath))
                        {
                            // Load the file contents from the remote machine and create the buffer
                            PSCommand command = new PSCommand();
                            command.AddCommand("Microsoft.PowerShell.Management\\Get-Content");
                            command.AddParameter("Path", remoteFilePath);
                            command.AddParameter("Raw");
                            command.AddParameter("Encoding", "Byte");

                            byte[] fileContent =
                                (await this.powerShellContext.ExecuteCommand <byte[]>(command, false, false))
                                .FirstOrDefault();

                            if (fileContent != null)
                            {
                                this.StoreRemoteFile(localFilePath, fileContent, pathMappings);
                            }
                            else
                            {
                                this.logger.Write(
                                    LogLevel.Warning,
                                    $"Could not load contents of remote file '{remoteFilePath}'");
                            }
                        }
                    }
                }
                catch (IOException e)
                {
                    this.logger.Write(
                        LogLevel.Error,
                        $"Caught {e.GetType().Name} while attempting to get remote file at path '{remoteFilePath}'\r\n\r\n{e.ToString()}");
                }
            }

            return(localFilePath);
        }
        private RemotePathMappings GetPathMappings(RunspaceDetails runspaceDetails)
        {
            RemotePathMappings remotePathMappings = null;
            string             computerName       = runspaceDetails.SessionDetails.ComputerName;

            if (!this.filesPerComputer.TryGetValue(computerName, out remotePathMappings))
            {
                remotePathMappings = new RemotePathMappings(runspaceDetails, this);
                this.filesPerComputer.Add(computerName, remotePathMappings);
            }

            return(remotePathMappings);
        }
示例#9
0
 /// <summary>
 /// Creates a clone of the given runspace through which another
 /// runspace was attached.  Sets the IsAttached property of the
 /// resulting RunspaceDetails object to true.
 /// </summary>
 /// <param name="runspaceDetails">
 /// The RunspaceDetails object which the new object based.
 /// </param>
 /// <param name="runspaceContext">
 /// The RunspaceContext of the runspace.
 /// </param>
 /// <param name="sessionDetails">
 /// The SessionDetails for the runspace.
 /// </param>
 /// <returns>
 /// A new RunspaceDetails instance for the attached runspace.
 /// </returns>
 public static RunspaceDetails CreateFromContext(
     RunspaceDetails runspaceDetails,
     RunspaceContext runspaceContext,
     SessionDetails sessionDetails)
 {
     return
         (new RunspaceDetails(
              runspaceDetails.Runspace,
              sessionDetails,
              runspaceDetails.PowerShellVersion,
              runspaceDetails.Location,
              runspaceContext,
              runspaceDetails.ConnectionString));
 }
        private string StoreRemoteFile(
            string remoteFilePath,
            byte[] fileContent,
            RunspaceDetails runspaceDetails)
        {
            RemotePathMappings pathMappings  = this.GetPathMappings(runspaceDetails);
            string             localFilePath = pathMappings.GetMappedPath(remoteFilePath);

            this.StoreRemoteFile(
                localFilePath,
                fileContent,
                pathMappings);

            return(localFilePath);
        }
示例#11
0
 /// <summary>
 /// Creates a new RunspaceDetails object from a remote
 /// debugging session.
 /// </summary>
 /// <param name="runspaceDetails">
 /// The RunspaceDetails object which the new object based.
 /// </param>
 /// <param name="runspaceLocation">
 /// The RunspaceLocation of the runspace.
 /// </param>
 /// <param name="runspaceContext">
 /// The RunspaceContext of the runspace.
 /// </param>
 /// <param name="sessionDetails">
 /// The SessionDetails for the runspace.
 /// </param>
 /// <returns>
 /// A new RunspaceDetails instance for the attached runspace.
 /// </returns>
 public static RunspaceDetails CreateFromDebugger(
     RunspaceDetails runspaceDetails,
     RunspaceLocation runspaceLocation,
     RunspaceContext runspaceContext,
     SessionDetails sessionDetails)
 {
     // TODO: Get the PowerShellVersion correctly!
     return
         (new RunspaceDetails(
              runspaceDetails.Runspace,
              sessionDetails,
              runspaceDetails.PowerShellVersion,
              runspaceLocation,
              runspaceContext,
              runspaceDetails.ConnectionString));
 }
示例#12
0
        /// <summary>
        /// Creates a clone of the given runspace through which another
        /// runspace was attached.  Sets the IsAttached property of the
        /// resulting RunspaceDetails object to true.
        /// </summary>
        /// <param name="runspaceDetails">
        /// The RunspaceDetails object which the new object based.
        /// </param>
        /// <param name="attachedRunspaceId">
        /// The id of the runspace that has been attached.
        /// </param>
        /// <returns>
        /// A new RunspaceDetails instance for the attached runspace.
        /// </returns>
        public static RunspaceDetails CreateAttached(
            RunspaceDetails runspaceDetails,
            Guid attachedRunspaceId)
        {
            RunspaceDetails newRunspace =
                new RunspaceDetails(
                    attachedRunspaceId,
                    runspaceDetails.Runspace,
                    runspaceDetails.PowerShellVersion,
                    runspaceDetails.Location,
                    runspaceDetails.ConnectionString);

            // Since this is an attached runspace, set the IsAttached
            // property and carry forward the ID of the attached runspace
            newRunspace.IsAttached = true;
            newRunspace.Id         = attachedRunspaceId;

            return(newRunspace);
        }
        /// <summary>
        /// Creates a temporary file with the given name and contents
        /// corresponding to the specified runspace.
        /// </summary>
        /// <param name="fileName">
        /// The name of the file to be created under the session path.
        /// </param>
        /// <param name="fileContents">
        /// The contents of the file to be created.
        /// </param>
        /// <param name="runspaceDetails">
        /// The runspace for which the temporary file relates.
        /// </param>
        /// <returns>The full temporary path of the file if successful, null otherwise.</returns>
        public string CreateTemporaryFile(string fileName, string fileContents, RunspaceDetails runspaceDetails)
        {
            string temporaryFilePath = Path.Combine(this.processTempPath, fileName);

            try
            {
                File.WriteAllText(temporaryFilePath, fileContents);

                RemotePathMappings pathMappings = this.GetPathMappings(runspaceDetails);
                pathMappings.AddOpenedLocalPath(temporaryFilePath);
            }
            catch (IOException e)
            {
                this.logger.Write(
                    LogLevel.Error,
                    $"Caught {e.GetType().Name} while attempting to write temporary file at path '{temporaryFilePath}'\r\n\r\n{e.ToString()}");

                temporaryFilePath = null;
            }

            return(temporaryFilePath);
        }
        private void RemovePSEditFunction(RunspaceDetails runspaceDetails)
        {
            try
            {
                if (runspaceDetails.Runspace.Events != null)
                {
                    runspaceDetails.Runspace.Events.ReceivedEvents.PSEventReceived -= HandlePSEventReceived;
                }

                if (runspaceDetails.Runspace.RunspaceStateInfo.State == RunspaceState.Opened)
                {
                    using (var powerShell = System.Management.Automation.PowerShell.Create())
                    {
                        powerShell.Runspace = runspaceDetails.Runspace;
                        powerShell.Commands.AddScript(RemovePSEditFunctionScript);
                        powerShell.Invoke();
                    }
                }
            }
            catch (RemoteException e)
            {
                Logger.WriteException("Could not remove psedit function.", e);
            }
        }
        private void RegisterPSEditFunction(RunspaceDetails runspaceDetails)
        {
            try
            {
                runspaceDetails.Runspace.Events.ReceivedEvents.PSEventReceived += HandlePSEventReceived;

                var createScript =
                    string.Format(
                        CreatePSEditFunctionScript,
                        (runspaceDetails.Location == RunspaceLocation.Local && !runspaceDetails.IsAttached)
                            ? string.Empty : "-Forward");

                PSCommand createCommand = new PSCommand();
                createCommand
                .AddScript(createScript)
                .AddParameter("PSEditFunction", PSEditFunctionScript);

                if (runspaceDetails.IsAttached)
                {
                    this.powerShellContext.ExecuteCommand(createCommand).Wait();
                }
                else
                {
                    using (var powerShell = System.Management.Automation.PowerShell.Create())
                    {
                        powerShell.Runspace = runspaceDetails.Runspace;
                        powerShell.Commands = createCommand;
                        powerShell.Invoke();
                    }
                }
            }
            catch (RemoteException e)
            {
                Logger.WriteException("Could not create psedit function.", e);
            }
        }