// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Management.Automation.Host; using System.Management.Automation.Tracing; using Microsoft.PowerShell; using Microsoft.PowerShell.Commands; namespace System.Management.Automation.Runspaces { /// /// Defines a factory class for creating Runspace objects. /// public static class RunspaceFactory { /// /// Static constructor. /// static RunspaceFactory() { // Set ETW activity Id Guid activityId = EtwActivity.GetActivityId(); if (activityId == Guid.Empty) { EtwActivity.SetActivityId(EtwActivity.CreateActivityId()); } } #region Runspace Factory /// /// Creates a runspace using host of type . /// /// /// A runspace object. /// public static Runspace CreateRunspace() { PSHost host = new DefaultHost(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture); return CreateRunspace(host); } /// /// Creates a runspace using specified host. This runspace is created using the /// configuration information from EntryAssembly. /// /// /// The explicit PSHost implementation. /// /// /// A runspace object /// /// /// Thrown when host is null. /// public static Runspace CreateRunspace(PSHost host) { if (host == null) { throw PSTraceSource.NewArgumentNullException(nameof(host)); } return new LocalRunspace(host, InitialSessionState.CreateDefault()); } /// /// Creates a runspace using /// /// /// InitialSessionState information for the runspace. /// /// /// A runspace object /// /// /// Thrown when initialSessionState is null /// [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Runspace")] public static Runspace CreateRunspace(InitialSessionState initialSessionState) { if (initialSessionState == null) { throw PSTraceSource.NewArgumentNullException(nameof(initialSessionState)); } PSHost host = new DefaultHost(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture); return CreateRunspace(host, initialSessionState); } /// /// Creates a runspace using specified PSHost and InitialSessionState. /// /// /// Host implementation for runspace. /// /// /// InitialSessionState information for the runspace. /// /// /// A runspace object /// /// /// Thrown when host is null /// /// /// Thrown when initialSessionState is null /// [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Runspace")] public static Runspace CreateRunspace(PSHost host, InitialSessionState initialSessionState) { if (host == null) { throw PSTraceSource.NewArgumentNullException(nameof(host)); } if (initialSessionState == null) { throw PSTraceSource.NewArgumentNullException(nameof(initialSessionState)); } return new LocalRunspace(host, initialSessionState); } /// /// Creates a runspace using specified PSHost and InitialSessionState. /// /// /// Host implementation for runspace. /// /// /// InitialSessionState information for the runspace. /// /// /// A runspace object /// /// /// Thrown when host is null /// /// /// Thrown when initialSessionState is null /// [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Runspace")] internal static Runspace CreateRunspaceFromSessionStateNoClone(PSHost host, InitialSessionState initialSessionState) { if (host == null) { throw PSTraceSource.NewArgumentNullException(nameof(host)); } if (initialSessionState == null) { throw PSTraceSource.NewArgumentNullException(nameof(initialSessionState)); } return new LocalRunspace(host, initialSessionState, true); } #endregion #region RunspacePool Factory /// /// Creates a RunspacePool with MaxRunspaces 1 and MinRunspaces 1. /// public static RunspacePool CreateRunspacePool() { return CreateRunspacePool(1, 1); } /// /// Creates a RunspacePool /// /// limits the number of Runspaces that can exist in this /// pool. The minimum pool size is set to . /// /// /// The minimum number of Runspaces that exist in this /// pool. Should be greater than or equal to 1. /// /// /// The maximum number of Runspaces that can exist in this /// pool. Should be greater than or equal to 1. /// /// /// Maximum runspaces is less than 1. /// Minimum runspaces is less than 1. /// public static RunspacePool CreateRunspacePool(int minRunspaces, int maxRunspaces) { return CreateRunspacePool(minRunspaces, maxRunspaces, new DefaultHost ( CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture )); } /// /// Creates a RunspacePool using the supplied . /// The minimum runspaces size is set to 1. The maximum runspaces size is /// set to 1. /// /// /// initialSessionState to use when creating a new /// Runspace in the pool. /// /// /// InitialSessionState is null. /// [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Runspace")] public static RunspacePool CreateRunspacePool(InitialSessionState initialSessionState) { return CreateRunspacePool(1, 1, initialSessionState, new DefaultHost ( CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture )); } /// /// Creates a RunspacePool using the supplied , /// and /// /// /// The minimum number of Runspaces that can exist in this pool. /// Should be greater than or equal to 1. /// /// /// The maximum number of Runspaces that can exist in this pool. /// Should be greater than or equal to 1. /// /// /// The explicit PSHost implementation. /// /// /// is null. /// /// /// A local runspacepool instance. /// [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Runspaces")] public static RunspacePool CreateRunspacePool(int minRunspaces, int maxRunspaces, PSHost host) { return new RunspacePool(minRunspaces, maxRunspaces, host); } /// /// Creates a RunspacePool using the supplied , /// and /// /// /// The minimum number of Runspaces that can exist in this pool. /// Should be greater than or equal to 1. /// /// /// The maximum number of Runspaces that can exist in this pool. /// Should be greater than or equal to 1. /// /// /// initialSessionState to use when creating a new Runspace in the /// pool. /// /// /// InitialSessionState is null. /// /// /// The explicit PSHost implementation. /// /// /// is null. /// is null. /// /// /// Maximum runspaces is less than 1. /// Minimum runspaces is less than 1. /// [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Runspace")] [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Runspaces")] public static RunspacePool CreateRunspacePool(int minRunspaces, int maxRunspaces, InitialSessionState initialSessionState, PSHost host) { return new RunspacePool(minRunspaces, maxRunspaces, initialSessionState, host); } #endregion #region RunspacePool - remote Factory /// /// Creates a RunspacePool /// on the specified remote computer. /// /// limits the number of Runspaces that can exist in this /// pool. The minimum pool size is set to /// . /// /// /// The minimum number of Runspace that should exist in this /// pool. Should be greater than 1. /// /// /// The maximum number of Runspaces that can exist in this /// pool. Should be greater than or equal to 1. /// /// RunspaceConnectionInfo object describing /// the remote computer on which this runspace pool needs to be /// created /// /// Maximum Pool size is less than 1. /// Minimum Pool size is less than 1. /// /// /// connectionInfo is null [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Runspaces")] public static RunspacePool CreateRunspacePool(int minRunspaces, int maxRunspaces, RunspaceConnectionInfo connectionInfo) { return CreateRunspacePool(minRunspaces, maxRunspaces, connectionInfo, null); } /// /// Creates a RunspacePool /// on the specified remote runspace computer. /// /// limits the number of Runspaces that can exist in this /// pool. The minimum pool size is set to /// . /// /// /// The minimum number of Runspace that should exist in this /// pool. Should be greater than 1. /// /// /// The maximum number of Runspaces that can exist in this /// pool. Should be greater than or equal to 1. /// /// Host associated with this /// runspace pool /// RunspaceConnectionInfo object describing /// the remote computer on which this runspace pool needs to be /// created /// /// Maximum Pool size is less than 1. /// Minimum Pool size is less than 1. /// /// /// connectionInfo is null [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Runspaces")] public static RunspacePool CreateRunspacePool(int minRunspaces, int maxRunspaces, RunspaceConnectionInfo connectionInfo, PSHost host) { return CreateRunspacePool(minRunspaces, maxRunspaces, connectionInfo, host, null); } /// /// Creates a RunspacePool /// on the specified remote runspace computer. /// /// limits the number of Runspaces that can exist in this /// pool. The minimum pool size is set to /// . /// /// /// The minimum number of Runspace that should exist in this /// pool. Should be greater than 1. /// /// /// The maximum number of Runspaces that can exist in this /// pool. Should be greater than or equal to 1. /// /// /// The TypeTable to use while deserializing/serializing remote objects. /// TypeTable has the following information used by serializer: /// 1. SerializationMethod /// 2. SerializationDepth /// 3. SpecificSerializationProperties /// TypeTable has the following information used by deserializer: /// 1. TargetTypeForDeserialization /// 2. TypeConverter /// /// If is null no custom serialization/deserialization /// can be done. Default PowerShell behavior will be used in this case. /// /// Host associated with this /// runspace pool /// RunspaceConnectionInfo object describing /// the remote computer on which this runspace pool needs to be /// created /// /// Maximum Pool size is less than 1. /// Minimum Pool size is less than 1. /// /// /// connectionInfo is null [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Runspaces")] public static RunspacePool CreateRunspacePool(int minRunspaces, int maxRunspaces, RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable) { return CreateRunspacePool(minRunspaces, maxRunspaces, connectionInfo, host, typeTable, null); } /// /// Creates a RunspacePool /// on the specified remote runspace computer. /// /// limits the number of Runspaces that can exist in this /// pool. The minimum pool size is set to /// . /// /// /// The minimum number of Runspace that should exist in this /// pool. Should be greater than 1. /// /// /// The maximum number of Runspaces that can exist in this /// pool. Should be greater than or equal to 1. /// /// /// The TypeTable to use while deserializing/serializing remote objects. /// TypeTable has the following information used by serializer: /// 1. SerializationMethod /// 2. SerializationDepth /// 3. SpecificSerializationProperties /// TypeTable has the following information used by deserializer: /// 1. TargetTypeForDeserialization /// 2. TypeConverter /// /// If is null no custom serialization/deserialization /// can be done. Default PowerShell behavior will be used in this case. /// /// Host associated with this /// runspace pool /// /// Application arguments the server can see in /// /// RunspaceConnectionInfo object describing /// the remote computer on which this runspace pool needs to be /// created /// /// Maximum Pool size is less than 1. /// Minimum Pool size is less than 1. /// /// /// connectionInfo is null [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Runspaces")] public static RunspacePool CreateRunspacePool(int minRunspaces, int maxRunspaces, RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable, PSPrimitiveDictionary applicationArguments) { if (connectionInfo is not WSManConnectionInfo && connectionInfo is not NewProcessConnectionInfo && connectionInfo is not NamedPipeConnectionInfo && connectionInfo is not VMConnectionInfo && connectionInfo is not ContainerConnectionInfo) { throw new NotSupportedException(); } if (connectionInfo is WSManConnectionInfo) { RemotingCommandUtil.CheckHostRemotingPrerequisites(); } return new RunspacePool(minRunspaces, maxRunspaces, typeTable, host, applicationArguments, connectionInfo); } #endregion RunspacePool - remote Factory #region Runspace - Remote Factory /// /// Creates a remote Runspace. /// /// It defines connection path to a remote runspace that needs to be created. /// The explicit PSHost implementation. /// /// The TypeTable to use while deserializing/serializing remote objects. /// TypeTable has the following information used by serializer: /// 1. SerializationMethod /// 2. SerializationDepth /// 3. SpecificSerializationProperties /// /// TypeTable has the following information used by deserializer: /// 1. TargetTypeForDeserialization /// 2. TypeConverter /// /// A remote Runspace. public static Runspace CreateRunspace(RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable) { return CreateRunspace(connectionInfo, host, typeTable, null, null); } /// /// Creates a remote Runspace. /// /// It defines connection path to a remote runspace that needs to be created. /// The explicit PSHost implementation. /// /// The TypeTable to use while deserializing/serializing remote objects. /// TypeTable has the following information used by serializer: /// 1. SerializationMethod /// 2. SerializationDepth /// 3. SpecificSerializationProperties /// /// TypeTable has the following information used by deserializer: /// 1. TargetTypeForDeserialization /// 2. TypeConverter /// /// /// Application arguments the server can see in /// /// A remote Runspace. public static Runspace CreateRunspace(RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable, PSPrimitiveDictionary applicationArguments) { return CreateRunspace(connectionInfo, host, typeTable, applicationArguments, null); } /// /// Creates a remote Runspace. /// /// It defines connection path to a remote runspace that needs to be created. /// The explicit PSHost implementation. /// /// The TypeTable to use while deserializing/serializing remote objects. /// TypeTable has the following information used by serializer: /// 1. SerializationMethod /// 2. SerializationDepth /// 3. SpecificSerializationProperties /// /// TypeTable has the following information used by deserializer: /// 1. TargetTypeForDeserialization /// 2. TypeConverter /// /// /// Application arguments the server can see in /// /// Name for remote runspace. /// A remote Runspace. public static Runspace CreateRunspace(RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable, PSPrimitiveDictionary applicationArguments, string name) { if (connectionInfo is WSManConnectionInfo) { RemotingCommandUtil.CheckHostRemotingPrerequisites(); } return new RemoteRunspace(typeTable, connectionInfo, host, applicationArguments, name); } /// /// Creates a remote Runspace. /// /// The explicit PSHost implementation. /// It defines connection path to a remote runspace that needs to be created. /// A remote Runspace. public static Runspace CreateRunspace(PSHost host, RunspaceConnectionInfo connectionInfo) { return CreateRunspace(connectionInfo, host, null); } /// /// Creates a remote Runspace. /// /// It defines connection path to a remote runspace that needs to be created. /// A remote Runspace. public static Runspace CreateRunspace(RunspaceConnectionInfo connectionInfo) { return CreateRunspace(null, connectionInfo); } #endregion Runspace - Remote Factory #region V3 Extensions /// /// Creates an out-of-process remote Runspace. /// /// /// The TypeTable to use while deserializing/serializing remote objects. /// TypeTable has the following information used by serializer: /// 1. SerializationMethod /// 2. SerializationDepth /// 3. SpecificSerializationProperties /// /// TypeTable has the following information used by deserializer: /// 1. TargetTypeForDeserialization /// 2. TypeConverter /// /// An out-of-process remote Runspace. public static Runspace CreateOutOfProcessRunspace(TypeTable typeTable) { NewProcessConnectionInfo connectionInfo = new NewProcessConnectionInfo(null); return CreateRunspace(connectionInfo, null, typeTable); } /// /// Creates an out-of-process remote Runspace. /// /// /// The TypeTable to use while deserializing/serializing remote objects. /// TypeTable has the following information used by serializer: /// 1. SerializationMethod /// 2. SerializationDepth /// 3. SpecificSerializationProperties /// /// TypeTable has the following information used by deserializer: /// 1. TargetTypeForDeserialization /// 2. TypeConverter /// /// It represents a PowerShell process that is used for an out-of-process remote Runspace /// An out-of-process remote Runspace. public static Runspace CreateOutOfProcessRunspace(TypeTable typeTable, PowerShellProcessInstance processInstance) { NewProcessConnectionInfo connectionInfo = new NewProcessConnectionInfo(null) { Process = processInstance }; return CreateRunspace(connectionInfo, null, typeTable); } #endregion V3 Extensions } }