Exemple #1
0
        /// <summary>
        /// Creates and populates a new RunspaceDetails instance for the given runspace.
        /// </summary>
        /// <param name="runspace">
        /// The runspace for which details will be gathered.
        /// </param>
        /// <param name="sessionDetails">
        /// The SessionDetails for the runspace.
        /// </param>
        /// <param name="logger">An ILogger implementation used for writing log messages.</param>
        /// <returns>A new RunspaceDetails instance.</returns>
        internal static RunspaceDetails CreateFromRunspace(
            Runspace runspace,
            SessionDetails sessionDetails,
            ILogger logger)
        {
            Validate.IsNotNull(nameof(runspace), runspace);
            Validate.IsNotNull(nameof(sessionDetails), sessionDetails);

            var runspaceLocation = RunspaceLocation.Local;
            var runspaceContext  = RunspaceContext.Original;
            var versionDetails   = PowerShellVersionDetails.GetVersionDetails(runspace, logger);

            string connectionString = null;

            if (runspace.ConnectionInfo != null)
            {
                // Use 'dynamic' to avoid missing NamedPipeRunspaceConnectionInfo
                // on PS v3 and v4
                try
                {
                    dynamic connectionInfo = runspace.ConnectionInfo;
                    if (connectionInfo.ProcessId != null)
                    {
                        connectionString = connectionInfo.ProcessId.ToString();
                        runspaceContext  = RunspaceContext.EnteredProcess;
                    }
                }
                catch (RuntimeBinderException)
                {
                    // ProcessId property isn't on the object, move on.
                }

                // Grab the $host.name which will tell us if we're in a PSRP session or not
                string hostName =
                    PowerShellContext.ExecuteScriptAndGetItem <string>(
                        "$Host.Name",
                        runspace,
                        defaultValue: string.Empty);

                // hostname is 'ServerRemoteHost' when the user enters a session.
                // ex. Enter-PSSession, Enter-PSHostProcess
                if (hostName.Equals("ServerRemoteHost", StringComparison.Ordinal))
                {
                    runspaceLocation = RunspaceLocation.Remote;
                    connectionString =
                        runspace.ConnectionInfo.ComputerName +
                        (connectionString != null ? $"-{connectionString}" : string.Empty);
                }
            }

            return
                (new RunspaceDetails(
                     runspace,
                     sessionDetails,
                     versionDetails,
                     runspaceLocation,
                     runspaceContext,
                     connectionString));
        }
        /// <summary>
        /// Creates and populates a new RunspaceDetails instance for the given runspace.
        /// </summary>
        /// <param name="runspace">
        /// The runspace for which details will be gathered.
        /// </param>
        /// <param name="sessionDetails">
        /// The SessionDetails for the runspace.
        /// </param>
        /// <param name="logger">An ILogger implementation used for writing log messages.</param>
        /// <returns>A new RunspaceDetails instance.</returns>
        internal static RunspaceDetails CreateFromRunspace(
            Runspace runspace,
            SessionDetails sessionDetails,
            ILogger logger)
        {
            Validate.IsNotNull(nameof(runspace), runspace);
            Validate.IsNotNull(nameof(sessionDetails), sessionDetails);

            var runspaceId       = runspace.InstanceId;
            var runspaceLocation = RunspaceLocation.Local;
            var runspaceContext  = RunspaceContext.Original;
            var versionDetails   = PowerShellVersionDetails.GetVersionDetails(runspace, logger);

            string connectionString = null;

            if (runspace.ConnectionInfo != null)
            {
                // Use 'dynamic' to avoid missing NamedPipeRunspaceConnectionInfo
                // on PS v3 and v4
                try
                {
                    dynamic connectionInfo = runspace.ConnectionInfo;
                    if (connectionInfo.ProcessId != null)
                    {
                        connectionString = connectionInfo.ProcessId.ToString();
                        runspaceContext  = RunspaceContext.EnteredProcess;
                    }
                }
                catch (RuntimeBinderException)
                {
                    // ProcessId property isn't on the object, move on.
                }

                if (runspace.ConnectionInfo.ComputerName != "localhost")
                {
                    runspaceId =
                        PowerShellContext.ExecuteScriptAndGetItem <Guid>(
                            "$host.Runspace.InstanceId",
                            runspace);

                    runspaceLocation = RunspaceLocation.Remote;
                    connectionString =
                        runspace.ConnectionInfo.ComputerName +
                        (connectionString != null ? $"-{connectionString}" : string.Empty);
                }
            }

            return
                (new RunspaceDetails(
                     runspace,
                     sessionDetails,
                     versionDetails,
                     runspaceLocation,
                     runspaceContext,
                     connectionString));
        }
Exemple #3
0
 /// <summary>
 /// Creates a new instance of the RunspaceDetails class.
 /// </summary>
 /// <param name="instanceId">
 /// The InstanceId Guid for the runspace.
 /// </param>
 /// <param name="runspace">
 /// The runspace for which this instance contains details.
 /// </param>
 /// <param name="powerShellVersion">
 /// The PowerShellVersionDetails of the runspace.
 /// </param>
 /// <param name="runspaceLocation">
 /// The RunspaceLocale of the runspace.
 /// </param>
 /// <param name="connectionString">
 /// The connection string of the runspace.
 /// </param>
 public RunspaceDetails(
     Guid instanceId,
     Runspace runspace,
     PowerShellVersionDetails powerShellVersion,
     RunspaceLocation runspaceLocation,
     string connectionString)
 {
     this.Id                = instanceId;
     this.Runspace          = runspace;
     this.PowerShellVersion = powerShellVersion;
     this.Location          = runspaceLocation;
     this.ConnectionString  = connectionString;
 }
Exemple #4
0
 /// <summary>
 /// Creates a new instance of the RunspaceDetails class.
 /// </summary>
 /// <param name="runspace">
 /// The runspace for which this instance contains details.
 /// </param>
 /// <param name="powerShellVersion">
 /// The PowerShellVersionDetails of the runspace.
 /// </param>
 /// <param name="runspaceLocation">
 /// The RunspaceLocale of the runspace.
 /// </param>
 /// <param name="connectionString">
 /// The connection string of the runspace.
 /// </param>
 public RunspaceDetails(
     Runspace runspace,
     PowerShellVersionDetails powerShellVersion,
     RunspaceLocation runspaceLocation,
     string connectionString)
     : this(
         runspace.InstanceId,
         runspace,
         powerShellVersion,
         runspaceLocation,
         connectionString)
 {
 }
Exemple #5
0
 /// <summary>
 /// Creates a new instance of the RunspaceDetails class.
 /// </summary>
 /// <param name="runspace">
 /// The runspace for which this instance contains details.
 /// </param>
 /// <param name="sessionDetails">
 /// The SessionDetails for the runspace.
 /// </param>
 /// <param name="powerShellVersion">
 /// The PowerShellVersionDetails of the runspace.
 /// </param>
 /// <param name="runspaceLocation">
 /// The RunspaceLocation of the runspace.
 /// </param>
 /// <param name="runspaceContext">
 /// The RunspaceContext of the runspace.
 /// </param>
 /// <param name="connectionString">
 /// The connection string of the runspace.
 /// </param>
 public RunspaceDetails(
     Runspace runspace,
     SessionDetails sessionDetails,
     PowerShellVersionDetails powerShellVersion,
     RunspaceLocation runspaceLocation,
     RunspaceContext runspaceContext,
     string connectionString)
 {
     this.Runspace          = runspace;
     this.SessionDetails    = sessionDetails;
     this.PowerShellVersion = powerShellVersion;
     this.Location          = runspaceLocation;
     this.Context           = runspaceContext;
     this.ConnectionString  = connectionString;
 }