/********************************************************************++ Copyright (c) Microsoft Corporation. All rights reserved. --********************************************************************/ using System.Globalization; using System.Diagnostics.CodeAnalysis; 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 . /// This runspace is created using the information /// from EntryAssembly. /// /// /// 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("host"); } return CreateRunspace(host, RunspaceConfiguration.Create()); } /// /// Creates a runspace using /// /// /// RunspaceConfiguration information for the runspace. /// /// /// A runspace object /// /// /// Thrown when runspaceConfiguration is null /// #if CORECLR internal #else public #endif static Runspace CreateRunspace(RunspaceConfiguration runspaceConfiguration) { if (runspaceConfiguration == null) { throw PSTraceSource.NewArgumentNullException("runspaceConfiguration"); } PSHost host = new DefaultHost(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture); return CreateRunspace(host, runspaceConfiguration); } /// /// Creates a runspace using specified PSHost and RunspaceConfiguration /// /// /// Host implementation for runspace. /// /// /// RunspaceConfiguration information for the runspace. /// /// /// A runspace object /// /// /// Thrown when host is null /// /// /// Thrown when runspaceConfiguration is null /// #if CORECLR internal #else public #endif static Runspace CreateRunspace(PSHost host, RunspaceConfiguration runspaceConfiguration) { if (host == null) { throw PSTraceSource.NewArgumentNullException("host"); } if (runspaceConfiguration == null) { throw PSTraceSource.NewArgumentNullException("runspaceConfiguration"); } return new LocalRunspace(host, runspaceConfiguration); } //========================================================= // Create runspaces with InitialSessionState instead of RunspaceConfig objects... /// /// 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("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("host"); } if (initialSessionState == null) { throw PSTraceSource.NewArgumentNullException("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("host"); } if (initialSessionState == null) { throw PSTraceSource.NewArgumentNullException("initialSessionState"); } return new LocalRunspace(host, initialSessionState, true); } #endregion #region RunspacePool Factory /// /// Creates a RunspacePool using default RunspaceConfiguration /// with MaxRunspaces 1 and MinRunspaces 1. /// public static RunspacePool CreateRunspacePool() { return CreateRunspacePool(1, 1, RunspaceConfiguration.Create(), new DefaultHost ( CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture )); } /// /// Creates a RunspacePool using default RunspaceConfiguration. /// /// 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, RunspaceConfiguration.Create(), 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. /// /// /// RunspaceConfiguration 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 CreateRunspacePool(minRunspaces, maxRunspaces, RunspaceConfiguration.Create(), 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. /// /// /// RunspaceConfiguration to use when creating a new Runspace in the /// pool. /// /// /// RunspaceConfiguration is null. /// /// /// The explicit PSHost implementation. /// /// /// is null. /// is null. /// /// /// Maximum runspaces is less than 1. /// Minimum runspaces is less than 1. /// private static RunspacePool CreateRunspacePool(int minRunspaces, int maxRunspaces, RunspaceConfiguration runspaceConfiguration, PSHost host) { return new RunspacePool(minRunspaces, maxRunspaces, runspaceConfiguration, 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. /// /// /// RunspaceConfiguration 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 compuer. /// /// 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. SerailizationDepth /// 3. SpecificSerializationProperties /// TypeTable has the following inforamtion used by deserializer: /// 1. TargetTypeForDeserializaiton /// 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 compuer. /// /// 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. SerailizationDepth /// 3. SpecificSerializationProperties /// TypeTable has the following inforamtion used by deserializer: /// 1. TargetTypeForDeserializaiton /// 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 WSManConnectionInfo)) && (!(connectionInfo is NewProcessConnectionInfo)) && (!(connectionInfo is NamedPipeConnectionInfo)) && (!(connectionInfo is VMConnectionInfo)) && (!(connectionInfo is 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 /// /// /// /// /// The TypeTable to use while deserializing/serializing remote objects. /// TypeTable has the following information used by serializer: /// 1. SerializationMethod /// 2. SerailizationDepth /// 3. SpecificSerializationProperties /// TypeTable has the following inforamtion used by deserializer: /// 1. TargetTypeForDeserializaiton /// 2. TypeConverter /// /// /// /// public static Runspace CreateRunspace(RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable) { return CreateRunspace(connectionInfo, host, typeTable, null, null); } /// /// /// /// /// The TypeTable to use while deserializing/serializing remote objects. /// TypeTable has the following information used by serializer: /// 1. SerializationMethod /// 2. SerailizationDepth /// 3. SpecificSerializationProperties /// TypeTable has the following inforamtion used by deserializer: /// 1. TargetTypeForDeserializaiton /// 2. TypeConverter /// /// /// /// /// Application arguments the server can see in /// /// public static Runspace CreateRunspace(RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable, PSPrimitiveDictionary applicationArguments) { return CreateRunspace(connectionInfo, host, typeTable, applicationArguments, null); } /// /// /// /// /// /// /// The TypeTable to use while deserializing/serializing remote objects. /// TypeTable has the following information used by serializer: /// 1. SerializationMethod /// 2. SerailizationDepth /// 3. SpecificSerializationProperties /// TypeTable has the following inforamtion used by deserializer: /// 1. TargetTypeForDeserializaiton /// 2. TypeConverter /// /// /// Application arguments the server can see in /// /// Name for remote runspace. /// public static Runspace CreateRunspace(RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable, PSPrimitiveDictionary applicationArguments, string name) { if ((!(connectionInfo is WSManConnectionInfo)) && (!(connectionInfo is NewProcessConnectionInfo)) && (!(connectionInfo is NamedPipeConnectionInfo)) && (!(connectionInfo is SSHConnectionInfo)) && (!(connectionInfo is VMConnectionInfo)) && (!(connectionInfo is ContainerConnectionInfo))) { throw new NotSupportedException(); } if (connectionInfo is WSManConnectionInfo) { RemotingCommandUtil.CheckHostRemotingPrerequisites(); } return new RemoteRunspace(typeTable, connectionInfo, host, applicationArguments, name); } /// /// /// /// /// /// public static Runspace CreateRunspace(PSHost host, RunspaceConnectionInfo connectionInfo) { return CreateRunspace(connectionInfo, host, null); } /// /// /// /// /// public static Runspace CreateRunspace(RunspaceConnectionInfo connectionInfo) { return CreateRunspace(null, connectionInfo); } #endregion Runspace - Remote Factory #region V3 Extensions /// /// /// /// /// public static Runspace CreateOutOfProcessRunspace(TypeTable typeTable) { NewProcessConnectionInfo connectionInfo = new NewProcessConnectionInfo(null); return CreateRunspace(connectionInfo, null, typeTable); } /// /// /// /// /// /// public static Runspace CreateOutOfProcessRunspace(TypeTable typeTable, PowerShellProcessInstance processInstance) { NewProcessConnectionInfo connectionInfo = new NewProcessConnectionInfo(null) { Process = processInstance }; return CreateRunspace(connectionInfo, null, typeTable); } #endregion V3 Extensions } }