Example #1
10
        internal override void Execute(ExecutionContext context, ICommandRuntime commandRuntime)
        {
            if ((lValue is NumberNode) && (rValue is NumberNode))
            {
                int lNum = (int)((NumberNode) lValue).GetValue(context);
                int rNum = (int)((NumberNode) rValue).GetValue(context);

                if (lNum <= rNum)
                {
                    for(; lNum <= rNum; lNum++)
                    {
                        commandRuntime.WriteObject(lNum);
                    }
                }
                else
                {
                    for(;lNum >= rNum; lNum--)
                    {
                        commandRuntime.WriteObject(lNum);
                    }
                }

                return;
            }

            throw new InvalidOperationException("Can't execute 'range operator' for non-number values");
        }
Example #2
0
 public AzModule(ICommandRuntime runtime, IEventStore store, TelemetryProvider provider)
 {
     _deferredEvents = store;
     _runtime        = runtime;
     _logger         = new AdalLogger(_deferredEvents.GetDebugLogger());;
     _telemetry      = provider;
 }
Example #3
0
        internal override void Execute(Pash.Implementation.ExecutionContext context, ICommandRuntime commandRuntime)
        {
            ExecutionContext nestedContext = context.CreateNestedContext();

            if (!(context.CurrentRunspace is LocalRunspace))
            {
                throw new InvalidOperationException("Invalid context");
            }

            // MUST: fix this with the commandRuntime
            Pipeline pipeline = context.CurrentRunspace.CreateNestedPipeline();

            context.PushPipeline(pipeline);

            try
            {
                Command cmd = new Command("Get-Variable");
                cmd.Parameters.Add("Name", new string[] { Text });
                // TODO: implement command invoke
                pipeline.Commands.Add(cmd);

                commandRuntime.WriteObject(pipeline.Invoke(), true);
                //context.outputStreamWriter.Write(pipeline.Invoke(), true);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                context.PopPipeline();
            }
        }
Example #4
0
        public void Start(string[] commandLineArguments, ICommandRuntime commandRuntime, OptionSet commonOptions)
        {
            string withoutExtension = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().FullLocalPath());
            string name             = commandLineArguments.Length > 0 ? commandLineArguments[0] : null;

            if (string.IsNullOrEmpty(name))
            {
                PrintGeneralHelp(withoutExtension);
            }
            else
            {
                Lazy <ICommand, CommandMetadata> lazy = _commands.Find(name);
                if (lazy == null)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Command '{0}' is not supported", name);
                    Console.ResetColor();
                    PrintGeneralHelp(withoutExtension);
                }
                else
                {
                    PrintCommandHelp(withoutExtension, lazy.Value, lazy.Metadata, commonOptions);
                }
            }
        }
 private void Begin(bool expectInput, ICommandRuntime commandRuntime)
 {
     try
     {
         this._pipeline.ExecutionScope = this._context.EngineSessionState.CurrentScope;
         this._context.PushPipelineProcessor(this._pipeline);
         this._expectInput = expectInput;
         if (commandRuntime is MshCommandRuntime mshCommandRuntime)
         {
             if (mshCommandRuntime.OutputPipe != null)
             {
                 this._pipeline.LinkPipelineSuccessOutput(mshCommandRuntime.OutputPipe);
             }
             if (mshCommandRuntime.ErrorOutputPipe != null)
             {
                 this._pipeline.LinkPipelineErrorOutput(mshCommandRuntime.ErrorOutputPipe);
             }
         }
         this._pipeline.StartStepping(this._expectInput);
     }
     finally
     {
         this._context.PopPipelineProcessor();
     }
 }
Example #6
0
        internal override void Execute(ExecutionContext context, ICommandRuntime commandRuntime)
        {
            ExecutionContext nestedContext = context.CreateNestedContext();

            if (lValue is VariableNode)
            {
                VariableNode varNode = (VariableNode)lValue;

                if (! (context.CurrentRunspace is LocalRunspace))
                    throw new InvalidOperationException("Invalid context");

                // MUST: fix this with the commandRuntime
                Pipeline pipeline = context.CurrentRunspace.CreateNestedPipeline();
                context.PushPipeline(pipeline);

                try
                {
                    Command cmd = new Command("Set-Variable");
                    cmd.Parameters.Add("Name", new string[] { varNode.Text });
                    cmd.Parameters.Add("Value", rValue.GetValue(context));
                    // TODO: implement command invoke
                    pipeline.Commands.Add(cmd);
                    pipeline.Invoke();
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    context.PopPipeline();
                }
            }
        }
Example #7
0
        private void Start(ICommandRuntime commandRuntime)
        {
            _log.Trace("Creating the Autofac container");
            _container = BuildContainer();
            RegisterAdditionalModules();
            ICommandLocator commandLocator = _container.Resolve <ICommandLocator>();
            string          commandName    = ExtractCommandName(ref _commandLineArguments);

            _log.TraceFormat("Finding the command: {0}", commandName);
            Lazy <ICommand, CommandMetadata> lazy = commandLocator.Find(commandName);

            if (lazy == null)
            {
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    lazy = commandLocator.Find("run");
                }
                else
                {
                    lazy = commandLocator.Find("help");
                    Environment.ExitCode = -1;
                }
            }
            _commandInstance = lazy.Value;
            _log.TraceFormat("Executing command: {0}", _commandInstance.GetType().Name);
            _commandInstance.Start(_commandLineArguments, commandRuntime, CommonOptions);
        }
Example #8
0
        internal override void Execute(Pash.Implementation.ExecutionContext context, ICommandRuntime commandRuntime)
        {
            ExecutionContext nestedContext = context.CreateNestedContext();

            if (! (context.CurrentRunspace is LocalRunspace))
                throw new InvalidOperationException("Invalid context");

            // MUST: fix this with the commandRuntime
            Pipeline pipeline = context.CurrentRunspace.CreateNestedPipeline();
            context.PushPipeline(pipeline);

            try
            {
                Command cmd = new Command("Get-Variable");
                cmd.Parameters.Add("Name", new string[] { Text });
                // TODO: implement command invoke
                pipeline.Commands.Add(cmd);

                commandRuntime.WriteObject(pipeline.Invoke(), true);
                //context.outputStreamWriter.Write(pipeline.Invoke(), true);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                context.PopPipeline();
            }
        }
Example #9
0
 public NetworkClient(AzureProfile profile, AzureSubscription subscription, ICommandRuntime commandRuntime)
     : this(CreateClient <NetworkManagementClient>(profile, subscription),
            CreateClient <ComputeManagementClient>(profile, subscription),
            CreateClient <ManagementClient>(profile, subscription),
            commandRuntime)
 {
 }
Example #10
0
        internal override void Execute(ExecutionContext context, ICommandRuntime commandRuntime)
        {
            // TODO: rewrite this - it should expand the commands in the original pipe

            PipelineCommandRuntime subRuntime = null;

            foreach (ASTNode node in Pipeline)
            {
                ExecutionContext subContext = context.CreateNestedContext();

                if (subRuntime == null)
                {
                    subContext.inputStreamReader = context.inputStreamReader;
                }
                else
                {
                    subContext.inputStreamReader = new PSObjectPipelineReader(subRuntime.outputResults);
                }

                subRuntime = new PipelineCommandRuntime(((PipelineCommandRuntime)commandRuntime).pipelineProcessor);
                subContext.inputStreamReader = subContext.inputStreamReader;

                node.Execute(subContext, subRuntime);
            }
        }
Example #11
0
 public NetworkClient(INetworkManagementClient client, IComputeManagementClient computeClient, IManagementClient managementClient, ICommandRuntime commandRuntime)
 {
     this.client           = client;
     this.computeClient    = computeClient;
     this.managementClient = managementClient;
     this.commandRuntime   = commandRuntime;
 }
Example #12
0
 private void Begin(bool expectInput, ICommandRuntime commandRuntime)
 {
     try
     {
         this._pipeline.ExecutionScope = this._context.EngineSessionState.CurrentScope;
         this._context.PushPipelineProcessor(this._pipeline);
         this._expectInput = expectInput;
         MshCommandRuntime runtime = commandRuntime as MshCommandRuntime;
         if (runtime != null)
         {
             if (runtime.OutputPipe != null)
             {
                 this._pipeline.LinkPipelineSuccessOutput(runtime.OutputPipe);
             }
             if (runtime.ErrorOutputPipe != null)
             {
                 this._pipeline.LinkPipelineErrorOutput(runtime.ErrorOutputPipe);
             }
         }
         this._pipeline.StartStepping(this._expectInput);
     }
     finally
     {
         this._context.PopPipelineProcessor(true);
     }
 }
Example #13
0
        internal override void Execute(ExecutionContext context, ICommandRuntime commandRuntime)
        {
            if ((lValue is NumberNode) && (rValue is NumberNode))
            {
                int lNum = (int)((NumberNode)lValue).GetValue(context);
                int rNum = (int)((NumberNode)rValue).GetValue(context);

                if (lNum <= rNum)
                {
                    for (; lNum <= rNum; lNum++)
                    {
                        commandRuntime.WriteObject(lNum);
                    }
                }
                else
                {
                    for (; lNum >= rNum; lNum--)
                    {
                        commandRuntime.WriteObject(lNum);
                    }
                }

                return;
            }

            throw new InvalidOperationException("Can't execute 'range operator' for non-number values");
        }
Example #14
0
        /// <summary>
        /// Retrieve the pipeline input of a specified cmdlet.
        /// </summary>
        /// <param name="commandRuntime">The runtime of the cmdlet whose pipeline should be retrieved.</param>
        /// <param name="cmdlet">The cmdlet whose pipeline should be retrieved.</param>
        /// <returns></returns>
        private static Pipeline GetCmdletPipelineInput(ICommandRuntime commandRuntime, InternalCommand cmdlet)
        {
            var inputPipe  = commandRuntime.GetInternalProperty("InputPipe");
            var enumerator = inputPipe.GetInternalField("_enumeratorToProcess");

            var currentPS = (PSObject)cmdlet.GetInternalProperty("CurrentPipelineObject");

            var current = PSObjectToString(currentPS) == string.Empty || currentPS == null ? null : currentPS.BaseObject;

            if (enumerator == null) //Piping from a cmdlet
            {
                if (current == null)
                {
                    return(null);
                }

                return(new Pipeline(current, new List <object> {
                    current
                }));
            }
            else //Piping from a variable
            {
                var declaringType  = enumerator.GetType().DeclaringType;
                var enumeratorType = enumerator.GetType();

                IEnumerable <object> list;

                if (declaringType == typeof(Array) || enumeratorType.Name == "SZArrayEnumerator") //It's a SZArrayEnumerator (piping straight from a variable). In .NET Core 3.1 SZArrayEnumerator is no longer nested
                {
                    list = ((object[])enumerator.GetInternalField("_array"));
                }
                else if (declaringType == typeof(List <>)) //It's a List<T>.Enumerator (piping from $groups[0].Group)
                {
                    list = enumerator.PSGetInternalField("_list", "list", null).ToIEnumerable();
                }
                else if (enumeratorType.IsGenericType && enumeratorType.GetGenericTypeDefinition() == typeof(ReadOnlyListEnumerator <>))
                {
                    list = enumerator.GetInternalField("list").ToIEnumerable();
                }
                else
                {
                    throw new NotImplementedException($"Don't know how to extract the pipeline input from a '{enumeratorType}' enumerator from type '{declaringType}'.");
                }

                list = list.Select(o =>
                {
                    var pso = o as PSObject;

                    if (pso != null)
                    {
                        return(pso.BaseObject);
                    }

                    return(o);
                });

                return(new Pipeline(current, list.ToList()));
            }
        }
        internal override void Execute(ExecutionContext context, ICommandRuntime commandRuntime)
        {
            // TODO: sum the values in both pipelines
            // TODO: if there are more than one value in the left - just copy left results and then the right results to the resulting pipe
            // TODO: if there is only one value on the left - convert the value on the right to the type of the left and then Sum

            commandRuntime.WriteObject(GetValue(context));
        }
Example #16
0
        internal override void Execute(ExecutionContext context, ICommandRuntime commandRuntime)
        {
            // TODO: sum the values in both pipelines
            // TODO: if there are more than one value in the left - just copy left results and then the right results to the resulting pipe
            // TODO: if there is only one value on the left - convert the value on the right to the type of the left and then Sum

            commandRuntime.WriteObject(GetValue(context));
        }
Example #17
0
 public AzModule(ICommandRuntime runtime, IEventStore eventHandler)
 {
     _runtime        = runtime;
     _deferredEvents = eventHandler;
     _logger         = new AdalLogger(_deferredEvents.GetDebugLogger());
     _telemetry      = TelemetryProvider.Create(
         _deferredEvents.GetWarningLogger(), _deferredEvents.GetDebugLogger());
 }
Example #18
0
 public AzModule(ICommandRuntime runtime)
 {
     _runtime         = runtime;
     _telemetryEvents = new Dictionary <string, AzurePSQoSEvent>(StringComparer.OrdinalIgnoreCase);
     _warningMessages = new ConcurrentQueue <string>();
     _debugMessages   = new ConcurrentQueue <string>();
     _logger          = new AdalLogger((message) => _debugMessages.CheckAndEnqueue(message));
     _metricHelper    = TelemetryProvider.Create((message) => _warningMessages.CheckAndEnqueue(message), (message) => _debugMessages.CheckAndEnqueue(message));
 }
Example #19
0
        public PSHostUserInterfaceTransformationLogger(ICommandRuntime commandRuntime)
        {
            if (commandRuntime == null)
            {
                throw new ArgumentNullException("commandRuntime", "PowerShell command runtime object required.");
            }

            _commandRuntime = commandRuntime;
        }
Example #20
0
 void ICommand.Start(string[] commandLineArguments, ICommandRuntime commandRuntime, OptionSet commonOptions)
 {
     Runtime = commandRuntime;
     UnrecognizedArguments(_options.Parse(commandLineArguments));
     foreach (ICommandOptions commandOptions in _optionSets)
     {
         commandOptions.Validate();
     }
     Start();
 }
Example #21
0
        public void Begin(bool expectInput, EngineIntrinsics contextToRedirectTo)
        {
            if (contextToRedirectTo == null)
            {
                throw new ArgumentNullException(nameof(contextToRedirectTo));
            }
            CommandProcessorBase commandProcessor = contextToRedirectTo.SessionState.Internal.ExecutionContext.CurrentCommandProcessor;
            ICommandRuntime      commandRuntime   = commandProcessor == null ? (ICommandRuntime)null : (ICommandRuntime)commandProcessor.CommandRuntime;

            this.Begin(expectInput, commandRuntime);
        }
Example #22
0
        internal ArrayList GetResults()
        {
            if (this.commandRuntime != null)
            {
                throw new InvalidOperationException();
            }
            if (this is PSCmdlet)
            {
                throw new InvalidOperationException(ResourceManagerCache.GetResourceString("CommandBaseStrings", "CannotInvokePSCmdletsDirectly"));
            }
            ArrayList outputArrayList = new ArrayList();

            this.CommandRuntime = (ICommandRuntime) new DefaultCommandRuntime(outputArrayList);
            this.BeginProcessing();
            this.ProcessRecord();
            this.EndProcessing();
            return(outputArrayList);
        }
Example #23
0
        internal override void Execute(ExecutionContext context, ICommandRuntime commandRuntime)
        {
            if (!(context.CurrentRunspace is LocalRunspace))
            {
                throw new InvalidOperationException(string.Format("Command \"{0}\" was not found.", CmdletName));
            }

            CommandManager cmdMgr = ((LocalRunspace)context.CurrentRunspace).CommandManager;

            CommandInfo cmdInfo = cmdMgr.FindCommand(CmdletName);

            if (cmdInfo == null)
            {
                throw new InvalidOperationException(string.Format("Command \"{0}\" was not found.", CmdletName));
            }

            // MUST: fix this with the commandRuntime
            Pipeline pipeline = context.CurrentRunspace.CreateNestedPipeline();

            context.PushPipeline(pipeline);

            try
            {
                // TODO: implement command invoke

                Command command = new Command(CmdletName);
                foreach (string param in Params.Params)
                {
                    command.Parameters.Add(null, param);
                }
                pipeline.Commands.Add(command);
                _results = pipeline.Invoke();

                commandRuntime.WriteObject(_results, true);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                context.PopPipeline();
            }
        }
Example #24
0
        internal override void Execute(ExecutionContext context, ICommandRuntime commandRuntime)
        {
            if (!(context.CurrentRunspace is LocalRunspace))
                throw new InvalidOperationException(string.Format("Command \"{0}\" was not found.", CmdletName));

            CommandManager cmdMgr = ((LocalRunspace)context.CurrentRunspace).CommandManager;

            CommandInfo cmdInfo = cmdMgr.FindCommand(CmdletName);

            if (cmdInfo == null)
                throw new InvalidOperationException(string.Format("Command \"{0}\" was not found.", CmdletName));

            // MUST: fix this with the commandRuntime
            Pipeline pipeline = context.CurrentRunspace.CreateNestedPipeline();
            context.PushPipeline(pipeline);

            try
            {
                // TODO: implement command invoke

                Command command = new Command(CmdletName);
                foreach (string param in Params.Params)
                {
                    command.Parameters.Add(null, param);
                }
                pipeline.Commands.Add(command);
                _results = pipeline.Invoke();

                commandRuntime.WriteObject(_results, true);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                context.PopPipeline();
            }
        }
Example #25
0
        /// <summary>
        /// Retrieve the pipeline input of a specified cmdlet.
        /// </summary>
        /// <param name="commandRuntime">The runtime of the cmdlet whose pipeline should be retrieved.</param>
        /// <param name="cmdlet">The cmdlet whose pipeline should be retrieved.</param>
        /// <returns></returns>
        private static Pipeline GetCmdletPipelineInput(ICommandRuntime commandRuntime, InternalCommand cmdlet)
        {
            var inputPipe  = commandRuntime.GetInternalProperty("InputPipe");
            var enumerator = inputPipe.GetInternalField("_enumeratorToProcess");

            var currentPS = (PSObject)cmdlet.GetInternalProperty("CurrentPipelineObject");

            var current = PSObjectToString(currentPS) == string.Empty || currentPS == null ? null : currentPS.BaseObject;

            if (enumerator == null) //Piping from a cmdlet
            {
                if (current == null)
                {
                    return(null);
                }

                return(new Pipeline(current, new List <object> {
                    current
                }));
            }
            else //Piping from a variable
            {
                var array = ((object[])enumerator.GetInternalField("_array")).Select(o =>
                {
                    if (o is PSObject)
                    {
                        return(o);
                    }
                    else
                    {
                        return(new PSObject(o));
                    }
                }).Cast <PSObject>();

                return(new Pipeline(current, array.Select(e => e.BaseObject).ToList()));
            }
        }
Example #26
0
        internal override void Execute(ExecutionContext context, ICommandRuntime commandRuntime)
        {
            ExecutionContext nestedContext = context.CreateNestedContext();

            if (lValue is VariableNode)
            {
                VariableNode varNode = (VariableNode)lValue;

                if (!(context.CurrentRunspace is LocalRunspace))
                {
                    throw new InvalidOperationException("Invalid context");
                }

                // MUST: fix this with the commandRuntime
                Pipeline pipeline = context.CurrentRunspace.CreateNestedPipeline();
                context.PushPipeline(pipeline);

                try
                {
                    Command cmd = new Command("Set-Variable");
                    cmd.Parameters.Add("Name", new string[] { varNode.Text });
                    cmd.Parameters.Add("Value", rValue.GetValue(context));
                    // TODO: implement command invoke
                    pipeline.Commands.Add(cmd);
                    pipeline.Invoke();
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    context.PopPipeline();
                }
            }
        }
Example #27
0
        internal override void Execute(ExecutionContext context, ICommandRuntime commandRuntime)
        {
            // TODO: rewrite this - it should expand the commands in the original pipe

            PipelineCommandRuntime subRuntime = null;

            foreach (ASTNode node in Pipeline)
            {
                ExecutionContext subContext = context.CreateNestedContext();

                if (subRuntime == null)
                {
                    subContext.inputStreamReader = context.inputStreamReader;
                }
                else
                {
                    subContext.inputStreamReader = new PSObjectPipelineReader(subRuntime.outputResults);
                }

                subRuntime = new PipelineCommandRuntime(((PipelineCommandRuntime)commandRuntime).pipelineProcessor);
                subContext.inputStreamReader = subContext.inputStreamReader;

                node.Execute(subContext, subRuntime);
            }
        }
 public GetDeploymentStatus(IServiceManagement channel, ICommandRuntime commandRuntime)
 {
     Channel = channel;
     CommandRuntime = commandRuntime;
 }
Example #29
0
 internal override void Execute(ExecutionContext context, ICommandRuntime commandRuntime)
 {
     context.outputStreamWriter.Write(GetValue(context), true);
 }
Example #30
0
 internal override void Execute(ExecutionContext context, ICommandRuntime commandRuntime)
 {
     throw new NotImplementedException();
 }
 public GetDeploymentStatus(ICommandRuntime commandRuntime)
 {
     CommandRuntime = commandRuntime;
 }
Example #32
0
        internal override void Execute(ExecutionContext context, ICommandRuntime commandRuntime)
        {
            context.outputStreamWriter.Write(GetValue(context));

            // TODO: extract the value to the pipeline
        }
Example #33
0
 public AzModule(ICommandRuntime runtime) : this(runtime, new EventStore())
 {
 }
Example #34
0
 internal override void Execute(ExecutionContext context, ICommandRuntime commandRuntime)
 {
     throw new NotImplementedException();
 }
Example #35
0
        internal override void Execute(ExecutionContext context, ICommandRuntime commandRuntime)
        {
            context.outputStreamWriter.Write(GetValue(context));

            // TODO: extract the value to the pipeline
        }
Example #36
0
 internal override void Execute(ExecutionContext context, ICommandRuntime commandRuntime)
 {
     commandRuntime.WriteObject(GetValue(context), true);
 }
Example #37
0
 public AzModule(ICommandRuntime runtime)
 {
     _runtime = runtime;
 }
Example #38
0
 internal override void Execute(ExecutionContext context, ICommandRuntime commandRuntime)
 {
     context.outputStreamWriter.Write(GetValue(context));
 }
Example #39
0
 internal override void Execute(ExecutionContext context, ICommandRuntime commandRuntime)
 {
     commandRuntime.WriteObject(GetValue(context), true);
 }
 public static void SetCommandRuntimeMock(this PSCmdlet cmdlet, ICommandRuntime value)
 {
     var property = GetInternalProperty("CommandRuntime", typeof(ICommandRuntime));
     property.SetValue(cmdlet, value);
 }
Example #41
0
 public NetworkClient(AzureSubscription subscription, ICommandRuntime commandRuntime)
     : this(CreateClient <NetworkManagementClient>(subscription),
            CreateClient <ManagementClient>(subscription),
            commandRuntime)
 {
 }
        public static void SetCommandRuntimeMock(this PSCmdlet cmdlet, ICommandRuntime value)
        {
            var property = GetInternalProperty("CommandRuntime", typeof(ICommandRuntime));

            property.SetValue(cmdlet, value);
        }