/// <summary>
        /// Removes a cmdlet entry from the cmdlet table.
        /// </summary>
        /// <param name="name">
        /// The name of the cmdlet entry to remove.
        /// </param>
        /// <param name="force">
        /// If true, the cmdlet is removed even if it is ReadOnly.
        /// </param>
        /// <exception cref="ArgumentException">
        /// If <paramref name="name"/> is null or empty.
        /// </exception>
        /// <exception cref="SessionStateUnauthorizedAccessException">
        /// If the function is constant.
        /// </exception>
        internal void RemoveCmdletEntry(string name, bool force)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw PSTraceSource.NewArgumentException("name");
            }

            // Use the scope enumerator to find an existing function

            SessionStateScopeEnumerator scopeEnumerator =
                new SessionStateScopeEnumerator(_currentScope);

            foreach (SessionStateScope scope in scopeEnumerator)
            {
                CmdletInfo cmdletInfo =
                    scope.GetCmdlet(name);

                if (cmdletInfo != null)
                {
                    // Make sure the cmdlet isn't private or if it is that the current
                    // scope is the same scope the cmdlet was retrieved from.

                    if ((cmdletInfo.Options & ScopedItemOptions.Private) != 0 &&
                        scope != _currentScope)
                    {
                        cmdletInfo = null;
                    }
                    else
                    {
                        scope.RemoveCmdletEntry(name, force);
                        break;
                    }
                }
            }
        }
        private void InitializeScopeEnumerator()
        {
            // Define the lookup scope and if we have to do single
            // level or dynamic lookup based on the lookup variable

            _initialScope = sessionState.CurrentScope;

            if (_lookupPath.IsGlobal)
            {
                _initialScope        = sessionState.GlobalScope;
                _isSingleScopeLookup = true;
            }
            else if (_lookupPath.IsLocal ||
                     _lookupPath.IsPrivate)
            {
                _initialScope        = sessionState.CurrentScope;
                _isSingleScopeLookup = true;
            }
            else if (_lookupPath.IsScript)
            {
                _initialScope        = sessionState.ScriptScope;
                _isSingleScopeLookup = true;
            }

            _scopeEnumerable =
                new SessionStateScopeEnumerator(_initialScope);

            _isInitialized = true;
        }
        /// <summary>
        /// Gets an IEnumerable for the alias table.
        /// </summary>
        internal IDictionary <string, AliasInfo> GetAliasTable()
        {
            // On 7.0 version we have 132 aliases so we set a larger number to reduce re-allocations.
            const int InitialAliasCount           = 150;
            Dictionary <string, AliasInfo> result =
                new Dictionary <string, AliasInfo>(InitialAliasCount, StringComparer.OrdinalIgnoreCase);

            SessionStateScopeEnumerator scopeEnumerator =
                new SessionStateScopeEnumerator(_currentScope);

            foreach (SessionStateScope scope in scopeEnumerator)
            {
                foreach (AliasInfo entry in scope.AliasTable)
                {
                    if (!result.ContainsKey(entry.Name))
                    {
                        // Make sure the alias isn't private or if it is that the current
                        // scope is the same scope the alias was retrieved from.

                        if ((entry.Options & ScopedItemOptions.Private) == 0 ||
                            scope == _currentScope)
                        {
                            result.Add(entry.Name, entry);
                        }
                    }
                }
            }

            return(result);
        }
        /// <summary>
        /// Gets an IEnumerable for the cmdlet table.
        /// </summary>
        internal IDictionary <string, List <CmdletInfo> > GetCmdletTable()
        {
            Dictionary <string, List <CmdletInfo> > result =
                new Dictionary <string, List <CmdletInfo> >(StringComparer.OrdinalIgnoreCase);

            SessionStateScopeEnumerator scopeEnumerator =
                new SessionStateScopeEnumerator(_currentScope);

            foreach (SessionStateScope scope in scopeEnumerator)
            {
                foreach (KeyValuePair <string, List <CmdletInfo> > entry in scope.CmdletTable)
                {
                    if (!result.ContainsKey(entry.Key))
                    {
                        // Make sure the cmdlet isn't private or if it is that the current
                        // scope is the same scope the alias was retrieved from.

                        List <CmdletInfo> toBeAdded = new List <CmdletInfo>();
                        foreach (CmdletInfo cmdletInfo in entry.Value)
                        {
                            if ((cmdletInfo.Options & ScopedItemOptions.Private) == 0 ||
                                scope == _currentScope)
                            {
                                toBeAdded.Add(cmdletInfo);
                            }
                        }

                        result.Add(entry.Key, toBeAdded);
                    }
                }
            }

            return(result);
        }
Exemple #5
0
        /// <summary>
        /// Gets an IEnumerable for the alias table
        /// </summary>
        ///
        internal IDictionary <string, AliasInfo> GetAliasTable()
        {
            Dictionary <string, AliasInfo> result =
                new Dictionary <string, AliasInfo>(StringComparer.OrdinalIgnoreCase);

            SessionStateScopeEnumerator scopeEnumerator =
                new SessionStateScopeEnumerator(_currentScope);

            foreach (SessionStateScope scope in scopeEnumerator)
            {
                foreach (AliasInfo entry in scope.AliasTable)
                {
                    if (!result.ContainsKey(entry.Name))
                    {
                        // Make sure the alias isn't private or if it is that the current
                        // scope is the same scope the alias was retrieved from.

                        if ((entry.Options & ScopedItemOptions.Private) == 0 ||
                            scope == _currentScope)
                        {
                            result.Add(entry.Name, entry);
                        }
                    }
                }
            }

            return(result);
        } // GetAliasTable
 public void Dispose()
 {
     this.current = default(T);
     this.scopeEnumerable.Dispose();
     this.scopeEnumerable = (SessionStateScopeEnumerator)null;
     this.isInitialized   = false;
     GC.SuppressFinalize((object)this);
 }
 public void Dispose()
 {
     this.current = default(T);
     this.scopeEnumerable.Dispose();
     this.scopeEnumerable = null;
     this.isInitialized   = false;
     GC.SuppressFinalize(this);
 }
Exemple #8
0
        } // RemoveAlias

        /// <summary>
        /// Gets the aliases by command name (used by metadata-driven help)
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        internal IEnumerable <string> GetAliasesByCommandName(string command)
        {
            SessionStateScopeEnumerator scopeEnumerator =
                new SessionStateScopeEnumerator(_currentScope);

            foreach (SessionStateScope scope in scopeEnumerator)
            {
                foreach (string alias in scope.GetAliasesByCommandName(command))
                {
                    yield return(alias);
                }
            }

            yield break;
        }
        /// <summary>
        /// Gets the value of the specified alias from the alias table.
        /// </summary>
        ///
        /// <param name="aliasName">
        /// The name of the alias value to retrieve.
        /// </param>
        ///
        /// <param name="origin">
        /// The origin of the command calling this API.
        /// </param>
        /// <returns>
        /// The AliasInfo representing the alias.
        /// </returns>
        ///
        internal AliasInfo GetAlias(string aliasName, CommandOrigin origin)
        {
            AliasInfo result = null;

            if (String.IsNullOrEmpty(aliasName))
            {
                return(null);
            }


            // Use the scope enumerator to find the alias using the
            // appropriate scoping rules

            SessionStateScopeEnumerator scopeEnumerator =
                new SessionStateScopeEnumerator(_currentScope);

            foreach (SessionStateScope scope in scopeEnumerator)
            {
                result = scope.GetAlias(aliasName);

                if (result != null)
                {
                    // Now check the visibility of the variable...
                    SessionState.ThrowIfNotVisible(origin, result);

                    // Make sure the alias isn't private or if it is that the current
                    // scope is the same scope the alias was retrieved from.

                    if ((result.Options & ScopedItemOptions.Private) != 0 &&
                        scope != _currentScope)
                    {
                        result = null;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(result);
        } // GetAlias
Exemple #10
0
        /// <summary>
        /// Gets a flattened view of the functions that are visible using
        /// the current scope as a reference and filtering the functions in
        /// the other scopes based on the scoping rules.
        /// </summary>
        /// <returns>
        /// An IDictionary representing the visible functions.
        /// </returns>
        internal IDictionary GetFunctionTable()
        {
            SessionStateScopeEnumerator scopeEnumerator =
                new SessionStateScopeEnumerator(_currentScope);

            Dictionary <string, FunctionInfo> result =
                new Dictionary <string, FunctionInfo>(StringComparer.OrdinalIgnoreCase);

            foreach (SessionStateScope scope in scopeEnumerator)
            {
                foreach (FunctionInfo entry in scope.FunctionTable.Values)
                {
                    if (!result.ContainsKey(entry.Name))
                    {
                        result.Add(entry.Name, entry);
                    }
                }
            }

            return(result);
        }
 private void InitializeScopeEnumerator()
 {
     this.initialScope = this.sessionState.CurrentScope;
     if (this.lookupPath.IsGlobal)
     {
         this.initialScope        = this.sessionState.GlobalScope;
         this.isSingleScopeLookup = true;
     }
     else if (this.lookupPath.IsLocal || this.lookupPath.IsPrivate)
     {
         this.initialScope        = this.sessionState.CurrentScope;
         this.isSingleScopeLookup = true;
     }
     else if (this.lookupPath.IsScript)
     {
         this.initialScope        = this.sessionState.ScriptScope;
         this.isSingleScopeLookup = true;
     }
     this.scopeEnumerable = new SessionStateScopeEnumerator(this.sessionState, this.initialScope);
     this.isInitialized   = true;
 }
        /// <summary>
        /// Removes the specified alias.
        /// </summary>
        ///
        /// <param name="aliasName">
        /// The name of the alias to remove.
        /// </param>
        ///
        /// <param name="force">
        /// If true the alias will be removed even if its ReadOnly.
        /// </param>
        ///
        /// <exception cref="ArgumentException">
        /// If <paramref name="aliasName"/> is null or empty.
        /// </exception>
        ///
        /// <exception cref="SessionStateUnauthorizedAccessException">
        /// If the alias is constant.
        /// </exception>
        ///
        internal void RemoveAlias(string aliasName, bool force)
        {
            if (String.IsNullOrEmpty(aliasName))
            {
                throw PSTraceSource.NewArgumentException("aliasName");
            }

            // Use the scope enumerator to find an existing function

            SessionStateScopeEnumerator scopeEnumerator =
                new SessionStateScopeEnumerator(_currentScope);

            foreach (SessionStateScope scope in scopeEnumerator)
            {
                AliasInfo alias =
                    scope.GetAlias(aliasName);


                if (alias != null)
                {
                    // Make sure the alias isn't private or if it is that the current
                    // scope is the same scope the alias was retrieved from.

                    if ((alias.Options & ScopedItemOptions.Private) != 0 &&
                        scope != _currentScope)
                    {
                        alias = null;
                    }
                    else
                    {
                        scope.RemoveAlias(aliasName, force);

                        break;
                    }
                }
            }
        } // RemoveAlias