Example #1
0
        internal override void SetSessionStateItem(string name, object value, bool writeItem)
        {
            FunctionProviderDynamicParameters dynamicParameters = this.DynamicParameters as FunctionProviderDynamicParameters;
            bool flag = dynamicParameters != null && dynamicParameters.OptionsSet;

            if (value == null)
            {
                if (flag)
                {
                    CommandInfo sessionStateItem = (CommandInfo)this.GetSessionStateItem(name);
                    if (sessionStateItem == null)
                    {
                        return;
                    }
                    FunctionProvider.SetOptions(sessionStateItem, dynamicParameters.Options);
                }
                else
                {
                    this.RemoveSessionStateItem(name);
                }
            }
            else
            {
                if (value is PSObject psObject)
                {
                    value = psObject.BaseObject;
                }
                CommandInfo commandInfo;
                if (value is ScriptBlock function)
                {
                    commandInfo = !flag ? (CommandInfo)this.SessionState.Internal.SetFunction(name, function, (bool)this.Force, this.Context.Origin) : (CommandInfo)this.SessionState.Internal.SetFunction(name, function, dynamicParameters.Options, (bool)this.Force, this.Context.Origin);
                }
                else if (value is FunctionInfo functionInfo)
                {
                    ScopedItemOptions options = functionInfo.Options;
                    if (flag)
                    {
                        options = dynamicParameters.Options;
                    }
                    commandInfo = (CommandInfo)this.SessionState.Internal.SetFunction(name, functionInfo.ScriptBlock, options, (bool)this.Force, this.Context.Origin);
                }
                else if (value is FilterInfo filterInfo)
                {
                    ScopedItemOptions options = filterInfo.Options;
                    if (flag)
                    {
                        options = dynamicParameters.Options;
                    }
                    commandInfo = (CommandInfo)this.SessionState.Internal.SetFunction(name, filterInfo.ScriptBlock, options, (bool)this.Force, this.Context.Origin);
                }
                else
                {
                    if (!(value is string script))
                    {
                        throw FunctionProvider.tracer.NewArgumentException(nameof(value));
                    }
                    ScriptBlock function = ScriptBlock.Create(this.Context.ExecutionContext, script);
                    commandInfo = !flag ? (CommandInfo)this.SessionState.Internal.SetFunction(name, function, (bool)this.Force, this.Context.Origin) : (CommandInfo)this.SessionState.Internal.SetFunction(name, function, dynamicParameters.Options, (bool)this.Force, this.Context.Origin);
                }
                if (!writeItem || commandInfo == null)
                {
                    return;
                }
                this.WriteItemObject((object)commandInfo, commandInfo.Name, false);
            }
        }
Example #2
0
        } // GetSessionStateItem

        /// <summary>
        /// Sets the function of the specified name to the specified value
        /// </summary>
        ///
        /// <param name="name">
        /// The name of the function to set.
        /// </param>
        ///
        /// <param name="value">
        /// The new value for the function.
        /// </param>
        ///
        /// <param name="writeItem">
        /// If true, the item that was set should be written to WriteItemObject.
        /// </param>
        ///
#pragma warning disable 0162
        internal override void SetSessionStateItem(string name, object value, bool writeItem)
        {
            Dbg.Diagnostics.Assert(
                !String.IsNullOrEmpty(name),
                "The caller should verify this parameter");

            FunctionProviderDynamicParameters dynamicParameters =
                DynamicParameters as FunctionProviderDynamicParameters;

            CommandInfo modifiedItem = null;

            bool dynamicParametersSpecified = dynamicParameters != null && dynamicParameters.OptionsSet;

            if (value == null)
            {
                // If the value wasn't specified but the options were, just set the
                // options on the existing function.
                // If the options weren't specified, then remove the function

                if (dynamicParametersSpecified)
                {
                    modifiedItem = (CommandInfo)GetSessionStateItem(name);

                    if (modifiedItem != null)
                    {
                        SetOptions(modifiedItem, dynamicParameters.Options);
                    }
                }
                else
                {
                    RemoveSessionStateItem(name);
                }
            }
            else
            {
                do // false loop
                {
                    // Unwrap the PSObject before binding it as a scriptblock...
                    PSObject pso = value as PSObject;
                    if (pso != null)
                    {
                        value = pso.BaseObject;
                    }

                    ScriptBlock scriptBlockValue = value as ScriptBlock;
                    if (scriptBlockValue != null)
                    {
                        if (dynamicParametersSpecified)
                        {
                            modifiedItem = SessionState.Internal.SetFunction(name, scriptBlockValue,
                                                                             null, dynamicParameters.Options, Force, Context.Origin);
                        }
                        else
                        {
                            modifiedItem = SessionState.Internal.SetFunction(name, scriptBlockValue, null, Force, Context.Origin);
                        }
                        break;
                    }

                    FunctionInfo function = value as FunctionInfo;
                    if (function != null)
                    {
                        ScopedItemOptions options = function.Options;

                        if (dynamicParametersSpecified)
                        {
                            options = dynamicParameters.Options;
                        }

                        modifiedItem = SessionState.Internal.SetFunction(name, function.ScriptBlock, function, options, Force, Context.Origin);
                        break;
                    }

                    String stringValue = value as string;
                    if (stringValue != null)
                    {
                        ScriptBlock scriptBlock = ScriptBlock.Create(Context.ExecutionContext, stringValue);

                        if (dynamicParametersSpecified)
                        {
                            modifiedItem = SessionState.Internal.SetFunction(name, scriptBlock, null, dynamicParameters.Options, Force, Context.Origin);
                        }
                        else
                        {
                            modifiedItem = SessionState.Internal.SetFunction(name, scriptBlock, null, Force, Context.Origin);
                        }
                        break;
                    }

                    throw PSTraceSource.NewArgumentException("value");
                } while (false);

                if (writeItem && modifiedItem != null)
                {
                    WriteItemObject(modifiedItem, modifiedItem.Name, false);
                }
            }
        } // SetSessionStateItem
Example #3
0
        internal override void SetSessionStateItem(string name, object value, bool writeItem)
        {
            FunctionProviderDynamicParameters dynamicParameters = base.DynamicParameters as FunctionProviderDynamicParameters;
            CommandInfo function = null;
            bool        flag     = (dynamicParameters != null) && dynamicParameters.OptionsSet;

            if (value == null)
            {
                if (!flag)
                {
                    this.RemoveSessionStateItem(name);
                }
                else
                {
                    function = (CommandInfo)this.GetSessionStateItem(name);
                    if (function != null)
                    {
                        SetOptions(function, dynamicParameters.Options);
                    }
                }
            }
            else
            {
                PSObject obj2 = value as PSObject;
                if (obj2 != null)
                {
                    value = obj2.BaseObject;
                }
                ScriptBlock block = value as ScriptBlock;
                if (block != null)
                {
                    if (flag)
                    {
                        function = base.SessionState.Internal.SetFunction(name, block, null, dynamicParameters.Options, (bool)base.Force, base.Context.Origin);
                    }
                    else
                    {
                        function = base.SessionState.Internal.SetFunction(name, block, null, (bool)base.Force, base.Context.Origin);
                    }
                }
                else
                {
                    FunctionInfo originalFunction = value as FunctionInfo;
                    if (originalFunction != null)
                    {
                        ScopedItemOptions options = originalFunction.Options;
                        if (flag)
                        {
                            options = dynamicParameters.Options;
                        }
                        function = base.SessionState.Internal.SetFunction(name, originalFunction.ScriptBlock, originalFunction, options, (bool)base.Force, base.Context.Origin);
                    }
                    else
                    {
                        string script = value as string;
                        if (script == null)
                        {
                            throw PSTraceSource.NewArgumentException("value");
                        }
                        ScriptBlock block2 = ScriptBlock.Create(base.Context.ExecutionContext, script);
                        if (flag)
                        {
                            function = base.SessionState.Internal.SetFunction(name, block2, null, dynamicParameters.Options, (bool)base.Force, base.Context.Origin);
                        }
                        else
                        {
                            function = base.SessionState.Internal.SetFunction(name, block2, null, (bool)base.Force, base.Context.Origin);
                        }
                    }
                }
                if (writeItem && (function != null))
                {
                    base.WriteItemObject(function, function.Name, false);
                }
            }
        }