Esempio n. 1
0
        private static object InvokeScript(string methodName, ScriptBlock script, object @this, object[] arguments)
        {
            object obj2;

            try
            {
                obj2 = script.DoInvokeReturnAsIs(true, ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe, AutomationNull.Value, AutomationNull.Value, @this, arguments);
            }
            catch (SessionStateOverflowException exception)
            {
                throw new MethodInvocationException("ScriptMethodSessionStateOverflowException", exception, ExtendedTypeSystem.MethodInvocationException, new object[] { methodName, arguments.Length, exception.Message });
            }
            catch (RuntimeException exception2)
            {
                throw new MethodInvocationException("ScriptMethodRuntimeException", exception2, ExtendedTypeSystem.MethodInvocationException, new object[] { methodName, arguments.Length, exception2.Message });
            }
            catch (TerminateException)
            {
                throw;
            }
            catch (FlowControlException exception3)
            {
                throw new MethodInvocationException("ScriptMethodFlowControlException", exception3, ExtendedTypeSystem.MethodInvocationException, new object[] { methodName, arguments.Length, exception3.Message });
            }
            catch (PSInvalidOperationException exception4)
            {
                throw new MethodInvocationException("ScriptMethodInvalidOperationException", exception4, ExtendedTypeSystem.MethodInvocationException, new object[] { methodName, arguments.Length, exception4.Message });
            }
            return(obj2);
        }
Esempio n. 2
0
 private static object InvokeScript(string methodName, ScriptBlock script, object @this, object[] arguments)
 {
     object obj2;
     try
     {
         obj2 = script.DoInvokeReturnAsIs(true, ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe, AutomationNull.Value, AutomationNull.Value, @this, arguments);
     }
     catch (SessionStateOverflowException exception)
     {
         throw new MethodInvocationException("ScriptMethodSessionStateOverflowException", exception, ExtendedTypeSystem.MethodInvocationException, new object[] { methodName, arguments.Length, exception.Message });
     }
     catch (RuntimeException exception2)
     {
         throw new MethodInvocationException("ScriptMethodRuntimeException", exception2, ExtendedTypeSystem.MethodInvocationException, new object[] { methodName, arguments.Length, exception2.Message });
     }
     catch (TerminateException)
     {
         throw;
     }
     catch (FlowControlException exception3)
     {
         throw new MethodInvocationException("ScriptMethodFlowControlException", exception3, ExtendedTypeSystem.MethodInvocationException, new object[] { methodName, arguments.Length, exception3.Message });
     }
     catch (PSInvalidOperationException exception4)
     {
         throw new MethodInvocationException("ScriptMethodInvalidOperationException", exception4, ExtendedTypeSystem.MethodInvocationException, new object[] { methodName, arguments.Length, exception4.Message });
     }
     return obj2;
 }
        private Collection <PSObject> InvokeScript(ScriptBlock sb, bool useNewScope, PipelineResultTypes writeToPipeline, IList input, params object[] args)
        {
            object obj2;

            if (this._cmdlet != null)
            {
                this._cmdlet.ThrowIfStopping();
            }
            Cmdlet contextCmdlet = null;

            ScriptBlock.ErrorHandlingBehavior writeToExternalErrorPipe = ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe;
            if ((writeToPipeline & PipelineResultTypes.Output) == PipelineResultTypes.Output)
            {
                contextCmdlet    = this._cmdlet;
                writeToPipeline &= ~PipelineResultTypes.Output;
            }
            if ((writeToPipeline & PipelineResultTypes.Error) == PipelineResultTypes.Error)
            {
                writeToExternalErrorPipe = ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe;
                writeToPipeline         &= ~PipelineResultTypes.Error;
            }
            if (writeToPipeline != PipelineResultTypes.None)
            {
                throw PSTraceSource.NewNotImplementedException();
            }
            if (contextCmdlet != null)
            {
                sb.InvokeUsingCmdlet(contextCmdlet, useNewScope, writeToExternalErrorPipe, AutomationNull.Value, input, AutomationNull.Value, args);
                obj2 = AutomationNull.Value;
            }
            else
            {
                obj2 = sb.DoInvokeReturnAsIs(useNewScope, writeToExternalErrorPipe, AutomationNull.Value, input, AutomationNull.Value, args);
            }
            if (obj2 == AutomationNull.Value)
            {
                return(new Collection <PSObject>());
            }
            Collection <PSObject> collection = obj2 as Collection <PSObject>;

            if (collection == null)
            {
                collection = new Collection <PSObject>();
                IEnumerator enumerator = null;
                enumerator = LanguagePrimitives.GetEnumerator(obj2);
                if (enumerator != null)
                {
                    while (enumerator.MoveNext())
                    {
                        object current = enumerator.Current;
                        collection.Add(LanguagePrimitives.AsPSObjectOrNull(current));
                    }
                    return(collection);
                }
                collection.Add(LanguagePrimitives.AsPSObjectOrNull(obj2));
            }
            return(collection);
        }
Esempio n. 4
0
        private static object SplitWithPredicate(System.Management.Automation.ExecutionContext context, IScriptExtent errorPosition, IEnumerable <string> content, ScriptBlock predicate, int limit)
        {
            List <string> list = new List <string>();

            foreach (string str in content)
            {
                List <string> items = new List <string>();
                if (limit == 1)
                {
                    list.Add(str);
                    continue;
                }
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < str.Length; i++)
                {
                    if (LanguagePrimitives.IsTrue(predicate.DoInvokeReturnAsIs(true, ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe, CharToString(str[i]), AutomationNull.Value, AutomationNull.Value, new object[] { str, i })))
                    {
                        items.Add(builder.ToString());
                        builder = new StringBuilder();
                        if ((limit > 0) && (items.Count >= (limit - 1)))
                        {
                            if ((i + 1) < str.Length)
                            {
                                items.Add(str.Substring(i + 1));
                            }
                            else
                            {
                                items.Add("");
                            }
                            break;
                        }
                        if (i == (str.Length - 1))
                        {
                            items.Add("");
                        }
                    }
                    else
                    {
                        builder.Append(str[i]);
                    }
                }
                if ((builder.Length > 0) && ((limit <= 0) || (items.Count < limit)))
                {
                    items.Add(builder.ToString());
                }
                ExtendList <string>(list, items);
            }
            return(list.ToArray());
        }
Esempio n. 5
0
        private Collection <PSObject> InvokeScript(ScriptBlock sb, bool useNewScope,
                                                   PipelineResultTypes writeToPipeline, IList input, params object[] args)
        {
            if (_cmdlet != null)
            {
                _cmdlet.ThrowIfStopping();
            }

            Cmdlet cmdletToUse = null;

            ScriptBlock.ErrorHandlingBehavior errorHandlingBehavior = ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe;

            // Check if they want output
            if ((writeToPipeline & PipelineResultTypes.Output) == PipelineResultTypes.Output)
            {
                cmdletToUse      = _cmdlet;
                writeToPipeline &= (~PipelineResultTypes.Output);
            }

            // Check if they want error
            if ((writeToPipeline & PipelineResultTypes.Error) == PipelineResultTypes.Error)
            {
                errorHandlingBehavior = ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe;
                writeToPipeline      &= (~PipelineResultTypes.Error);
            }

            if (writeToPipeline != PipelineResultTypes.None)
            {
                // The only output types are Output and Error.
                throw PSTraceSource.NewNotImplementedException();
            }

            // If the cmdletToUse is not null, then the result of the evaluation will be
            // streamed out the output pipe of the cmdlet.
            object rawResult;

            if (cmdletToUse != null)
            {
                sb.InvokeUsingCmdlet(
                    contextCmdlet: cmdletToUse,
                    useLocalScope: useNewScope,
                    errorHandlingBehavior: errorHandlingBehavior,
                    dollarUnder: AutomationNull.Value,
                    input: input,
                    scriptThis: AutomationNull.Value,
                    args: args);
                rawResult = AutomationNull.Value;
            }
            else
            {
                rawResult = sb.DoInvokeReturnAsIs(
                    useLocalScope: useNewScope,
                    errorHandlingBehavior: errorHandlingBehavior,
                    dollarUnder: AutomationNull.Value,
                    input: input,
                    scriptThis: AutomationNull.Value,
                    args: args);
            }

            if (rawResult == AutomationNull.Value)
            {
                return(new Collection <PSObject>());
            }

            // If the result is already a collection of PSObjects, just return it...
            Collection <PSObject> result = rawResult as Collection <PSObject>;

            if (result != null)
            {
                return(result);
            }

            result = new Collection <PSObject>();

            IEnumerator list = null;

            list = LanguagePrimitives.GetEnumerator(rawResult);

            if (list != null)
            {
                while (list.MoveNext())
                {
                    object val = list.Current;

                    result.Add(LanguagePrimitives.AsPSObjectOrNull(val));
                }
            }
            else
            {
                result.Add(LanguagePrimitives.AsPSObjectOrNull(rawResult));
            }

            return(result);
        }
 private Collection<PSObject> InvokeScript(ScriptBlock sb, bool useNewScope, PipelineResultTypes writeToPipeline, IList input, params object[] args)
 {
     object obj2;
     if (this._cmdlet != null)
     {
         this._cmdlet.ThrowIfStopping();
     }
     Cmdlet contextCmdlet = null;
     ScriptBlock.ErrorHandlingBehavior writeToExternalErrorPipe = ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe;
     if ((writeToPipeline & PipelineResultTypes.Output) == PipelineResultTypes.Output)
     {
         contextCmdlet = this._cmdlet;
         writeToPipeline &= ~PipelineResultTypes.Output;
     }
     if ((writeToPipeline & PipelineResultTypes.Error) == PipelineResultTypes.Error)
     {
         writeToExternalErrorPipe = ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe;
         writeToPipeline &= ~PipelineResultTypes.Error;
     }
     if (writeToPipeline != PipelineResultTypes.None)
     {
         throw PSTraceSource.NewNotImplementedException();
     }
     if (contextCmdlet != null)
     {
         sb.InvokeUsingCmdlet(contextCmdlet, useNewScope, writeToExternalErrorPipe, AutomationNull.Value, input, AutomationNull.Value, args);
         obj2 = AutomationNull.Value;
     }
     else
     {
         obj2 = sb.DoInvokeReturnAsIs(useNewScope, writeToExternalErrorPipe, AutomationNull.Value, input, AutomationNull.Value, args);
     }
     if (obj2 == AutomationNull.Value)
     {
         return new Collection<PSObject>();
     }
     Collection<PSObject> collection = obj2 as Collection<PSObject>;
     if (collection == null)
     {
         collection = new Collection<PSObject>();
         IEnumerator enumerator = null;
         enumerator = LanguagePrimitives.GetEnumerator(obj2);
         if (enumerator != null)
         {
             while (enumerator.MoveNext())
             {
                 object current = enumerator.Current;
                 collection.Add(LanguagePrimitives.AsPSObjectOrNull(current));
             }
             return collection;
         }
         collection.Add(LanguagePrimitives.AsPSObjectOrNull(obj2));
     }
     return collection;
 }
Esempio n. 7
0
        private Collection<PSObject> InvokeScript(ScriptBlock sb, bool useNewScope,
            PipelineResultTypes writeToPipeline, IList input, params object[] args)
        {
            if (null != _cmdlet)
                _cmdlet.ThrowIfStopping();

            Cmdlet cmdletToUse = null;
            ScriptBlock.ErrorHandlingBehavior errorHandlingBehavior = ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe;

            // Check if they want output
            if ((writeToPipeline & PipelineResultTypes.Output) == PipelineResultTypes.Output)
            {
                cmdletToUse = _cmdlet;
                writeToPipeline &= (~PipelineResultTypes.Output);
            }

            // Check if they want error
            if ((writeToPipeline & PipelineResultTypes.Error) == PipelineResultTypes.Error)
            {
                errorHandlingBehavior = ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe;
                writeToPipeline &= (~PipelineResultTypes.Error);
            }

            if (writeToPipeline != PipelineResultTypes.None)
            {
                // The only output types are Output and Error.
                throw PSTraceSource.NewNotImplementedException();
            }

            // If the cmdletToUse is not null, then the result of the evaluation will be
            // streamed out the output pipe of the cmdlet.
            object rawResult;
            if (cmdletToUse != null)
            {
                sb.InvokeUsingCmdlet(
                    contextCmdlet: cmdletToUse,
                    useLocalScope: useNewScope,
                    errorHandlingBehavior: errorHandlingBehavior,
                    dollarUnder: AutomationNull.Value,
                    input: input,
                    scriptThis: AutomationNull.Value,
                    args: args);
                rawResult = AutomationNull.Value;
            }
            else
            {
                rawResult = sb.DoInvokeReturnAsIs(
                    useLocalScope: useNewScope,
                    errorHandlingBehavior: errorHandlingBehavior,
                    dollarUnder: AutomationNull.Value,
                    input: input,
                    scriptThis: AutomationNull.Value,
                    args: args);
            }

            if (rawResult == AutomationNull.Value)
            {
                return new Collection<PSObject>();
            }

            // If the result is already a collection of PSObjects, just return it...
            Collection<PSObject> result = rawResult as Collection<PSObject>;
            if (result != null)
                return result;

            result = new Collection<PSObject>();

            IEnumerator list = null;
            list = LanguagePrimitives.GetEnumerator(rawResult);

            if (list != null)
            {
                while (list.MoveNext())
                {
                    object val = list.Current;

                    result.Add(LanguagePrimitives.AsPSObjectOrNull(val));
                }
            }
            else
            {
                result.Add(LanguagePrimitives.AsPSObjectOrNull(rawResult));
            }

            return result;
        }