Ejemplo n.º 1
0
        /// <summary>
        /// Clones the specified scriptblock maintaining its language mode.
        /// </summary>
        /// <param name="ScriptBlock">The Scriptblock to clone</param>
        /// <returns>A clone of the scriptblock with the languagemode intact</returns>
        public static ScriptBlock Clone(this ScriptBlock ScriptBlock)
        {
            ScriptBlock newBlock = (ScriptBlock)UtilityHost.InvokePrivateMethod("Clone", ScriptBlock, null);

            UtilityHost.SetPrivateProperty("LanguageMode", newBlock, UtilityHost.GetPrivateProperty("LanguageMode", ScriptBlock));
            return(newBlock);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Validates that the parameter argument is not untrusted
        /// </summary>
        /// <param name="arguments">Object to validate</param>
        /// <param name="engineIntrinsics">
        /// The engine APIs for the context under which the validation is being
        /// evaluated.
        /// </param>
        /// <exception cref="ValidationMetadataException">
        /// if the argument is untrusted.
        /// </exception>
        protected override void Validate(object arguments, EngineIntrinsics engineIntrinsics)
        {
            bool       everConstrained = false;
            bool       isFullLanguage  = false;
            MethodInfo marked          = null;

            try
            {
                object executionContextTLS = UtilityHost.GetExecutionContextFromTLS();
                everConstrained = (bool)UtilityHost.GetPrivateStaticProperty(executionContextTLS.GetType(), "HasEverUsedConstrainedLanguage");
                isFullLanguage  = (PSLanguageMode)UtilityHost.GetPrivateProperty("LanguageMode", executionContextTLS) == PSLanguageMode.FullLanguage;
                marked          = UtilityHost.GetPrivateStaticMethod(executionContextTLS.GetType(), "IsMarkedAsUntrusted");
            }
            catch { }

            if (everConstrained && isFullLanguage)
            {
                if ((bool)marked.Invoke(null, BindingFlags.NonPublic | BindingFlags.Static, null, new object[] { arguments }, System.Globalization.CultureInfo.CurrentCulture))
                {
                    throw new ValidationMetadataException(String.Format(Localization.LocalizationHost.Read("PSFramework.Assembly.Validation.UntrustedData"), arguments));
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns the correct results, either by executing the scriptblock or consulting the cache
        /// </summary>
        /// <returns></returns>
        public string[] Invoke()
        {
            if (!ShouldExecute)
            {
                return(LastResult);
            }

            List <string> results = new List <string>();

            CallStackFrame callerFrame = null;
            InvocationInfo info        = null;

            try
            {
                object CurrentCommandProcessor = UtilityHost.GetPrivateProperty("CurrentCommandProcessor", UtilityHost.GetExecutionContextFromTLS());
                object CommandObject           = UtilityHost.GetPrivateProperty("Command", CurrentCommandProcessor);
                info = UtilityHost.GetPublicProperty("MyInvocation", CommandObject) as InvocationInfo;
            }
            catch (Exception e)
            {
                if (PSFCore.PSFCoreHost.DebugMode)
                {
                    PSFCore.PSFCoreHost.WriteDebug(String.Format("Script Container {0} | Error accessing Current Command Processor", Name), e);
                }
            }

            if (info == null)
            {
                IEnumerable <CallStackFrame> _callStack = Utility.UtilityHost.Callstack;

                object errorItem = null;
                try
                {
                    if (_callStack.Count() > 0)
                    {
                        callerFrame = _callStack.Where(frame => frame.InvocationInfo != null && frame.InvocationInfo.MyCommand != null).First();
                    }
                }
                catch (Exception e) { errorItem = e; }
                if (PSFCore.PSFCoreHost.DebugMode)
                {
                    PSFCore.PSFCoreHost.WriteDebug(String.Format("Script Container {0} | Callframe selected", Name), callerFrame);
                    PSFCore.PSFCoreHost.WriteDebug(String.Format("Script Container {0} | Script Callstack", Name), new Message.CallStack(Utility.UtilityHost.Callstack));
                    if (errorItem != null)
                    {
                        PSFCore.PSFCoreHost.WriteDebug(String.Format("Script Container {0} | Error when selecting Callframe", Name), errorItem);
                    }
                }
            }
            if (info == null && callerFrame != null)
            {
                info = callerFrame.InvocationInfo;
            }

            // ScriptBlock scriptBlock = ScriptBlock.Create(ScriptBlock.ToString());
            if (info != null)
            {
                foreach (PSObject item in ScriptBlock.Invoke(info.MyCommand.Name, "", "", null, info.BoundParameters))
                {
                    results.Add((string)item.Properties["CompletionText"].Value);
                }
            }
            else
            {
                foreach (PSObject item in ScriptBlock.Invoke("<ScriptBlock>", "", "", null, new Dictionary <string, object>(StringComparer.InvariantCultureIgnoreCase)))
                {
                    results.Add((string)item.Properties["CompletionText"].Value);
                }
            }

            return(results.ToArray());
        }