// Create a new command in the shell context.
        internal void CreateCommand(
            IntPtr pluginContext,
            WSManNativeApi.WSManPluginRequest requestDetails,
            int flags,
            string commandLine,
            WSManNativeApi.WSManCommandArgSet arguments)
        {
            try
            {
                // inbound cmd information is already verified.. so no need to verify here.
                WSManPluginCommandTransportManager serverCmdTransportMgr = new WSManPluginCommandTransportManager(transportMgr);
                serverCmdTransportMgr.Initialize();

                // Apply quota limits on the command transport manager
                _remoteSession.ApplyQuotaOnCommandTransportManager(serverCmdTransportMgr);

                WSManPluginCommandSession mgdCmdSession = new WSManPluginCommandSession(requestDetails, serverCmdTransportMgr, _remoteSession);
                AddToActiveCmdSessions(mgdCmdSession);
                mgdCmdSession.SessionClosed += new EventHandler <EventArgs>(this.HandleCommandSessionClosed);

                mgdCmdSession.shutDownContext = new WSManPluginOperationShutdownContext(
                    pluginContext,
                    creationRequestDetails.unmanagedHandle,
                    mgdCmdSession.creationRequestDetails.unmanagedHandle,
                    false);

                do
                {
                    if (!mgdCmdSession.ProcessArguments(arguments))
                    {
                        WSManPluginInstance.ReportOperationComplete(
                            requestDetails,
                            WSManPluginErrorCodes.InvalidArgSet,
                            StringUtil.Format(
                                RemotingErrorIdStrings.WSManPluginInvalidArgSet,
                                "WSManPluginCommand"));
                        break;
                    }

                    // Report plugin context to WSMan
                    mgdCmdSession.ReportContext();
                } while (false);
            }
            catch (System.Exception e)
            {
                CommandProcessorBase.CheckForSevereException(e);

                // if there is an exception creating remote session send the message to client.
                WSManPluginInstance.ReportOperationComplete(
                    requestDetails,
                    WSManPluginErrorCodes.ManagedException,
                    StringUtil.Format(
                        RemotingErrorIdStrings.WSManPluginManagedException,
                        e.Message));
            }
        }
        internal bool ProcessArguments(
            WSManNativeApi.WSManCommandArgSet arguments)
        {
            if (1 != arguments.argsCount)
            {
                return(false);
            }

            System.Byte[] convertedBase64 = Convert.FromBase64String(arguments.args[0]);
            transportMgr.ProcessRawData(convertedBase64, WSManPluginConstants.SupportedInputStream);

            return(true);
        }
Ejemplo n.º 3
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));
 }
Ejemplo n.º 5
0
        /// <summary>
        /// 
        /// </summary>
        /// <exception cref="PSRemotingTransportException">
        /// WSManRunShellCommandEx failed.
        /// </exception>
        internal override void CreateAsync()
        {
            byte[] cmdPart1 = serializedPipeline.ReadOrRegisterCallback(null);
            if (null != cmdPart1)
            {
                #region SHIM: Redirection code for command code send.

                bool sendContinue = true;

                if (s_commandCodeSendRedirect != null)
                {
                    object[] arguments = new object[2] { null, cmdPart1 };
                    sendContinue = (bool)s_commandCodeSendRedirect.DynamicInvoke(arguments);
                    cmdPart1 = (byte[])arguments[0];
                }

                if (!sendContinue)
                    return;

                #endregion

                WSManNativeApi.WSManCommandArgSet argSet = new WSManNativeApi.WSManCommandArgSet(cmdPart1);

                // create cmdContextId
                _cmdContextId = GetNextCmdTMHandleId();
                AddCmdTransportManager(_cmdContextId, this);

                PSEtwLog.LogAnalyticInformational(PSEventId.WSManCreateCommand, PSOpcode.Connect,
                                PSTask.CreateRunspace, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic,
                                RunspacePoolInstanceId.ToString(), powershellInstanceId.ToString());

                _createCmdCompleted = new WSManNativeApi.WSManShellAsync(new IntPtr(_cmdContextId), s_cmdCreateCallback);
                _createCmdCompletedGCHandle = GCHandle.Alloc(_createCmdCompleted);
                _reconnectCmdCompleted = new WSManNativeApi.WSManShellAsync(new IntPtr(_cmdContextId), s_cmdReconnectCallback);

                using (argSet)
                {
                    lock (syncObject)
                    {
                        if (!isClosed)
                        {
                            WSManNativeApi.WSManRunShellCommandEx(_wsManShellOperationHandle,
                                0,
                                PowershellInstanceId.ToString().ToUpperInvariant(),
                                // WSManRunsShellCommand doesn't accept empty string "".
                                (_cmdLine == null || _cmdLine.Length == 0) ? " " : (_cmdLine.Length <= 256 ? _cmdLine : _cmdLine.Substring(0, 255)),
                                argSet,
                                IntPtr.Zero,
                                _createCmdCompleted,
                                ref _wsManCmdOperationHandle);

                            tracer.WriteLine("Started cmd with command context : {0} Operation context: {1}", _cmdContextId, _wsManCmdOperationHandle);
                        }
                    }
                }
            }

            if (_wsManCmdOperationHandle == IntPtr.Zero)
            {
                PSRemotingTransportException e = new PSRemotingTransportException(RemotingErrorIdStrings.RunShellCommandExFailed);
                TransportErrorOccuredEventArgs eventargs =
                    new TransportErrorOccuredEventArgs(e, TransportMethodEnum.RunShellCommandEx);
                ProcessWSManTransportError(eventargs);
                return;
            }
        }
        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);
            }
        }