예제 #1
0
 internal void NewDrive(PSDriveInfo newDrive)
 {
     if (newDrive == null)
     {
         throw PSTraceSource.NewArgumentNullException("newDrive");
     }
     if (this.GetDrives().ContainsKey(newDrive.Name))
     {
         SessionStateException exception = new SessionStateException(newDrive.Name, SessionStateCategory.Drive, "DriveAlreadyExists", SessionStateStrings.DriveAlreadyExists, ErrorCategory.ResourceExists, new object[0]);
         throw exception;
     }
     if (!newDrive.IsAutoMounted && (this.GetDrives().Count > (this.DriveCapacity.FastValue - 1)))
     {
         SessionStateOverflowException exception2 = new SessionStateOverflowException(newDrive.Name, SessionStateCategory.Drive, "DriveOverflow", SessionStateStrings.DriveOverflow, new object[] { this.DriveCapacity.FastValue });
         throw exception2;
     }
     if (!newDrive.IsAutoMounted)
     {
         this.GetDrives().Add(newDrive.Name, newDrive);
     }
     else if (!this.GetAutomountedDrives().ContainsKey(newDrive.Name))
     {
         this.GetAutomountedDrives().Add(newDrive.Name, newDrive);
     }
 }
예제 #2
0
 internal AliasInfo SetAliasItem(AliasInfo aliasToSet, bool force, CommandOrigin origin = CommandOrigin.Internal)
 {
     if (!this.GetAliases().ContainsKey(aliasToSet.Name))
     {
         if (this.GetAliases().Count > (this.AliasCapacity.FastValue - 1))
         {
             SessionStateOverflowException exception = new SessionStateOverflowException(aliasToSet.Name, SessionStateCategory.Alias, "AliasOverflow", SessionStateStrings.AliasOverflow, new object[] { this.AliasCapacity.FastValue });
             throw exception;
         }
         this.GetAliases()[aliasToSet.Name] = aliasToSet;
     }
     else
     {
         AliasInfo valueToCheck = this.GetAliases()[aliasToSet.Name];
         SessionState.ThrowIfNotVisible(origin, valueToCheck);
         if (((valueToCheck.Options & ScopedItemOptions.Constant) != ScopedItemOptions.None) || (((valueToCheck.Options & ScopedItemOptions.ReadOnly) != ScopedItemOptions.None) && !force))
         {
             SessionStateUnauthorizedAccessException exception2 = new SessionStateUnauthorizedAccessException(aliasToSet.Name, SessionStateCategory.Alias, "AliasNotWritable", SessionStateStrings.AliasNotWritable);
             throw exception2;
         }
         if (((aliasToSet.Options & ScopedItemOptions.AllScope) == ScopedItemOptions.None) && ((valueToCheck.Options & ScopedItemOptions.AllScope) != ScopedItemOptions.None))
         {
             SessionStateUnauthorizedAccessException exception3 = new SessionStateUnauthorizedAccessException(aliasToSet.Name, SessionStateCategory.Alias, "AliasAllScopeOptionCannotBeRemoved", SessionStateStrings.AliasAllScopeOptionCannotBeRemoved);
             throw exception3;
         }
         this.RemoveAliasFromCache(valueToCheck.Name, valueToCheck.Definition);
         this.GetAliases()[aliasToSet.Name] = aliasToSet;
     }
     this.AddAliasToCache(aliasToSet.Name, aliasToSet.Definition);
     return(this.GetAliases()[aliasToSet.Name]);
 }
예제 #3
0
 internal AliasInfo SetAliasValue(string name, string value, System.Management.Automation.ExecutionContext context, bool force, CommandOrigin origin)
 {
     if (!this.GetAliases().ContainsKey(name))
     {
         if (this.GetAliases().Count > (this.AliasCapacity.FastValue - 1))
         {
             SessionStateOverflowException exception = new SessionStateOverflowException(name, SessionStateCategory.Alias, "AliasOverflow", SessionStateStrings.AliasOverflow, new object[] { this.AliasCapacity.FastValue });
             throw exception;
         }
         this.GetAliases()[name] = new AliasInfo(name, value, context);
     }
     else
     {
         AliasInfo valueToCheck = this.GetAliases()[name];
         if (((valueToCheck.Options & ScopedItemOptions.Constant) != ScopedItemOptions.None) || (!force && ((valueToCheck.Options & ScopedItemOptions.ReadOnly) != ScopedItemOptions.None)))
         {
             SessionStateUnauthorizedAccessException exception2 = new SessionStateUnauthorizedAccessException(name, SessionStateCategory.Alias, "AliasNotWritable", SessionStateStrings.AliasNotWritable);
             throw exception2;
         }
         SessionState.ThrowIfNotVisible(origin, valueToCheck);
         this.RemoveAliasFromCache(valueToCheck.Name, valueToCheck.Definition);
         if (force)
         {
             this.GetAliases().Remove(name);
             valueToCheck            = new AliasInfo(name, value, context);
             this.GetAliases()[name] = valueToCheck;
         }
         else
         {
             valueToCheck.SetDefinition(value, false);
         }
     }
     this.AddAliasToCache(name, value);
     return(this.GetAliases()[name]);
 }
예제 #4
0
 internal FunctionInfo SetFunction(string name, ScriptBlock function, FunctionInfo originalFunction, ScopedItemOptions options, bool force, CommandOrigin origin, System.Management.Automation.ExecutionContext context, string helpFile, Func <string, ScriptBlock, FunctionInfo, ScopedItemOptions, System.Management.Automation.ExecutionContext, string, FunctionInfo> functionFactory)
 {
     if (!this.GetFunctions().ContainsKey(name))
     {
         if (this.GetFunctions().Count > (this.FunctionCapacity.FastValue - 1))
         {
             SessionStateOverflowException exception = new SessionStateOverflowException(name, SessionStateCategory.Function, "FunctionOverflow", SessionStateStrings.FunctionOverflow, new object[] { this.FunctionCapacity.FastValue });
             throw exception;
         }
         FunctionInfo info = functionFactory(name, function, originalFunction, options, context, helpFile);
         this.GetFunctions()[name] = info;
         if (IsFunctionOptionSet(info, ScopedItemOptions.AllScope))
         {
             this.GetAllScopeFunctions()[name] = info;
         }
     }
     else
     {
         FunctionInfo valueToCheck = this.GetFunctions()[name];
         SessionState.ThrowIfNotVisible(origin, valueToCheck);
         if (IsFunctionOptionSet(valueToCheck, ScopedItemOptions.Constant) || (!force && IsFunctionOptionSet(valueToCheck, ScopedItemOptions.ReadOnly)))
         {
             SessionStateUnauthorizedAccessException exception2 = new SessionStateUnauthorizedAccessException(name, SessionStateCategory.Function, "FunctionNotWritable", SessionStateStrings.FunctionNotWritable);
             throw exception2;
         }
         if ((options & ScopedItemOptions.Constant) != ScopedItemOptions.None)
         {
             SessionStateUnauthorizedAccessException exception3 = new SessionStateUnauthorizedAccessException(name, SessionStateCategory.Function, "FunctionCannotBeMadeConstant", SessionStateStrings.FunctionCannotBeMadeConstant);
             throw exception3;
         }
         if (((options & ScopedItemOptions.AllScope) == ScopedItemOptions.None) && IsFunctionOptionSet(valueToCheck, ScopedItemOptions.AllScope))
         {
             SessionStateUnauthorizedAccessException exception4 = new SessionStateUnauthorizedAccessException(name, SessionStateCategory.Function, "FunctionAllScopeOptionCannotBeRemoved", SessionStateStrings.FunctionAllScopeOptionCannotBeRemoved);
             throw exception4;
         }
         FunctionInfo info3       = valueToCheck;
         FunctionInfo newFunction = null;
         if (info3 != null)
         {
             newFunction = functionFactory(name, function, originalFunction, options, context, helpFile);
             if (!info3.GetType().Equals(newFunction.GetType()) || (((info3.Options & ScopedItemOptions.ReadOnly) != ScopedItemOptions.None) && force))
             {
                 this.GetFunctions()[name] = newFunction;
             }
             else
             {
                 bool flag2 = force || ((options & ScopedItemOptions.ReadOnly) == ScopedItemOptions.None);
                 info3.Update(newFunction, flag2, options, helpFile);
             }
         }
     }
     return(this.GetFunctions()[name]);
 }
예제 #5
0
        internal PSVariable NewVariable(PSVariable newVariable, bool force, SessionStateInternal sessionState)
        {
            PSVariable variable;
            bool       flag = this.TryGetVariable(newVariable.Name, this.ScopeOrigin, true, out variable);

            if (flag)
            {
                if (((variable == null) || variable.IsConstant) || (!force && variable.IsReadOnly))
                {
                    SessionStateUnauthorizedAccessException exception = new SessionStateUnauthorizedAccessException(newVariable.Name, SessionStateCategory.Variable, "VariableNotWritable", SessionStateStrings.VariableNotWritable);
                    throw exception;
                }
                if (variable is LocalVariable)
                {
                    SessionStateUnauthorizedAccessException exception2 = new SessionStateUnauthorizedAccessException(newVariable.Name, SessionStateCategory.Variable, "VariableNotWritableRare", SessionStateStrings.VariableNotWritableRare);
                    throw exception2;
                }
                if (!object.ReferenceEquals(newVariable, variable))
                {
                    variable.WasRemoved = true;
                    variable            = newVariable;
                }
            }
            else
            {
                variable = newVariable;
            }
            if (!flag && (this._variables.Count > (this.VariableCapacity.FastValue - 1)))
            {
                SessionStateOverflowException exception3 = new SessionStateOverflowException(newVariable.Name, SessionStateCategory.Variable, "VariableOverflow", SessionStateStrings.VariableOverflow, new object[] { this.VariableCapacity.FastValue });
                throw exception3;
            }
            if (System.Management.Automation.ExecutionContext.HasEverUsedConstrainedLanguage)
            {
                System.Management.Automation.ExecutionContext executionContextFromTLS = LocalPipeline.GetExecutionContextFromTLS();
                if (((executionContextFromTLS != null) && (executionContextFromTLS.LanguageMode == PSLanguageMode.ConstrainedLanguage)) && ((variable.Options & ScopedItemOptions.AllScope) == ScopedItemOptions.AllScope))
                {
                    throw new PSNotSupportedException();
                }
            }
            this._variables[variable.Name] = variable;
            variable.SessionState          = sessionState;
            return(variable);
        }
예제 #6
0
 internal PSVariable SetVariable(string name, object value, bool asValue, bool force, SessionStateInternal sessionState, CommandOrigin origin = CommandOrigin.Internal, bool fastPath = false)
 {
     PSVariable variable;
     PSVariable variable2 = value as PSVariable;
     if (fastPath)
     {
         if (this.Parent != null)
         {
             throw new NotImplementedException("fastPath");
         }
         variable = new PSVariable(name, variable2.Value, variable2.Options, variable2.Attributes) {
             Description = variable2.Description
         };
         this.GetPrivateVariables()[name] = variable;
         return variable;
     }
     bool flag = this.TryGetVariable(name, origin, true, out variable);
     if (!asValue && (variable2 != null))
     {
         if (flag)
         {
             if (((variable == null) || variable.IsConstant) || (!force && variable.IsReadOnly))
             {
                 SessionStateUnauthorizedAccessException exception = new SessionStateUnauthorizedAccessException(name, SessionStateCategory.Variable, "VariableNotWritable", SessionStateStrings.VariableNotWritable);
                 throw exception;
             }
             if ((variable is LocalVariable) && (variable2.Attributes.Any<Attribute>() || (variable2.Options != variable.Options)))
             {
                 SessionStateUnauthorizedAccessException exception2 = new SessionStateUnauthorizedAccessException(name, SessionStateCategory.Variable, "VariableNotWritableRare", SessionStateStrings.VariableNotWritableRare);
                 throw exception2;
             }
             if (variable.IsReadOnly && force)
             {
                 this._variables.Remove(name);
                 flag = false;
                 variable = new PSVariable(name, variable2.Value, variable2.Options, variable2.Attributes) {
                     Description = variable2.Description
                 };
             }
             else
             {
                 variable.Attributes.Clear();
                 variable.Value = variable2.Value;
                 variable.Options = variable2.Options;
                 variable.Description = variable2.Description;
                 foreach (Attribute attribute in variable2.Attributes)
                 {
                     variable.Attributes.Add(attribute);
                 }
             }
         }
         else
         {
             variable = variable2;
         }
     }
     else if (variable != null)
     {
         variable.Value = value;
     }
     else
     {
         variable = (this.LocalsTuple != null) ? (this.LocalsTuple.TrySetVariable(name, value)) ?? new PSVariable(name, value) : new PSVariable(name, value);
     }
     if (!flag && (this._variables.Count > (this.VariableCapacity.FastValue - 1)))
     {
         SessionStateOverflowException exception3 = new SessionStateOverflowException(name, SessionStateCategory.Variable, "VariableOverflow", SessionStateStrings.VariableOverflow, new object[] { this.VariableCapacity.FastValue });
         throw exception3;
     }
     if (System.Management.Automation.ExecutionContext.HasEverUsedConstrainedLanguage)
     {
         System.Management.Automation.ExecutionContext executionContextFromTLS = LocalPipeline.GetExecutionContextFromTLS();
         if (((executionContextFromTLS != null) && (executionContextFromTLS.LanguageMode == PSLanguageMode.ConstrainedLanguage)) && ((variable.Options & ScopedItemOptions.AllScope) == ScopedItemOptions.AllScope))
         {
             /* TODO: Review how to  get around this: */ /* throw new PSNotSupportedException(); */
         }
     }
     this._variables[name] = variable;
     variable.SessionState = sessionState;
     return variable;
 }
예제 #7
0
 internal FunctionInfo SetFunction(string name, ScriptBlock function, FunctionInfo originalFunction, ScopedItemOptions options, bool force, CommandOrigin origin, System.Management.Automation.ExecutionContext context, string helpFile, Func<string, ScriptBlock, FunctionInfo, ScopedItemOptions, System.Management.Automation.ExecutionContext, string, FunctionInfo> functionFactory)
 {
     if (!this.GetFunctions().ContainsKey(name))
     {
         if (this.GetFunctions().Count > (this.FunctionCapacity.FastValue - 1))
         {
             SessionStateOverflowException exception = new SessionStateOverflowException(name, SessionStateCategory.Function, "FunctionOverflow", SessionStateStrings.FunctionOverflow, new object[] { this.FunctionCapacity.FastValue });
             throw exception;
         }
         FunctionInfo info = functionFactory(name, function, originalFunction, options, context, helpFile);
         this.GetFunctions()[name] = info;
         if (IsFunctionOptionSet(info, ScopedItemOptions.AllScope))
         {
             this.GetAllScopeFunctions()[name] = info;
         }
     }
     else
     {
         FunctionInfo valueToCheck = this.GetFunctions()[name];
         SessionState.ThrowIfNotVisible(origin, valueToCheck);
         if (IsFunctionOptionSet(valueToCheck, ScopedItemOptions.Constant) || (!force && IsFunctionOptionSet(valueToCheck, ScopedItemOptions.ReadOnly)))
         {
             SessionStateUnauthorizedAccessException exception2 = new SessionStateUnauthorizedAccessException(name, SessionStateCategory.Function, "FunctionNotWritable", SessionStateStrings.FunctionNotWritable);
             throw exception2;
         }
         if ((options & ScopedItemOptions.Constant) != ScopedItemOptions.None)
         {
             SessionStateUnauthorizedAccessException exception3 = new SessionStateUnauthorizedAccessException(name, SessionStateCategory.Function, "FunctionCannotBeMadeConstant", SessionStateStrings.FunctionCannotBeMadeConstant);
             throw exception3;
         }
         if (((options & ScopedItemOptions.AllScope) == ScopedItemOptions.None) && IsFunctionOptionSet(valueToCheck, ScopedItemOptions.AllScope))
         {
             SessionStateUnauthorizedAccessException exception4 = new SessionStateUnauthorizedAccessException(name, SessionStateCategory.Function, "FunctionAllScopeOptionCannotBeRemoved", SessionStateStrings.FunctionAllScopeOptionCannotBeRemoved);
             throw exception4;
         }
         FunctionInfo info3 = valueToCheck;
         FunctionInfo newFunction = null;
         if (info3 != null)
         {
             newFunction = functionFactory(name, function, originalFunction, options, context, helpFile);
             if (!info3.GetType().Equals(newFunction.GetType()) || (((info3.Options & ScopedItemOptions.ReadOnly) != ScopedItemOptions.None) && force))
             {
                 this.GetFunctions()[name] = newFunction;
             }
             else
             {
                 bool flag2 = force || ((options & ScopedItemOptions.ReadOnly) == ScopedItemOptions.None);
                 info3.Update(newFunction, flag2, options, helpFile);
             }
         }
     }
     return this.GetFunctions()[name];
 }
예제 #8
0
 internal AliasInfo SetAliasValue(string name, string value, ScopedItemOptions options, System.Management.Automation.ExecutionContext context, bool force, CommandOrigin origin)
 {
     if (!this.GetAliases().ContainsKey(name))
     {
         if (this.GetAliases().Count > (this.AliasCapacity.FastValue - 1))
         {
             SessionStateOverflowException exception = new SessionStateOverflowException(name, SessionStateCategory.Alias, "AliasOverflow", SessionStateStrings.AliasOverflow, new object[] { this.AliasCapacity.FastValue });
             throw exception;
         }
         AliasInfo info = new AliasInfo(name, value, context, options);
         this.GetAliases()[name] = info;
     }
     else
     {
         AliasInfo valueToCheck = this.GetAliases()[name];
         if (((valueToCheck.Options & ScopedItemOptions.Constant) != ScopedItemOptions.None) || (!force && ((valueToCheck.Options & ScopedItemOptions.ReadOnly) != ScopedItemOptions.None)))
         {
             SessionStateUnauthorizedAccessException exception2 = new SessionStateUnauthorizedAccessException(name, SessionStateCategory.Alias, "AliasNotWritable", SessionStateStrings.AliasNotWritable);
             throw exception2;
         }
         if ((options & ScopedItemOptions.Constant) != ScopedItemOptions.None)
         {
             SessionStateUnauthorizedAccessException exception3 = new SessionStateUnauthorizedAccessException(name, SessionStateCategory.Alias, "AliasCannotBeMadeConstant", SessionStateStrings.AliasCannotBeMadeConstant);
             throw exception3;
         }
         if (((options & ScopedItemOptions.AllScope) == ScopedItemOptions.None) && ((valueToCheck.Options & ScopedItemOptions.AllScope) != ScopedItemOptions.None))
         {
             SessionStateUnauthorizedAccessException exception4 = new SessionStateUnauthorizedAccessException(name, SessionStateCategory.Alias, "AliasAllScopeOptionCannotBeRemoved", SessionStateStrings.AliasAllScopeOptionCannotBeRemoved);
             throw exception4;
         }
         SessionState.ThrowIfNotVisible(origin, valueToCheck);
         this.RemoveAliasFromCache(valueToCheck.Name, valueToCheck.Definition);
         if (force)
         {
             this.GetAliases().Remove(name);
             valueToCheck = new AliasInfo(name, value, context, options);
             this.GetAliases()[name] = valueToCheck;
         }
         else
         {
             valueToCheck.Options = options;
             valueToCheck.SetDefinition(value, false);
         }
     }
     this.AddAliasToCache(name, value);
     return this.GetAliases()[name];
 }
예제 #9
0
        } // SetAliasValue


        /// <summary>
        /// Sets an alias to the given value.
        /// </summary>
        ///
        /// <param name="aliasToSet">
        /// The information about the alias to be set
        /// </param>
        ///
        /// <param name="force">
        /// If true, the alias will be set even if there is an existing ReadOnly
        /// alias.
        /// </param>
        /// 
        /// <param name="origin">
        /// Specifies the command origin of the calling command.
        /// </param>
        /// 
        /// <returns>
        /// The string representing the value that was set.
        /// </returns>
        ///
        /// <exception cref="SessionStateUnauthorizedAccessException">
        /// If the alias is read-only or constant.
        /// </exception>
        /// 
        /// <exception cref="SessionStateOverflowException">
        /// If the maximum number of aliases has been reached for this scope.
        /// </exception>
        /// 
        internal AliasInfo SetAliasItem(AliasInfo aliasToSet, bool force, CommandOrigin origin = CommandOrigin.Internal)
        {
            Diagnostics.Assert(
                aliasToSet != null,
                "The caller should verify the aliasToSet");


            var aliasInfos = GetAliases();
            AliasInfo aliasInfo;
            if (!aliasInfos.TryGetValue(aliasToSet.Name, out aliasInfo))
            {
                if (aliasInfos.Count > AliasCapacity.FastValue - 1)
                {
                    SessionStateOverflowException e =
                        new SessionStateOverflowException(
                                aliasToSet.Name,
                                SessionStateCategory.Alias,
                                "AliasOverflow",
                                SessionStateStrings.AliasOverflow,
                                AliasCapacity.FastValue);

                    throw e;
                }
            }
            else
            {
                // An existing alias cannot be set if it is ReadOnly or Constant unless
                // force is specified, in which case an existing ReadOnly alias can
                // be set.

                SessionState.ThrowIfNotVisible(origin, aliasInfo);
                if ((aliasInfo.Options & ScopedItemOptions.Constant) != 0 ||
                    ((aliasInfo.Options & ScopedItemOptions.ReadOnly) != 0 && !force))
                {
                    SessionStateUnauthorizedAccessException e =
                        new SessionStateUnauthorizedAccessException(
                                aliasToSet.Name,
                                SessionStateCategory.Alias,
                                "AliasNotWritable",
                                SessionStateStrings.AliasNotWritable);

                    throw e;
                }

                if ((aliasToSet.Options & ScopedItemOptions.AllScope) == 0 &&
                    (aliasInfo.Options & ScopedItemOptions.AllScope) != 0)
                {
                    // user is trying to remove the AllScope option from the alias. 
                    // Do not allow this (as per spec).

                    SessionStateUnauthorizedAccessException e =
                        new SessionStateUnauthorizedAccessException(
                                aliasToSet.Name,
                                SessionStateCategory.Alias,
                                "AliasAllScopeOptionCannotBeRemoved",
                                SessionStateStrings.AliasAllScopeOptionCannotBeRemoved);

                    throw e;
                }

                RemoveAliasFromCache(aliasInfo.Name, aliasInfo.Definition);
            }
            aliasInfos[aliasToSet.Name] = aliasToSet;

            AddAliasToCache(aliasToSet.Name, aliasToSet.Definition);

            return aliasToSet;
        } // SetAliasItem
예제 #10
0
 internal void NewDrive(PSDriveInfo newDrive)
 {
     if (newDrive == null)
     {
         throw PSTraceSource.NewArgumentNullException("newDrive");
     }
     if (this.GetDrives().ContainsKey(newDrive.Name))
     {
         SessionStateException exception = new SessionStateException(newDrive.Name, SessionStateCategory.Drive, "DriveAlreadyExists", SessionStateStrings.DriveAlreadyExists, ErrorCategory.ResourceExists, new object[0]);
         throw exception;
     }
     if (!newDrive.IsAutoMounted && (this.GetDrives().Count > (this.DriveCapacity.FastValue - 1)))
     {
         SessionStateOverflowException exception2 = new SessionStateOverflowException(newDrive.Name, SessionStateCategory.Drive, "DriveOverflow", SessionStateStrings.DriveOverflow, new object[] { this.DriveCapacity.FastValue });
         throw exception2;
     }
     if (!newDrive.IsAutoMounted)
     {
         this.GetDrives().Add(newDrive.Name, newDrive);
     }
     else if (!this.GetAutomountedDrives().ContainsKey(newDrive.Name))
     {
         this.GetAutomountedDrives().Add(newDrive.Name, newDrive);
     }
 }
예제 #11
0
        } // GetAlias

        /// <summary>
        /// Sets an alias to the given value.
        /// </summary>
        ///
        /// <param name="name">
        /// The name of the alias to set.
        /// </param>
        ///
        /// <param name="value">
        /// The value for the alias
        /// </param>
        ///
        /// <param name="context">
        /// The execution context for this engine instance.
        /// </param>
        /// 
        /// <param name="force">
        /// If true, the value will be set even if the alias is ReadOnly.
        /// </param>
        /// 
        /// <param name="origin">
        /// Origin of the caller of this API
        /// </param>
        /// 
        /// <returns>
        /// The string representing the value that was set.
        /// </returns>
        ///
        /// <exception cref="SessionStateUnauthorizedAccessException">
        /// if the alias is read-only or constant.
        /// </exception>
        /// 
        /// <exception cref="SessionStateOverflowException">
        /// If the maximum number of aliases has been reached for this scope.
        /// </exception>
        /// 
        internal AliasInfo SetAliasValue(string name, string value, ExecutionContext context, bool force, CommandOrigin origin)
        {
            Diagnostics.Assert(
                name != null,
                "The caller should verify the name");

            var aliasInfos = GetAliases();
            AliasInfo aliasInfo;
            if (!aliasInfos.TryGetValue(name, out aliasInfo))
            {
                if (aliasInfos.Count > AliasCapacity.FastValue - 1)
                {
                    SessionStateOverflowException e =
                        new SessionStateOverflowException(
                                name,
                                SessionStateCategory.Alias,
                                "AliasOverflow",
                            SessionStateStrings.AliasOverflow,
                                AliasCapacity.FastValue);

                    throw e;
                }
                aliasInfos[name] = new AliasInfo(name, value, context);
            }
            else
            {
                // Make sure the alias isn't constant or readonly
                if ((aliasInfo.Options & ScopedItemOptions.Constant) != 0 ||
                    (!force && (aliasInfo.Options & ScopedItemOptions.ReadOnly) != 0))
                {
                    SessionStateUnauthorizedAccessException e =
                        new SessionStateUnauthorizedAccessException(
                                name,
                                SessionStateCategory.Alias,
                                "AliasNotWritable",
                                SessionStateStrings.AliasNotWritable);

                    throw e;
                }

                SessionState.ThrowIfNotVisible(origin, aliasInfo);
                RemoveAliasFromCache(aliasInfo.Name, aliasInfo.Definition);

                if (force)
                {
                    aliasInfos.Remove(name);
                    aliasInfo = new AliasInfo(name, value, context);
                    aliasInfos[name] = aliasInfo;
                }
                else
                {
                    aliasInfo.SetDefinition(value, false);
                }
            }

            AddAliasToCache(name, value);

            return aliasInfos[name];
        } // SetAliasValue
예제 #12
0
        } // SetVariable

        /// <summary>
        /// Sets a variable to the given value.
        /// </summary>
        ///
        /// <param name="newVariable">
        /// The new variable to create.
        /// </param>
        /// 
        /// <param name="force">
        /// If true, the variable will be set even if it is readonly.
        /// </param>
        /// 
        /// <param name="sessionState">
        /// Which SessionState this variable belongs to.
        /// </param>
        /// 
        /// <returns>
        /// The PSVariable representing the variable that was set.
        /// </returns>
        ///
        /// <exception cref="SessionStateUnauthorizedAccessException">
        /// If the variable is read-only or constant.
        /// </exception> 
        /// 
        /// <exception cref="SessionStateOverflowException">
        /// If the maximum number of variables has been reached for this scope.
        /// </exception>
        /// 
        internal PSVariable NewVariable(PSVariable newVariable, bool force, SessionStateInternal sessionState)
        {
            PSVariable variable;
            bool varExists = TryGetVariable(newVariable.Name, ScopeOrigin, true, out variable);

            if (varExists)
            {
                // First check the variable to ensure that it
                // is not constant or readonly

                if (variable == null || variable.IsConstant || (!force && variable.IsReadOnly))
                {
                    SessionStateUnauthorizedAccessException e =
                        new SessionStateUnauthorizedAccessException(
                                newVariable.Name,
                                SessionStateCategory.Variable,
                                "VariableNotWritable",
                                SessionStateStrings.VariableNotWritable);

                    throw e;
                }

                if (variable is LocalVariable)
                {
                    SessionStateUnauthorizedAccessException e =
                        new SessionStateUnauthorizedAccessException(
                                newVariable.Name,
                                SessionStateCategory.Variable,
                                "VariableNotWritableRare",
                                SessionStateStrings.VariableNotWritableRare);

                    throw e;
                }

                // If the new and old variable are the same then don't bother
                // doing the assignment and marking as "removed".
                // This can happen when a module variable is imported twice.
                if (!ReferenceEquals(newVariable, variable))
                {
                    // Mark the old variable as removed...
                    variable.WasRemoved = true;
                    variable = newVariable;
                }
            }
            else
            {
                // Since the variable doesn't exist, use the new Variable
                // object

                variable = newVariable;
            }

            // Now check to make sure we don't exceed the max variable count
            // if this is a new variable.

            if (!varExists && _variables.Count > VariableCapacity.FastValue - 1)
            {
                SessionStateOverflowException e =
                    new SessionStateOverflowException(
                            newVariable.Name,
                            SessionStateCategory.Variable,
                            "VariableOverflow",
                                SessionStateStrings.VariableOverflow,
                            VariableCapacity.FastValue);

                throw e;
            }

            // Don't let people set AllScope variables in ConstrainedLanguage,
            // as they can be used to interfere with the session state of
            // trusted commands.
            if (ExecutionContext.HasEverUsedConstrainedLanguage)
            {
                var context = System.Management.Automation.Runspaces.LocalPipeline.GetExecutionContextFromTLS();

                if ((context != null) &&
                    (context.LanguageMode == PSLanguageMode.ConstrainedLanguage) &&
                    ((variable.Options & ScopedItemOptions.AllScope) == ScopedItemOptions.AllScope))
                {
                    throw new PSNotSupportedException();
                }
            }

            _variables[variable.Name] = variable;
            variable.SessionState = sessionState;
            return variable;
        } // NewVariable
예제 #13
0
        /// <summary>
        /// Sets a variable to the given value.
        /// </summary>
        ///
        /// <param name="name">
        /// The name of the variable to set.
        /// </param>
        ///
        /// <param name="value">
        /// The value for the variable
        /// </param>
        ///
        /// <param name="asValue">
        /// If true, sets the variable value to newValue. If false, newValue must
        /// be a PSVariable object and the item will be set rather than the value.
        /// </param>
        /// 
        /// <param name="force">
        /// If true, the variable will be set even if it is readonly.
        /// </param>
        ///
        /// <param name="sessionState">
        /// Which SessionState this variable belongs to.
        /// </param>
        /// 
        /// <param name="origin">
        /// The origin of the caller
        /// </param>
        /// 
        /// <param name="fastPath">
        /// If true and the variable is being set in the global scope,
        /// then all of the normal variable lookup stuff is bypassed and
        /// the variable is added directly to the dictionary.
        /// </param>
        /// <returns>
        /// The PSVariable representing the variable that was set.
        /// </returns>
        ///
        /// <exception cref="SessionStateUnauthorizedAccessException">
        /// If the variable is read-only or constant.
        /// </exception> 
        /// 
        /// <exception cref="SessionStateOverflowException">
        /// If the maximum number of variables has been reached for this scope.
        /// </exception>
        /// 
        internal PSVariable SetVariable(string name, object value, bool asValue, bool force, SessionStateInternal sessionState, CommandOrigin origin = CommandOrigin.Internal, bool fastPath = false)
        {
            Diagnostics.Assert(name != null, "The caller should verify the name");

            PSVariable variable;
            PSVariable variableToSet = value as PSVariable;

            // Set the variable directly in the table, bypassing all of the checks. This
            // can only be used for global scope otherwise the slow path is used.
            if (fastPath)
            {
                if (Parent != null)
                {
                    throw new NotImplementedException("fastPath");
                }
                variable = new PSVariable(name, variableToSet.Value, variableToSet.Options, variableToSet.Attributes) { Description = variableToSet.Description };
                GetPrivateVariables()[name] = variable;
                return variable;
            }

            bool varExists = TryGetVariable(name, origin, true, out variable);

            // Initialize the private variable dictionary if it's not yet
            if (_variables == null) { GetPrivateVariables(); }

            if (!asValue && variableToSet != null)
            {
                if (varExists)
                {
                    // First check the variable to ensure that it
                    // is not constant or readonly

                    if (variable == null || variable.IsConstant || (!force && variable.IsReadOnly))
                    {
                        SessionStateUnauthorizedAccessException e =
                            new SessionStateUnauthorizedAccessException(
                                    name,
                                    SessionStateCategory.Variable,
                                    "VariableNotWritable",
                                    SessionStateStrings.VariableNotWritable);

                        throw e;
                    }

                    if (variable is LocalVariable
                        && (variableToSet.Attributes.Any() || variableToSet.Options != variable.Options))
                    {
                        SessionStateUnauthorizedAccessException e =
                            new SessionStateUnauthorizedAccessException(
                                    name,
                                    SessionStateCategory.Variable,
                                    "VariableNotWritableRare",
                                    SessionStateStrings.VariableNotWritableRare);

                        throw e;
                    }

                    if (variable.IsReadOnly && force)
                    {
                        _variables.Remove(name);
                        varExists = false;
                        variable = new PSVariable(name, variableToSet.Value, variableToSet.Options, variableToSet.Attributes) { Description = variableToSet.Description };
                    }
                    else
                    {
                        // Since the variable already exists, copy
                        // the value, options, description, and attributes
                        // to it.
                        variable.Attributes.Clear();

                        variable.Value = variableToSet.Value;
                        variable.Options = variableToSet.Options;
                        variable.Description = variableToSet.Description;

                        foreach (Attribute attr in variableToSet.Attributes)
                        {
                            variable.Attributes.Add(attr);
                        }
                    }
                }
                else
                {
                    // Since the variable doesn't exist, use the new Variable
                    // object

                    variable = variableToSet;
                }
            }
            else if (variable != null)
            {
                variable.Value = value;
            }
            else
            {
                variable = (LocalsTuple != null ? LocalsTuple.TrySetVariable(name, value) : null) ?? new PSVariable(name, value);
            }

            // Now check to make sure we don't exceed the max variable count
            // if this is a new variable.

            if (!varExists && _variables.Count > VariableCapacity.FastValue - 1)
            {
                SessionStateOverflowException e =
                    new SessionStateOverflowException(
                            name,
                            SessionStateCategory.Variable,
                            "VariableOverflow",
                                SessionStateStrings.VariableOverflow,
                            VariableCapacity.FastValue);

                throw e;
            }

            // Don't let people set AllScope variables in ConstrainedLanguage,
            // as they can be used to interfere with the session state of
            // trusted commands.
            if (ExecutionContext.HasEverUsedConstrainedLanguage)
            {
                var context = System.Management.Automation.Runspaces.LocalPipeline.GetExecutionContextFromTLS();

                if ((context != null) &&
                    (context.LanguageMode == PSLanguageMode.ConstrainedLanguage) &&
                    ((variable.Options & ScopedItemOptions.AllScope) == ScopedItemOptions.AllScope))
                {
                    throw new PSNotSupportedException();
                }
            }


            _variables[name] = variable;
            variable.SessionState = sessionState;
            return variable;
        } // SetVariable
예제 #14
0
        /// <summary>
        /// Adds a new drive to the scope's drive collection.
        /// </summary>
        /// 
        /// <param name="newDrive">
        /// The new drive to be added.
        /// </param>
        /// 
        /// <remarks>
        /// This method assumes the drive has already been verified and
        /// the provider has already been notified.
        /// </remarks>
        /// 
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="newDrive" /> is null.
        /// </exception>
        /// 
        /// <exception cref="SessionStateException">
        /// If a drive of the same name already exists in this scope.
        /// </exception>
        /// 
        /// <exception cref="SessionStateOverflowException">
        /// If the maximum drive count has been reached for this scope.
        /// </exception>
        /// 
        internal void NewDrive(PSDriveInfo newDrive)
        {
            if (newDrive == null)
            {
                throw PSTraceSource.NewArgumentNullException("newDrive");
            }

            // Ensure that multiple threads do not try to modify the
            // drive data at the same time.

            var driveInfos = GetDrives();
            if (driveInfos.ContainsKey(newDrive.Name))
            {
                SessionStateException e =
                    new SessionStateException(
                        newDrive.Name,
                        SessionStateCategory.Drive,
                        "DriveAlreadyExists",
                        SessionStateStrings.DriveAlreadyExists,
                        ErrorCategory.ResourceExists);

                throw e;
            }

            if (!newDrive.IsAutoMounted &&
                driveInfos.Count > DriveCapacity.FastValue - 1)
            {
                SessionStateOverflowException e =
                    new SessionStateOverflowException(
                        newDrive.Name,
                        SessionStateCategory.Drive,
                        "DriveOverflow",
                        SessionStateStrings.DriveOverflow,
                        DriveCapacity.FastValue);

                throw e;
            }

            if (!newDrive.IsAutoMounted)
            {
                driveInfos.Add(newDrive.Name, newDrive);
            }
            else
            {
                var automountedDrives = GetAutomountedDrives();
                if (!automountedDrives.ContainsKey(newDrive.Name))
                {
                    automountedDrives.Add(newDrive.Name, newDrive);
                }
            }
        } // New Drive
예제 #15
0
        /// <summary>
        /// Sets an function to the given function declaration.
        /// </summary>
        ///
        /// <param name="name">
        /// The name of the function to set.
        /// </param>
        ///
        /// <param name="function">
        /// The script block that the function should represent.
        /// </param>
        ///
        /// <param name="originalFunction">
        /// The original function (if any) from which the scriptblock was derived.
        /// </param>
        ///
        /// <param name="options">
        /// The options that should be applied to the function.
        /// </param>
        /// 
        /// <param name="force">
        /// If true, the function will be set even if its ReadOnly.
        /// </param>
        /// 
        /// <param name="origin">
        /// The origin of the caller of this API
        /// </param>
        /// 
        /// <param name="context">
        /// The execution context for the function/filter.
        /// </param>
        /// 
        /// <param name="helpFile">
        /// The name of the help file associated with the function.
        /// </param>
        /// 
        /// <param name="functionFactory">
        /// Function to create the FunctionInfo.
        /// </param>
        ///
        /// <returns>
        /// A FunctionInfo that is either a FilterInfo or FunctionInfo representing the
        /// function or filter.
        /// </returns>
        ///
        /// <exception cref="SessionStateUnauthorizedAccessException">
        /// If the function is read-only or constant.
        /// </exception> 
        /// 
        /// <exception cref="SessionStateOverflowException">
        /// If the maximum number of functions have been reached for this scope.
        /// </exception>
        /// 
        internal FunctionInfo SetFunction(
            string name,
            ScriptBlock function,
            FunctionInfo originalFunction,
            ScopedItemOptions options,
            bool force,
            CommandOrigin origin,
            ExecutionContext context,
            string helpFile,
            Func<string, ScriptBlock, FunctionInfo, ScopedItemOptions, ExecutionContext, string, FunctionInfo> functionFactory)
        {
            Diagnostics.Assert(
                name != null,
                "The caller should verify the name");

            var functionInfos = GetFunctions();
            FunctionInfo existingValue;
            FunctionInfo result;
            if (!functionInfos.TryGetValue(name, out existingValue))
            {
                if (functionInfos.Count > FunctionCapacity.FastValue - 1)
                {
                    SessionStateOverflowException e =
                        new SessionStateOverflowException(
                                name,
                                SessionStateCategory.Function,
                                "FunctionOverflow",
                                SessionStateStrings.FunctionOverflow,
                                FunctionCapacity.FastValue);

                    throw e;
                }

                result = functionFactory(name, function, originalFunction, options, context, helpFile);
                functionInfos[name] = result;

                if (IsFunctionOptionSet(result, ScopedItemOptions.AllScope))
                {
                    GetAllScopeFunctions()[name] = result;
                }
            }
            else
            {
                // Make sure the function isn't constant or readonly

                SessionState.ThrowIfNotVisible(origin, existingValue);

                if (IsFunctionOptionSet(existingValue, ScopedItemOptions.Constant) ||
                    (!force && IsFunctionOptionSet(existingValue, ScopedItemOptions.ReadOnly)))
                {
                    SessionStateUnauthorizedAccessException e =
                        new SessionStateUnauthorizedAccessException(
                                name,
                                SessionStateCategory.Function,
                                "FunctionNotWritable",
                                SessionStateStrings.FunctionNotWritable);

                    throw e;
                }

                // Ensure we are not trying to set the function to constant as this can only be
                // done at creation time.

                if ((options & ScopedItemOptions.Constant) != 0)
                {
                    SessionStateUnauthorizedAccessException e =
                        new SessionStateUnauthorizedAccessException(
                                name,
                                SessionStateCategory.Function,
                                "FunctionCannotBeMadeConstant",
                                SessionStateStrings.FunctionCannotBeMadeConstant);

                    throw e;
                }

                // Ensure we are not trying to remove the AllScope option

                if ((options & ScopedItemOptions.AllScope) == 0 &&
                    IsFunctionOptionSet(existingValue, ScopedItemOptions.AllScope))
                {
                    SessionStateUnauthorizedAccessException e =
                        new SessionStateUnauthorizedAccessException(
                                name,
                                SessionStateCategory.Function,
                                "FunctionAllScopeOptionCannotBeRemoved",
                                SessionStateStrings.FunctionAllScopeOptionCannotBeRemoved);

                    throw e;
                }

                FunctionInfo existingFunction = existingValue;
                FunctionInfo newValue = null;

                // If the function type changes (i.e.: function to workflow or back)
                // then we need to blast what was there
                newValue = functionFactory(name, function, originalFunction, options, context, helpFile);

                bool changesFunctionType = existingFunction.GetType() != newValue.GetType();

                // Since the options are set after the script block, we have to
                // forcefully apply the script block if the options will be
                // set to not being ReadOnly
                if (changesFunctionType ||
                    ((existingFunction.Options & ScopedItemOptions.ReadOnly) != 0 && force))
                {
                    result = newValue;
                    functionInfos[name] = newValue;
                }
                else
                {
                    bool applyForce = force || (options & ScopedItemOptions.ReadOnly) == 0;

                    existingFunction.Update(newValue, applyForce, options, helpFile);
                    result = existingFunction;
                }
            }

            return result;
        } // SetFunction
예제 #16
0
 internal PSVariable NewVariable(PSVariable newVariable, bool force, SessionStateInternal sessionState)
 {
     PSVariable variable;
     bool flag = this.TryGetVariable(newVariable.Name, this.ScopeOrigin, true, out variable);
     if (flag)
     {
         if (((variable == null) || variable.IsConstant) || (!force && variable.IsReadOnly))
         {
             SessionStateUnauthorizedAccessException exception = new SessionStateUnauthorizedAccessException(newVariable.Name, SessionStateCategory.Variable, "VariableNotWritable", SessionStateStrings.VariableNotWritable);
             throw exception;
         }
         if (variable is LocalVariable)
         {
             SessionStateUnauthorizedAccessException exception2 = new SessionStateUnauthorizedAccessException(newVariable.Name, SessionStateCategory.Variable, "VariableNotWritableRare", SessionStateStrings.VariableNotWritableRare);
             throw exception2;
         }
         if (!object.ReferenceEquals(newVariable, variable))
         {
             variable.WasRemoved = true;
             variable = newVariable;
         }
     }
     else
     {
         variable = newVariable;
     }
     if (!flag && (this._variables.Count > (this.VariableCapacity.FastValue - 1)))
     {
         SessionStateOverflowException exception3 = new SessionStateOverflowException(newVariable.Name, SessionStateCategory.Variable, "VariableOverflow", SessionStateStrings.VariableOverflow, new object[] { this.VariableCapacity.FastValue });
         throw exception3;
     }
     if (System.Management.Automation.ExecutionContext.HasEverUsedConstrainedLanguage)
     {
         System.Management.Automation.ExecutionContext executionContextFromTLS = LocalPipeline.GetExecutionContextFromTLS();
         if (((executionContextFromTLS != null) && (executionContextFromTLS.LanguageMode == PSLanguageMode.ConstrainedLanguage)) && ((variable.Options & ScopedItemOptions.AllScope) == ScopedItemOptions.AllScope))
         {
             throw new PSNotSupportedException();
         }
     }
     this._variables[variable.Name] = variable;
     variable.SessionState = sessionState;
     return variable;
 }
예제 #17
0
 internal AliasInfo SetAliasItem(AliasInfo aliasToSet, bool force, CommandOrigin origin = CommandOrigin.Internal)
 {
     if (!this.GetAliases().ContainsKey(aliasToSet.Name))
     {
         if (this.GetAliases().Count > (this.AliasCapacity.FastValue - 1))
         {
             SessionStateOverflowException exception = new SessionStateOverflowException(aliasToSet.Name, SessionStateCategory.Alias, "AliasOverflow", SessionStateStrings.AliasOverflow, new object[] { this.AliasCapacity.FastValue });
             throw exception;
         }
         this.GetAliases()[aliasToSet.Name] = aliasToSet;
     }
     else
     {
         AliasInfo valueToCheck = this.GetAliases()[aliasToSet.Name];
         SessionState.ThrowIfNotVisible(origin, valueToCheck);
         if (((valueToCheck.Options & ScopedItemOptions.Constant) != ScopedItemOptions.None) || (((valueToCheck.Options & ScopedItemOptions.ReadOnly) != ScopedItemOptions.None) && !force))
         {
             SessionStateUnauthorizedAccessException exception2 = new SessionStateUnauthorizedAccessException(aliasToSet.Name, SessionStateCategory.Alias, "AliasNotWritable", SessionStateStrings.AliasNotWritable);
             throw exception2;
         }
         if (((aliasToSet.Options & ScopedItemOptions.AllScope) == ScopedItemOptions.None) && ((valueToCheck.Options & ScopedItemOptions.AllScope) != ScopedItemOptions.None))
         {
             SessionStateUnauthorizedAccessException exception3 = new SessionStateUnauthorizedAccessException(aliasToSet.Name, SessionStateCategory.Alias, "AliasAllScopeOptionCannotBeRemoved", SessionStateStrings.AliasAllScopeOptionCannotBeRemoved);
             throw exception3;
         }
         this.RemoveAliasFromCache(valueToCheck.Name, valueToCheck.Definition);
         this.GetAliases()[aliasToSet.Name] = aliasToSet;
     }
     this.AddAliasToCache(aliasToSet.Name, aliasToSet.Definition);
     return this.GetAliases()[aliasToSet.Name];
 }
예제 #18
0
        internal PSVariable SetVariable(string name, object value, bool asValue, bool force, SessionStateInternal sessionState, CommandOrigin origin = CommandOrigin.Internal, bool fastPath = false)
        {
            PSVariable variable;
            PSVariable variable2 = value as PSVariable;

            if (fastPath)
            {
                if (this.Parent != null)
                {
                    throw new NotImplementedException("fastPath");
                }
                variable = new PSVariable(name, variable2.Value, variable2.Options, variable2.Attributes)
                {
                    Description = variable2.Description
                };
                this.GetPrivateVariables()[name] = variable;
                return(variable);
            }
            bool flag = this.TryGetVariable(name, origin, true, out variable);

            if (!asValue && (variable2 != null))
            {
                if (flag)
                {
                    if (((variable == null) || variable.IsConstant) || (!force && variable.IsReadOnly))
                    {
                        SessionStateUnauthorizedAccessException exception = new SessionStateUnauthorizedAccessException(name, SessionStateCategory.Variable, "VariableNotWritable", SessionStateStrings.VariableNotWritable);
                        throw exception;
                    }
                    if ((variable is LocalVariable) && (variable2.Attributes.Any <Attribute>() || (variable2.Options != variable.Options)))
                    {
                        SessionStateUnauthorizedAccessException exception2 = new SessionStateUnauthorizedAccessException(name, SessionStateCategory.Variable, "VariableNotWritableRare", SessionStateStrings.VariableNotWritableRare);
                        throw exception2;
                    }
                    if (variable.IsReadOnly && force)
                    {
                        this._variables.Remove(name);
                        flag     = false;
                        variable = new PSVariable(name, variable2.Value, variable2.Options, variable2.Attributes)
                        {
                            Description = variable2.Description
                        };
                    }
                    else
                    {
                        variable.Attributes.Clear();
                        variable.Value       = variable2.Value;
                        variable.Options     = variable2.Options;
                        variable.Description = variable2.Description;
                        foreach (Attribute attribute in variable2.Attributes)
                        {
                            variable.Attributes.Add(attribute);
                        }
                    }
                }
                else
                {
                    variable = variable2;
                }
            }
            else if (variable != null)
            {
                variable.Value = value;
            }
            else
            {
                variable = (this.LocalsTuple != null) ? (this.LocalsTuple.TrySetVariable(name, value)) ?? new PSVariable(name, value) : new PSVariable(name, value);
            }
            if (!flag && (this._variables.Count > (this.VariableCapacity.FastValue - 1)))
            {
                SessionStateOverflowException exception3 = new SessionStateOverflowException(name, SessionStateCategory.Variable, "VariableOverflow", SessionStateStrings.VariableOverflow, new object[] { this.VariableCapacity.FastValue });
                throw exception3;
            }
            if (System.Management.Automation.ExecutionContext.HasEverUsedConstrainedLanguage)
            {
                System.Management.Automation.ExecutionContext executionContextFromTLS = LocalPipeline.GetExecutionContextFromTLS();
                if (((executionContextFromTLS != null) && (executionContextFromTLS.LanguageMode == PSLanguageMode.ConstrainedLanguage)) && ((variable.Options & ScopedItemOptions.AllScope) == ScopedItemOptions.AllScope))
                {
                    /* TODO: Review how to  get around this: */ /* throw new PSNotSupportedException(); */
                }
            }
            this._variables[name] = variable;
            variable.SessionState = sessionState;
            return(variable);
        }
예제 #19
0
        } // SetAliasValue

        /// <summary>
        /// Sets an alias to the given value.
        /// </summary>
        ///
        /// <param name="name">
        /// The name of the alias to set.
        /// </param>
        ///
        /// <param name="value">
        /// The value for the alias
        /// </param>
        ///
        /// <param name="context">
        /// The execution context for this engine instance.
        /// </param>
        /// 
        /// <param name="options">
        /// The options to set on the alias.
        /// </param>
        /// 
        /// <param name="force">
        /// If true, the value will be set even if the alias is ReadOnly.
        /// </param>
        /// 
        /// <param name="origin">
        /// Origin of the caller of this API
        /// </param>
        /// 
        /// <returns>
        /// The string representing the value that was set.
        /// </returns>
        ///
        /// <exception cref="SessionStateUnauthorizedAccessException">
        /// If the alias is read-only or constant.
        /// </exception>
        /// 
        /// <exception cref="SessionStateOverflowException">
        /// If the maximum number of aliases has been reached for this scope.
        /// </exception>
        /// 
        internal AliasInfo SetAliasValue(
            string name,
            string value,
            ScopedItemOptions options,
            ExecutionContext context,
            bool force,
            CommandOrigin origin)
        {
            Diagnostics.Assert(
                name != null,
                "The caller should verify the name");

            var aliasInfos = GetAliases();
            AliasInfo aliasInfo;
            AliasInfo result;
            if (!aliasInfos.TryGetValue(name, out aliasInfo))
            {
                if (aliasInfos.Count > AliasCapacity.FastValue - 1)
                {
                    SessionStateOverflowException e =
                        new SessionStateOverflowException(
                                name,
                                SessionStateCategory.Alias,
                                "AliasOverflow",
                                SessionStateStrings.AliasOverflow,
                                AliasCapacity.FastValue);

                    throw e;
                }
                result = new AliasInfo(name, value, context, options);
                aliasInfos[name] = result;
            }
            else
            {
                // Make sure the alias isn't constant or readonly
                if ((aliasInfo.Options & ScopedItemOptions.Constant) != 0 ||
                    (!force && (aliasInfo.Options & ScopedItemOptions.ReadOnly) != 0))
                {
                    SessionStateUnauthorizedAccessException e =
                        new SessionStateUnauthorizedAccessException(
                                name,
                                SessionStateCategory.Alias,
                                "AliasNotWritable",
                                SessionStateStrings.AliasNotWritable);

                    throw e;
                }


                // Ensure we are not trying to set the alias to constant as this can only be
                // done at creation time.

                if ((options & ScopedItemOptions.Constant) != 0)
                {
                    SessionStateUnauthorizedAccessException e =
                        new SessionStateUnauthorizedAccessException(
                                name,
                                SessionStateCategory.Alias,
                                "AliasCannotBeMadeConstant",
                                SessionStateStrings.AliasCannotBeMadeConstant);

                    throw e;
                }

                if ((options & ScopedItemOptions.AllScope) == 0 &&
                    (aliasInfo.Options & ScopedItemOptions.AllScope) != 0)
                {
                    // user is trying to remove the AllScope option from the alias. 
                    // Do not allow this (as per spec).

                    SessionStateUnauthorizedAccessException e =
                        new SessionStateUnauthorizedAccessException(
                                name,
                                SessionStateCategory.Alias,
                                "AliasAllScopeOptionCannotBeRemoved",
                                SessionStateStrings.AliasAllScopeOptionCannotBeRemoved);

                    throw e;
                }

                SessionState.ThrowIfNotVisible(origin, aliasInfo);
                RemoveAliasFromCache(aliasInfo.Name, aliasInfo.Definition);

                if (force)
                {
                    aliasInfos.Remove(name);
                    result = new AliasInfo(name, value, context, options);
                    aliasInfos[name] = result;
                }
                else
                {
                    result = aliasInfo;
                    aliasInfo.Options = options;
                    aliasInfo.SetDefinition(value, false);
                }
            }

            AddAliasToCache(name, value);

            return result;
        } // SetAliasValue