protected void Start(string initialCommand, PSRemotingCryptoHelperServer cryptoHelper, string workingDirectory = null, string configurationName = null) { _initialCommand = initialCommand; sessionTM = CreateSessionTransportManager(configurationName, cryptoHelper, workingDirectory); try { do { string data = originalStdIn.ReadLine(); lock (_syncObject) { if (sessionTM == null) { sessionTM = CreateSessionTransportManager(configurationName, cryptoHelper, workingDirectory); } } if (string.IsNullOrEmpty(data)) { lock (_syncObject) { // give a chance to runspace/pipelines to close (as it looks like the client died // intermittently) sessionTM.Close(null); sessionTM = null; } throw new PSRemotingTransportException( PSRemotingErrorId.IPCUnknownElementReceived, RemotingErrorIdStrings.IPCUnknownElementReceived, string.Empty); } // process data in a thread pool thread..this way Runspace, Command // data can be processed concurrently. #if !UNIX Utils.QueueWorkItemWithImpersonation( _windowsIdentityToImpersonate, new WaitCallback(ProcessingThreadStart), data); #else ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessingThreadStart), data); #endif }while (true); } catch (Exception e) { PSEtwLog.LogOperationalError( PSEventId.TransportError, PSOpcode.Open, PSTask.None, PSKeyword.UseAlwaysOperational, Guid.Empty.ToString(), Guid.Empty.ToString(), OutOfProcessUtils.EXITCODE_UNHANDLED_EXCEPTION, e.Message, e.StackTrace); PSEtwLog.LogAnalyticError( PSEventId.TransportError_Analytic, PSOpcode.Open, PSTask.None, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic, Guid.Empty.ToString(), Guid.Empty.ToString(), OutOfProcessUtils.EXITCODE_UNHANDLED_EXCEPTION, e.Message, e.StackTrace); if (_exitProcessOnError) { // notify the remote client of any errors and fail gracefully originalStdErr.WriteLine(e.Message); Environment.Exit(OutOfProcessUtils.EXITCODE_UNHANDLED_EXCEPTION); } } }
protected OutOfProcessServerSessionTransportManager CreateSessionTransportManager(string configurationName, PSRemotingCryptoHelperServer cryptoHelper, string workingDirectory) { PSSenderInfo senderInfo; #if !UNIX WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent(); PSPrincipal userPrincipal = new PSPrincipal( new PSIdentity(string.Empty, true, currentIdentity.Name, null), currentIdentity); senderInfo = new PSSenderInfo(userPrincipal, "http://localhost"); #else PSPrincipal userPrincipal = new PSPrincipal( new PSIdentity(string.Empty, true, string.Empty, null), null); senderInfo = new PSSenderInfo(userPrincipal, "http://localhost"); #endif OutOfProcessServerSessionTransportManager tm = new OutOfProcessServerSessionTransportManager(originalStdOut, originalStdErr, cryptoHelper); ServerRemoteSession.CreateServerRemoteSession( senderInfo, _initialCommand, tm, configurationName, workingDirectory); return(tm); }