public override PSPrimitiveDictionary GetApplicationPrivateData(PSSenderInfo senderInfo) { if (!Constants.IsPowerShellWebService) { ExchangeAuthorizationPlugin.InitializeAuthZPluginForRemotePS(senderInfo.ConnectionString); } ExchangeAuthorizationPlugin.EnsureSettingOverrideSyncIsStarted(); return(AuthZLogHelper.StartAndEndLoging <PSPrimitiveDictionary>("GetApplicationPrivateData", () => AuthZLogHelper.ExecuteWSManPluginAPI <PSPrimitiveDictionary>("GetApplicationPrivateData", true, true, null, delegate() { InitialSessionState initialSessionStateCore = this.GetInitialSessionStateCore(senderInfo); if (!Constants.IsPowerShellWebService) { this.CheckSessionOverBudget(); } this.currentUserISS.Target = initialSessionStateCore; int value = InitialSessionStateBuilder.CalculateHashForImplicitRemoting(initialSessionStateCore); PSPrimitiveDictionary psprimitiveDictionary = new PSPrimitiveDictionary(); psprimitiveDictionary.Add("ImplicitRemoting", new PSPrimitiveDictionary { { "Hash", value } }); if (!Constants.IsPowerShellWebService) { psprimitiveDictionary.Add("SupportedVersions", this.ExpandVersions(senderInfo.ConnectionString)); } this.LogCommonValues(); return psprimitiveDictionary; }))); }
/// <summary> /// Constructor which creates a RunspacePool using the /// supplied <paramref name="connectionInfo"/>, <paramref name="minRunspaces"/> /// and <paramref name="maxRunspaces"/> /// </summary> /// <param name="maxRunspaces"> /// The maximum number of Runspaces that can exist in this pool. /// Should be greater than or equal to 1. /// </param> /// <param name="minRunspaces"> /// The minimum number of Runspaces that can exist in this pool. /// Should be greater than or equal to 1. /// </param> /// <param name="typeTable"> /// 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 /// </param> /// <param name="host">Host associated with this runspacepool</param> /// <param name="applicationArguments"> /// Application arguments the server can see in <see cref="System.Management.Automation.Remoting.PSSenderInfo.ApplicationArguments"/> /// </param> /// <param name="connectionInfo">The RunspaceConnectionInfo object /// which identifies this runspace pools connection to the server /// </param> /// <param name="name">Session name.</param> /// <exception cref="ArgumentException"> /// Maximum runspaces is less than 1. /// Minimum runspaces is less than 1. /// </exception> /// <exception cref="ArgumentException"> /// ConnectionInfo specified is null /// </exception> internal RemoteRunspacePoolInternal(int minRunspaces, int maxRunspaces, TypeTable typeTable, PSHost host, PSPrimitiveDictionary applicationArguments, RunspaceConnectionInfo connectionInfo, string name = null) : base(minRunspaces, maxRunspaces) { if (connectionInfo == null) { throw PSTraceSource.NewArgumentNullException("WSManConnectionInfo"); } PSEtwLog.LogOperationalVerbose(PSEventId.RunspacePoolConstructor, PSOpcode.Constructor, PSTask.CreateRunspace, PSKeyword.UseAlwaysOperational, instanceId.ToString(), minPoolSz.ToString(CultureInfo.InvariantCulture), maxPoolSz.ToString(CultureInfo.InvariantCulture)); _connectionInfo = connectionInfo.InternalCopy(); this.host = host; ApplicationArguments = applicationArguments; AvailableForConnection = false; DispatchTable = new DispatchTable<object>(); _runningPowerShells = new System.Collections.Concurrent.ConcurrentStack<PowerShell>(); if (!string.IsNullOrEmpty(name)) { this.Name = name; } CreateDSHandler(typeTable); }
internal RemoteRunspacePoolInternal(int minRunspaces, int maxRunspaces, TypeTable typeTable, PSHost host, PSPrimitiveDictionary applicationArguments, RunspaceConnectionInfo connectionInfo, string name = null) : base(minRunspaces, maxRunspaces) { this.applicationPrivateDataReceived = new ManualResetEvent(false); this.friendlyName = string.Empty; if (connectionInfo == null) { throw PSTraceSource.NewArgumentNullException("WSManConnectionInfo"); } PSEtwLog.LogOperationalVerbose(PSEventId.RunspacePoolConstructor, PSOpcode.Constructor, PSTask.CreateRunspace, PSKeyword.UseAlwaysOperational, new object[] { this.instanceId.ToString(), this.minPoolSz.ToString(CultureInfo.InvariantCulture), this.maxPoolSz.ToString(CultureInfo.InvariantCulture) }); if (connectionInfo is WSManConnectionInfo) { this.connectionInfo = ((WSManConnectionInfo)connectionInfo).Copy(); } else if (connectionInfo is NewProcessConnectionInfo) { this.connectionInfo = ((NewProcessConnectionInfo)connectionInfo).Copy(); } base.host = host; this.applicationArguments = applicationArguments; this.availableForConnection = false; this.dispatchTable = new DispatchTable <object>(); if (!string.IsNullOrEmpty(name)) { this.Name = name; } this.CreateDSHandler(typeTable); }
private PSSenderInfo(SerializationInfo info, StreamingContext context) { if (info != null) { string source = null; try { source = info.GetValue("CliXml", typeof(string)) as string; } catch (Exception) { return; } if (source != null) { try { PSSenderInfo info2 = DeserializingTypeConverter.RehydratePSSenderInfo(PSObject.AsPSObject(PSSerializer.Deserialize(source))); this.userPrinicpal = info2.userPrinicpal; this.clientTimeZone = info2.ClientTimeZone; this.connectionString = info2.connectionString; this.applicationArguments = info2.applicationArguments; } catch (Exception) { return; } } } }
private static SupportedVersionList ExtractSupportedVersionList(Runspace newRunspace) { PSPrimitiveDictionary applicationPrivateData = newRunspace.GetApplicationPrivateData(); object obj = applicationPrivateData["SupportedVersions"]; return(SupportedVersionList.Parse((obj != null) ? obj.ToString() : string.Empty)); }
internal void ProcessReceivedData(RemoteDataObject <PSObject> receivedData) { if (receivedData.RunspacePoolId != this.clientRunspacePoolId) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.RunspaceIdsDoNotMatch, new object[] { receivedData.RunspacePoolId, this.clientRunspacePoolId }); } switch (receivedData.DataType) { case RemotingDataType.RunspacePoolOperationResponse: this.SetMaxMinRunspacesResponseRecieved.SafeInvoke <RemoteDataEventArgs <PSObject> >(this, new RemoteDataEventArgs <PSObject>(receivedData.Data)); return; case RemotingDataType.RunspacePoolStateInfo: { RunspacePoolStateInfo runspacePoolStateInfo = RemotingDecoder.GetRunspacePoolStateInfo(receivedData.Data); this.StateInfoReceived.SafeInvoke <RemoteDataEventArgs <RunspacePoolStateInfo> >(this, new RemoteDataEventArgs <RunspacePoolStateInfo>(runspacePoolStateInfo)); this.NotifyAssociatedPowerShells(runspacePoolStateInfo); return; } case RemotingDataType.CreatePowerShell: case RemotingDataType.AvailableRunspaces: case RemotingDataType.GetCommandMetadata: break; case RemotingDataType.PSEventArgs: { PSEventArgs pSEventArgs = RemotingDecoder.GetPSEventArgs(receivedData.Data); this.PSEventArgsReceived.SafeInvoke <RemoteDataEventArgs <PSEventArgs> >(this, new RemoteDataEventArgs <PSEventArgs>(pSEventArgs)); break; } case RemotingDataType.ApplicationPrivateData: { PSPrimitiveDictionary applicationPrivateData = RemotingDecoder.GetApplicationPrivateData(receivedData.Data); this.ApplicationPrivateDataReceived.SafeInvoke <RemoteDataEventArgs <PSPrimitiveDictionary> >(this, new RemoteDataEventArgs <PSPrimitiveDictionary>(applicationPrivateData)); return; } case RemotingDataType.RunspacePoolInitData: { RunspacePoolInitInfo runspacePoolInitInfo = RemotingDecoder.GetRunspacePoolInitInfo(receivedData.Data); this.RSPoolInitInfoReceived.SafeInvoke <RemoteDataEventArgs <RunspacePoolInitInfo> >(this, new RemoteDataEventArgs <RunspacePoolInitInfo>(runspacePoolInitInfo)); return; } case RemotingDataType.RemoteHostCallUsingRunspaceHost: { RemoteHostCall data = RemoteHostCall.Decode(receivedData.Data); this.RemoteHostCallReceived.SafeInvoke <RemoteDataEventArgs <RemoteHostCall> >(this, new RemoteDataEventArgs <RemoteHostCall>(data)); return; } default: return; } }
internal System.Version GetRemoteProtocolVersion() { System.Version version; if (PSPrimitiveDictionary.TryPathGet <System.Version>(this.GetApplicationPrivateData(), out version, new string[] { "PSVersionTable", "PSRemotingProtocolVersion" })) { return(version); } return(RemotingConstants.ProtocolVersion); }
private TSession GetImpliedSession() { TSession local; if ((this.PSModuleInfo != null) && PSPrimitiveDictionary.TryPathGet <TSession>(this.PSModuleInfo.PrivateData as IDictionary, out local, new string[] { "CmdletsOverObjects", "DefaultSession" })) { return(local); } return(this.DefaultSession); }
private void GenerateHelperFunctionsGetImplicitRunspace(TextWriter writer) { string str; if (writer == null) { throw PSTraceSource.NewArgumentNullException("writer"); } PSPrimitiveDictionary.TryPathGet <string>(this.remoteRunspaceInfo.ApplicationPrivateData, out str, new string[] { "ImplicitRemoting", "Hash" }); str = str ?? string.Empty; writer.Write("\r\nfunction Get-PSImplicitRemotingSession\r\n{{\r\n param(\r\n [Parameter(Mandatory = $true, Position = 0)]\r\n [string] \r\n $commandName\r\n )\r\n\r\n $savedImplicitRemotingHash = '{4}'\r\n\r\n if (($script:PSSession -eq $null) -or ($script:PSSession.Runspace.RunspaceStateInfo.State -ne 'Opened'))\r\n {{\r\n Set-PSImplicitRemotingSession `\r\n (& $script:GetPSSession `\r\n -InstanceId {0} `\r\n -ErrorAction SilentlyContinue )\r\n }}\r\n if (($script:PSSession -ne $null) -and ($script:PSSession.Runspace.RunspaceStateInfo.State -eq 'Disconnected'))\r\n {{\r\n # If we are handed a disconnected session, try re-connecting it before creating a new session.\r\n Set-PSImplicitRemotingSession `\r\n (& $script:ConnectPSSession `\r\n -Session $script:PSSession `\r\n -ErrorAction SilentlyContinue)\r\n }}\r\n if (($script:PSSession -eq $null) -or ($script:PSSession.Runspace.RunspaceStateInfo.State -ne 'Opened'))\r\n {{\r\n Write-PSImplicitRemotingMessage ('{1}' -f $commandName)\r\n\r\n Set-PSImplicitRemotingSession `\r\n -CreatedByModule $true `\r\n -PSSession ( {2} )\r\n\r\n if ($savedImplicitRemotingHash -ne '')\r\n {{\r\n $newImplicitRemotingHash = [string]($script:PSSession.ApplicationPrivateData.{6}.{7})\r\n if ($newImplicitRemotingHash -ne $savedImplicitRemotingHash)\r\n {{\r\n & $script:WriteWarning -Message '{5}'\r\n }}\r\n }}\r\n\r\n {8}\r\n }}\r\n if (($script:PSSession -eq $null) -or ($script:PSSession.Runspace.RunspaceStateInfo.State -ne 'Opened'))\r\n {{\r\n throw '{3}'\r\n }}\r\n return [Management.Automation.Runspaces.PSSession]$script:PSSession\r\n}}\r\n", new object[] { this.remoteRunspaceInfo.InstanceId, CommandMetadata.EscapeSingleQuotedString(StringUtil.Format(ImplicitRemotingStrings.CreateNewRunspaceMessageTemplate, new object[0])), this.GenerateNewRunspaceExpression(), CommandMetadata.EscapeSingleQuotedString(StringUtil.Format(ImplicitRemotingStrings.ErrorNoRunspaceForThisModule, new object[0])), CommandMetadata.EscapeSingleQuotedString(str), CommandMetadata.EscapeSingleQuotedString(StringUtil.Format(ImplicitRemotingStrings.WarningMismatchedImplicitRemotingHash, new object[0])), "ImplicitRemoting", "Hash", this.GenerateReimportingOfModules() }); }
public override PSPrimitiveDictionary GetApplicationPrivateData() { if (this.applicationPrivateData == null) { lock (this.SyncRoot) { if (this.applicationPrivateData == null) { this.applicationPrivateData = new PSPrimitiveDictionary(); } } } return(this.applicationPrivateData); }
internal virtual PSPrimitiveDictionary GetApplicationPrivateData() { if (this.applicationPrivateData == null) { lock (this.syncObject) { if (this.applicationPrivateData == null) { this.applicationPrivateData = new PSPrimitiveDictionary(); } } } return(this.applicationPrivateData); }
internal void SetApplicationPrivateData(PSPrimitiveDictionary applicationPrivateData) { lock (base.syncObject) { if (!this.applicationPrivateDataReceived.WaitOne(0, false)) { this.applicationPrivateData = applicationPrivateData; this.applicationPrivateDataReceived.Set(); foreach (Runspace runspace in base.runspaceList) { runspace.SetApplicationPrivateData(applicationPrivateData); } } } }
internal ClientRunspacePoolDataStructureHandler(RemoteRunspacePoolInternal clientRunspacePool, TypeTable typeTable) { this.clientRunspacePoolId = clientRunspacePool.InstanceId; this.minRunspaces = clientRunspacePool.GetMinRunspaces(); this.maxRunspaces = clientRunspacePool.GetMaxRunspaces(); this.host = clientRunspacePool.Host; this.applicationArguments = clientRunspacePool.ApplicationArguments; this.remoteSession = this.CreateClientRemoteSession(clientRunspacePool); this.transportManager = this.remoteSession.SessionDataStructureHandler.TransportManager; this.transportManager.TypeTable = typeTable; this.remoteSession.StateChanged += new EventHandler <RemoteSessionStateEventArgs>(this.HandleClientRemoteSessionStateChanged); this._reconnecting = false; this.transportManager.RobustConnectionNotification += new EventHandler <ConnectionStatusEventArgs>(this.HandleRobustConnectionNotification); this.transportManager.CreateCompleted += new EventHandler <CreateCompleteEventArgs>(this.HandleSessionCreateCompleted); }
private void HandleCreateRunspacePool(object sender, RemoteDataEventArgs createRunspaceEventArg) { using (ServerRemoteSession._trace.TraceMethod()) { RemoteDataObject <PSObject> remoteDataObject = createRunspaceEventArg != null ? createRunspaceEventArg.ReceivedData : throw ServerRemoteSession._trace.NewArgumentNullException(nameof(createRunspaceEventArg)); if (this._context != null) { this._senderInfo.ClientTimeZone = this._context.ClientCapability.TimeZone; } this._senderInfo.ApplicationArguments = RemotingDecoder.GetApplicationArguments(remoteDataObject.Data); ConfigurationDataFromXML configData = PSSessionConfiguration.LoadEndPointConfiguration(this._configProviderId, this._initParameters); configData.InitializationScriptForOutOfProcessRunspace = this._initScriptForOutOfProcRS; this.maxRecvdObjectSize = configData.MaxReceivedObjectSizeMB; this.maxRecvdDataSizeCommand = configData.MaxReceivedCommandSizeMB; this._sessionConfigProvider = configData.CreateEndPointConfigurationInstance(); PSPrimitiveDictionary applicationPrivateData = this._sessionConfigProvider.GetApplicationPrivateData(this._senderInfo); InitialSessionState initialSessionState = this._sessionConfigProvider.GetInitialSessionState(this._senderInfo); if (initialSessionState == null) { throw ServerRemoteSession._trace.NewInvalidOperationException("RemotingErrorIdStrings", "InitialSessionStateNull", (object)this._configProviderId); } initialSessionState.ThrowOnRunspaceOpenError = true; initialSessionState.Variables.Add(new SessionStateVariableEntry("PSSenderInfo", (object)this._senderInfo, PSRemotingErrorInvariants.FormatResourceString(PSRemotingErrorId.PSSenderInfoDescription), ScopedItemOptions.ReadOnly)); if (!string.IsNullOrEmpty(configData.EndPointConfigurationTypeName)) { this.maxRecvdObjectSize = this._sessionConfigProvider.GetMaximumReceivedObjectSize(this._senderInfo); this.maxRecvdDataSizeCommand = this._sessionConfigProvider.GetMaximumReceivedDataSizePerCommand(this._senderInfo); } this._sessionDSHandler.TransportManager.ReceivedDataCollection.MaximumReceivedObjectSize = this.maxRecvdObjectSize; Guid runspacePoolId = remoteDataObject.RunspacePoolId; int minRunspaces = RemotingDecoder.GetMinRunspaces(remoteDataObject.Data); int maxRunspaces = RemotingDecoder.GetMaxRunspaces(remoteDataObject.Data); PSThreadOptions threadOptions = RemotingDecoder.GetThreadOptions(remoteDataObject.Data); ApartmentState apartmentState = RemotingDecoder.GetApartmentState((object)remoteDataObject.Data); HostInfo hostInfo = RemotingDecoder.GetHostInfo(remoteDataObject.Data); if (this._runspacePoolDriver != null) { throw new PSRemotingDataStructureException(PSRemotingErrorId.RunspaceAlreadyExists, new object[1] { (object)this._runspacePoolDriver.InstanceId }); } bool isAdministrator = this._senderInfo.UserInfo.IsInRole(WindowsBuiltInRole.Administrator); this._runspacePoolDriver = new ServerRunspacePoolDriver(runspacePoolId, minRunspaces, maxRunspaces, threadOptions, apartmentState, hostInfo, initialSessionState, applicationPrivateData, configData, this.SessionDataStructureHandler.TransportManager, isAdministrator, this._context.ServerCapability); this._runspacePoolDriver.Closed += new EventHandler <EventArgs>(this.HandleResourceClosing); this._runspacePoolDriver.Start(); } }
public static Runspace CreateRunspace( RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable, PSPrimitiveDictionary applicationArguments) { switch (connectionInfo) { case WSManConnectionInfo _: case NewProcessConnectionInfo _: return((Runspace) new RemoteRunspace(typeTable, connectionInfo, host, applicationArguments)); default: throw new NotSupportedException(); } }
internal ClientRunspacePoolDataStructureHandler( RemoteRunspacePoolInternal clientRunspacePool, TypeTable typeTable) { using (ClientRunspacePoolDataStructureHandler.tracer.TraceConstructor((object)this)) { this.clientRunspacePoolId = clientRunspacePool.InstanceId; this.minRunspaces = clientRunspacePool.GetMinRunspaces(); this.maxRunspaces = clientRunspacePool.GetMaxRunspaces(); this.host = clientRunspacePool.Host; this.applicationArguments = clientRunspacePool.ApplicationArguments; this.remoteSession = (ClientRemoteSession)this.CreateClientRemoteSession(clientRunspacePool); this.transportManager = this.remoteSession.SessionDataStructureHandler.TransportManager; this.transportManager.TypeTable = typeTable; this.remoteSession.StateChanged += new EventHandler <RemoteSessionStateEventArgs>(this.HandleClientRemoteSessionStateChanged); } }
public static RunspacePool CreateRunspacePool( int minRunspaces, int maxRunspaces, RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable, PSPrimitiveDictionary applicationArguments) { switch (connectionInfo) { case WSManConnectionInfo _: case NewProcessConnectionInfo _: return(new RunspacePool(minRunspaces, maxRunspaces, typeTable, host, applicationArguments, connectionInfo)); default: throw new NotSupportedException(); } }
/// <summary> /// Deserialization constructor /// </summary> /// <param name="info"></param> /// <param name="context"></param> private PSSenderInfo(SerializationInfo info, StreamingContext context) { if (info == null) { return; } string serializedData = null; try { serializedData = info.GetValue("CliXml", typeof(string)) as string; } catch (Exception) { // When a workflow is run locally, there won't be PSSenderInfo return; } if (serializedData == null) { return; } try { PSObject result = PSObject.AsPSObject(PSSerializer.Deserialize(serializedData)); PSSenderInfo senderInfo = DeserializingTypeConverter.RehydratePSSenderInfo(result); UserInfo = senderInfo.UserInfo; ConnectionString = senderInfo.ConnectionString; _applicationArguments = senderInfo._applicationArguments; #if !CORECLR // TimeZone Not In CoreCLR this.clientTimeZone = senderInfo.ClientTimeZone; #endif } catch (Exception) { // Ignore conversion errors return; } }
private TSession GetImpliedSession() { TSession sessionFromImportModule; // When being called from a CIM activity, this will be invoked as // a function so there will be no module info if (this.PSModuleInfo != null) { if (PSPrimitiveDictionary.TryPathGet( this.PSModuleInfo.PrivateData as IDictionary, out sessionFromImportModule, ScriptWriter.PrivateDataKey_CmdletsOverObjects, ScriptWriter.PrivateDataKey_DefaultSession)) { return sessionFromImportModule; } } return this.DefaultSession; }
/// <summary> /// Construct a runspace pool object. /// </summary> /// <param name="minRunspaces">Min runspaces.</param> /// <param name="maxRunspaces">Max runspaces.</param> /// <param name="typeTable">TypeTable.</param> /// <param name="host">Host.</param> /// <param name="applicationArguments">App arguments.</param> /// <param name="connectionInfo">Connection information.</param> /// <param name="name">Session name.</param> internal RunspacePool( int minRunspaces, int maxRunspaces, TypeTable typeTable, PSHost host, PSPrimitiveDictionary applicationArguments, RunspaceConnectionInfo connectionInfo, string name = null) { _internalPool = new RemoteRunspacePoolInternal( minRunspaces, maxRunspaces, typeTable, host, applicationArguments, connectionInfo, name); IsRemote = true; }
internal RemoteRunspacePoolInternal( int minRunspaces, int maxRunspaces, TypeTable typeTable, PSHost host, PSPrimitiveDictionary applicationArguments, RunspaceConnectionInfo connectionInfo) : base(minRunspaces, maxRunspaces) { using (RemoteRunspacePoolInternal.tracer.TraceConstructor((object)this)) { if (connectionInfo == null) { throw RemoteRunspacePoolInternal.tracer.NewArgumentNullException("WSManConnectionInfo"); } this.etwTracer.OperationalChannel.WriteVerbose(PSEventId.RunspacePoolConstructor, PSOpcode.Constructor, PSTask.CreateRunspace, (object)this.instanceId, (object)this.minPoolSz.ToString((IFormatProvider)CultureInfo.InvariantCulture), (object)this.maxPoolSz.ToString((IFormatProvider)CultureInfo.InvariantCulture)); if (connectionInfo is WSManConnectionInfo) { this.connectionInfo = (RunspaceConnectionInfo)((WSManConnectionInfo)connectionInfo).Copy(); } else if (connectionInfo is NewProcessConnectionInfo) { this.connectionInfo = (RunspaceConnectionInfo)((NewProcessConnectionInfo)connectionInfo).Copy(); } this.host = host; this.applicationArguments = applicationArguments; this.dispatchTable = new System.Management.Automation.Remoting.DispatchTable <object>(); this.dataStructureHandler = new ClientRunspacePoolDataStructureHandler(this, typeTable); this.dataStructureHandler.RemoteHostCallReceived += new EventHandler <RemoteDataEventArgs <RemoteHostCall> >(this.HandleRemoteHostCalls); this.dataStructureHandler.StateInfoReceived += new EventHandler <RemoteDataEventArgs <RunspacePoolStateInfo> >(this.HandleStateInfoReceived); this.dataStructureHandler.ApplicationPrivateDataReceived += new EventHandler <RemoteDataEventArgs <PSPrimitiveDictionary> >(this.HandleApplicationPrivateDataReceived); this.dataStructureHandler.SessionClosing += new EventHandler <RemoteDataEventArgs <Exception> >(this.HandleSessionClosing); this.dataStructureHandler.SessionClosed += new EventHandler <RemoteDataEventArgs <Exception> >(this.HandleSessionClosed); this.dataStructureHandler.SetMaxMinRunspacesResponseRecieved += new EventHandler <RemoteDataEventArgs <PSObject> >(this.HandleResponseReceived); this.dataStructureHandler.URIRedirectionReported += new EventHandler <RemoteDataEventArgs <Uri> >(this.HandleURIDirectionReported); this.dataStructureHandler.PSEventArgsReceived += new EventHandler <RemoteDataEventArgs <PSEventArgs> >(this.HandlePSEventArgsReceived); } }
internal RunspacePool( int minRunspaces, int maxRunspaces, TypeTable typeTable, PSHost host, PSPrimitiveDictionary applicationArguments, RunspaceConnectionInfo connectionInfo) { using (RunspacePool.tracer.TraceConstructor((object)this)) { switch (connectionInfo) { case WSManConnectionInfo _: case NewProcessConnectionInfo _: this.internalPool = (RunspacePoolInternal) new RemoteRunspacePoolInternal(minRunspaces, maxRunspaces, typeTable, host, applicationArguments, connectionInfo); this.isRemote = true; break; default: throw new NotSupportedException(); } } }
/// <summary> /// A method that runspace pools can use to propagate application private data into runspaces /// </summary> /// <param name="applicationPrivateData"></param> internal override void SetApplicationPrivateData(PSPrimitiveDictionary applicationPrivateData) { _applicationPrivateData = applicationPrivateData; }
internal void SetApplicationPrivateData(PSPrimitiveDictionary applicationPrivateData) { lock (this.syncObject) { if (_applicationPrivateDataReceived.WaitOne(0)) { return; // ignore server's attempt to set application private data if it has already been set } _applicationPrivateData = applicationPrivateData; _applicationPrivateDataReceived.Set(); foreach (Runspace runspace in this.runspaceList) { runspace.SetApplicationPrivateData(applicationPrivateData); } } }
/// <summary> /// Private data to be used by applications built on top of PowerShell. /// /// Local runspace pool is created with application private data set to an empty <see cref="PSPrimitiveDictionary"/>. /// </summary> internal virtual PSPrimitiveDictionary GetApplicationPrivateData() { if (_applicationPrivateData == null) { lock (this.syncObject) { if (_applicationPrivateData == null) { _applicationPrivateData = new PSPrimitiveDictionary(); } } } return _applicationPrivateData; }
internal RunspacePool(int minRunspaces, int maxRunspaces, TypeTable typeTable, PSHost host, PSPrimitiveDictionary applicationArguments, RunspaceConnectionInfo connectionInfo) { throw new NotImplementedException(); }
/// <summary> /// </summary> /// <param name="connectionInfo"></param> /// <param name="host"></param> /// <param name="typeTable"> /// 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 /// </param> /// <param name="applicationArguments"> /// Application arguments the server can see in <see cref="System.Management.Automation.Remoting.PSSenderInfo.ApplicationArguments"/> /// </param> /// <param name="name">Name for remote runspace.</param> /// <returns></returns> 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)); }
/// <summary> /// /// </summary> /// <param name="typeTable"> /// 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 /// </param> /// <param name="host"></param> /// <param name="connectionInfo"></param> /// <param name="applicationArguments"> /// Application arguments the server can see in <see cref="System.Management.Automation.Remoting.PSSenderInfo.ApplicationArguments"/> /// </param> /// <returns></returns> public static Runspace CreateRunspace(RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable, PSPrimitiveDictionary applicationArguments) { return CreateRunspace(connectionInfo, host, typeTable, applicationArguments, null); }
/// <summary> /// A method that runspace pools can use to propagate application private data into runspaces /// </summary> /// <param name="applicationPrivateData"></param> internal abstract void SetApplicationPrivateData(PSPrimitiveDictionary applicationPrivateData);
private void HandleClientRemoteSessionStateChanged(object sender, RemoteSessionStateEventArgs e) { if (e.SessionStateInfo.State == RemoteSessionState.NegotiationSending) { if (this.createRunspaceCalled) { return; } lock (this.syncObject) { if (this.createRunspaceCalled) { return; } this.createRunspaceCalled = true; } PSPrimitiveDictionary applicationArguments = PSPrimitiveDictionary.CloneAndAddPSVersionTable(this.applicationArguments); var createPoolData = RemotingEncoder.GenerateCreateRunspacePool(this.clientRunspacePoolId, this.minRunspaces, this.maxRunspaces, this.remoteSession.RemoteRunspacePoolInternal, this.host, applicationArguments); this.SendDataAsync(createPoolData); } if (e.SessionStateInfo.State == RemoteSessionState.NegotiationSendingOnConnect) { this.SendDataAsync(RemotingEncoder.GenerateConnectRunspacePool(this.clientRunspacePoolId, this.minRunspaces, this.maxRunspaces)); } else if (e.SessionStateInfo.State == RemoteSessionState.ClosingConnection) { List <ClientPowerShellDataStructureHandler> list; Exception closingReason = this.closingReason; if (closingReason == null) { closingReason = e.SessionStateInfo.Reason; this.closingReason = closingReason; } lock (this.associationSyncObject) { list = new List <ClientPowerShellDataStructureHandler>(this.associatedPowerShellDSHandlers.Values); } foreach (ClientPowerShellDataStructureHandler handler in list) { handler.CloseConnectionAsync(this.closingReason); } this.SessionClosing.SafeInvoke <RemoteDataEventArgs <Exception> >(this, new RemoteDataEventArgs <Exception>(closingReason)); } else if (e.SessionStateInfo.State == RemoteSessionState.Closed) { Exception reason = this.closingReason; if (reason == null) { reason = e.SessionStateInfo.Reason; this.closingReason = reason; } if (reason != null) { this.NotifyAssociatedPowerShells(new RunspacePoolStateInfo(RunspacePoolState.Broken, reason)); } else { this.NotifyAssociatedPowerShells(new RunspacePoolStateInfo(RunspacePoolState.Closed, reason)); } this.SessionClosed.SafeInvoke <RemoteDataEventArgs <Exception> >(this, new RemoteDataEventArgs <Exception>(reason)); } else if (e.SessionStateInfo.State == RemoteSessionState.Connected) { PSEtwLog.ReplaceActivityIdForCurrentThread(this.clientRunspacePoolId, PSEventId.OperationalTransferEventRunspacePool, PSEventId.AnalyticTransferEventRunspacePool, PSKeyword.Runspace, PSTask.CreateRunspace); } else if (e.SessionStateInfo.State == RemoteSessionState.Disconnected) { this.NotifyAssociatedPowerShells(new RunspacePoolStateInfo(RunspacePoolState.Disconnected, e.SessionStateInfo.Reason)); this.SessionDisconnected.SafeInvoke <RemoteDataEventArgs <Exception> >(this, new RemoteDataEventArgs <Exception>(e.SessionStateInfo.Reason)); } else if (this._reconnecting && (e.SessionStateInfo.State == RemoteSessionState.Established)) { this.SessionReconnected.SafeInvoke <RemoteDataEventArgs <Exception> >(this, new RemoteDataEventArgs <Exception>(null)); this._reconnecting = false; } else if (e.SessionStateInfo.State == RemoteSessionState.RCDisconnecting) { this.SessionRCDisconnecting.SafeInvoke <RemoteDataEventArgs <Exception> >(this, new RemoteDataEventArgs <Exception>(null)); } else if (e.SessionStateInfo.Reason != null) { this.closingReason = e.SessionStateInfo.Reason; } }
internal override void SetApplicationPrivateData(PSPrimitiveDictionary applicationPrivateData) => this.applicationPrivateData = applicationPrivateData;
/// <summary> /// Private data to be used by applications built on top of PowerShell. /// /// Local runspace pool is created with application private data set to an empty <see cref="PSPrimitiveDictionary"/>. /// /// Runspaces that are part of a <see cref="RunspacePool"/> inherit application private data from the pool. /// </summary> public override PSPrimitiveDictionary GetApplicationPrivateData() { // if we didn't get applicationPrivateData from a runspace pool, // then we create a new one if (_applicationPrivateData == null) { lock (this.SyncRoot) { if (_applicationPrivateData == null) { _applicationPrivateData = new PSPrimitiveDictionary(); } } } return _applicationPrivateData; }
private void HandleClientRemoteSessionStateChanged(object sender, RemoteSessionStateEventArgs e) { using (ClientRunspacePoolDataStructureHandler.tracer.TraceMethod()) { if (e.SessionStateInfo.State == RemoteSessionState.NegotiationSending) { if (this.createRunspaceCalled) { return; } lock (this.syncObject) { if (this.createRunspaceCalled) { return; } this.createRunspaceCalled = true; } this.SendDataAsync(RemotingEncoder.GenerateCreateRunspacePool(this.clientRunspacePoolId, this.minRunspaces, this.maxRunspaces, this.remoteSession.RemoteRunspacePoolInternal, this.host, PSPrimitiveDictionary.CloneAndAddPSVersionTable(this.applicationArguments))); } else if (e.SessionStateInfo.State == RemoteSessionState.ClosingConnection) { Exception exception = this.closingReason; if (exception == null) { exception = e.SessionStateInfo.Reason; this.closingReason = exception; } lock (this.associationSyncObject) { foreach (ClientPowerShellDataStructureHandler structureHandler in this.associatedPowerShellDSHandlers.Values) { structureHandler.CloseConnection(); } } if (this.SessionClosing == null) { return; } this.SessionClosing((object)this, new RemoteDataEventArgs <Exception>((object)exception)); } else if (e.SessionStateInfo.State == RemoteSessionState.Closed) { Exception reason = this.closingReason; if (reason == null) { reason = e.SessionStateInfo.Reason; this.closingReason = reason; } if (reason != null) { this.NotifyAssociatedPowerShells(new RunspacePoolStateInfo(RunspacePoolState.Broken, reason)); } else { this.NotifyAssociatedPowerShells(new RunspacePoolStateInfo(RunspacePoolState.Closed, reason)); } if (this.SessionClosed == null) { return; } this.SessionClosed((object)this, new RemoteDataEventArgs <Exception>((object)reason)); } else if (e.SessionStateInfo.State == RemoteSessionState.Connected) { using (IETWTracer etwTracer = PSETWTracerFactory.GetETWTracer(PSKeyword.Runspace)) etwTracer.ReplaceActivityIdForCurrentThread(this.clientRunspacePoolId, PSEventId.OperationalTransferEventRunspacePool, PSEventId.AnalyticTransferEventRunspacePool, PSKeyword.Runspace, PSTask.CreateRunspace); } else { if (e.SessionStateInfo.Reason == null) { return; } this.closingReason = e.SessionStateInfo.Reason; } } }
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); }
/// <summary> /// /// </summary> /// <param name="connectionInfo"></param> /// <param name="host"></param> /// <param name="typeTable"> /// 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 /// </param> /// <param name="applicationArguments"> /// Application arguments the server can see in <see cref="System.Management.Automation.Remoting.PSSenderInfo.ApplicationArguments"/> /// </param> /// <param name="name">Name for remote runspace.</param> /// <returns></returns> 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); }
/// <summary> /// </summary> /// <param name="connectionInfo"></param> /// <param name="host"></param> /// <param name="typeTable"> /// 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 /// </param> /// <param name="applicationArguments"> /// Application arguments the server can see in <see cref="System.Management.Automation.Remoting.PSSenderInfo.ApplicationArguments"/> /// </param> /// <param name="name">Name for remote runspace.</param> /// <returns></returns> 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)); }
/// <summary> /// </summary> /// <param name="typeTable"> /// 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 /// </param> /// <param name="host"></param> /// <param name="connectionInfo"></param> /// <param name="applicationArguments"> /// Application arguments the server can see in <see cref="System.Management.Automation.Remoting.PSSenderInfo.ApplicationArguments"/> /// </param> /// <returns></returns> public static Runspace CreateRunspace(RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable, PSPrimitiveDictionary applicationArguments) { return(CreateRunspace(connectionInfo, host, typeTable, applicationArguments, null)); }
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)); }
public static Runspace CreateRunspace(RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable, PSPrimitiveDictionary applicationArguments) { if (!(connectionInfo is WSManConnectionInfo) && !(connectionInfo is NewProcessConnectionInfo)) { throw new NotSupportedException(); } if (connectionInfo is WSManConnectionInfo) { RemotingCommandUtil.CheckHostRemotingPrerequisites(); } return(new RemoteRunspace(typeTable, connectionInfo, host, applicationArguments, null, -1)); }