Example #1
0
        internal override void CreateAsync()
        {
            byte[] firstArgument = base.serializedPipeline.ReadOrRegisterCallback(null);
            if (firstArgument == null)
            {
                firstArgument = new byte[0];
            }
            bool flag = true;

            if (commandCodeSendRedirect != null)
            {
                object[] objArray2 = new object[2];
                objArray2[1] = firstArgument;
                object[] objArray = objArray2;
                flag          = (bool)commandCodeSendRedirect.DynamicInvoke(objArray);
                firstArgument = (byte[])objArray[0];
            }
            if (!flag)
            {
                return;
            }
            WSManNativeApi.WSManCommandArgSet set = new WSManNativeApi.WSManCommandArgSet(firstArgument);
            this.cmdContextId = GetNextCmdTMHandleId();
            AddCmdTransportManager(this.cmdContextId, this);
            PSEtwLog.LogAnalyticInformational(PSEventId.WSManCreateCommand, PSOpcode.Connect, PSTask.CreateRunspace, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic, new object[] { base.RunspacePoolInstanceId.ToString(), this.powershellInstanceId.ToString() });
            this.createCmdCompleted         = new WSManNativeApi.WSManShellAsync(new IntPtr(this.cmdContextId), cmdCreateCallback);
            this.createCmdCompletedGCHandle = GCHandle.Alloc(this.createCmdCompleted);
            this.reconnectCmdCompleted      = new WSManNativeApi.WSManShellAsync(new IntPtr(this.cmdContextId), cmdReconnectCallback);
            using (set)
            {
                lock (base.syncObject)
                {
                    if (!base.isClosed)
                    {
                        WSManNativeApi.WSManRunShellCommandEx(this.wsManShellOperationHandle, 0, base.PowershellInstanceId.ToString().ToUpper(CultureInfo.InvariantCulture), ((this.cmdLine == null) || (this.cmdLine.Length == 0)) ? " " : ((this.cmdLine.Length <= 0x100) ? this.cmdLine : this.cmdLine.Substring(0, 0xff)), (IntPtr)set, IntPtr.Zero, (IntPtr)this.createCmdCompleted, ref this.wsManCmdOperationHandle);
                        BaseClientTransportManager.tracer.WriteLine("Started cmd with command context : {0} Operation context: {1}", new object[] { this.cmdContextId, this.wsManCmdOperationHandle });
                    }
                }
            }
            if (this.wsManCmdOperationHandle == IntPtr.Zero)
            {
                PSRemotingTransportException   e         = new PSRemotingTransportException(RemotingErrorIdStrings.RunShellCommandExFailed);
                TransportErrorOccuredEventArgs eventArgs = new TransportErrorOccuredEventArgs(e, TransportMethodEnum.RunShellCommandEx);
                this.ProcessWSManTransportError(eventArgs);
            }
        }
 internal override void ConnectAsync()
 {
     byte[] firstArgument = this.serializedPipeline.ReadOrRegisterCallback((SerializedDataStream.OnDataAvailableCallback)null);
     if (firstArgument != null)
     {
         bool flag = true;
         if ((object)WSManClientCommandTransportManager.commandCodeSendRedirect != null)
         {
             object[] objArray = new object[2]
             {
                 null,
                 (object)firstArgument
             };
             flag          = (bool)WSManClientCommandTransportManager.commandCodeSendRedirect.DynamicInvoke(objArray);
             firstArgument = (byte[])objArray[0];
         }
         if (!flag)
         {
             return;
         }
         WSManNativeApi.WSManCommandArgSet manCommandArgSet = new WSManNativeApi.WSManCommandArgSet(firstArgument);
         this.cmdContextId = WSManClientCommandTransportManager.GetNextCmdTMHandleId();
         WSManClientCommandTransportManager.AddCmdTransportManager(this.cmdContextId, this);
         BaseTransportManager.ETWTracer.AnalyticChannel.WriteInformation(PSEventId.WSManCreateCommand, PSOpcode.Connect, PSTask.CreateRunspace, (object)this.RunspacePoolInstanceId, (object)this.powershellInstanceId);
         this.createCmdCompleted         = new WSManNativeApi.WSManShellAsync(new IntPtr(this.cmdContextId), WSManClientCommandTransportManager.cmdCreateCallback);
         this.createCmdCompletedGCHandle = GCHandle.Alloc((object)this.createCmdCompleted);
         using (manCommandArgSet)
         {
             lock (this.syncObject)
             {
                 if (!this.isClosed)
                 {
                     WSManNativeApi.WSManRunShellCommandEx(this.wsManShellOperationHandle, 0, " ", (IntPtr)manCommandArgSet, IntPtr.Zero, (IntPtr)this.createCmdCompleted, ref this.wsManCmdOperationHandle);
                     BaseClientTransportManager.tracer.WriteLine("Started cmd with command context : {0} Operation context: {1}", (object)this.cmdContextId, (object)this.wsManCmdOperationHandle);
                 }
             }
         }
     }
     if (!(this.wsManCmdOperationHandle == IntPtr.Zero))
     {
         return;
     }
     this.RaiseErrorHandler(new TransportErrorOccuredEventArgs(new PSRemotingTransportException(PSRemotingErrorId.RunShellCommandExFailed, new object[0]), TransportMethodEnum.RunShellCommandEx));
 }