Ejemplo n.º 1
0
        /// <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(nameof(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;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        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;
        }
Ejemplo n.º 3
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
Ejemplo n.º 4
0
        /// <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);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Gets the value of the specified cmdlet from the cmdlet table.
        /// </summary>
        /// <param name="cmdletName">
        /// The name of the cmdlet value to retrieve.
        /// </param>
        /// <param name="origin">
        /// The origin of hte command trying to retrieve this cmdlet.
        /// </param>
        /// <returns>
        /// The CmdletInfo representing the cmdlet.
        /// </returns>
        internal CmdletInfo GetCmdlet(string cmdletName, CommandOrigin origin)
        {
            CmdletInfo result = null;

            if (string.IsNullOrEmpty(cmdletName))
            {
                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.GetCmdlet(cmdletName);

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

                    // 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 ((result.Options & ScopedItemOptions.Private) != 0 &&
                        scope != _currentScope)
                    {
                        result = null;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 6
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;
        } // GetFunctionTable
Ejemplo n.º 7
0
        /// <summary>
        /// Gets the value of the specified cmdlet from the cmdlet table.
        /// </summary>
        /// 
        /// <param name="cmdletName">
        /// The name of the cmdlet value to retrieve.
        /// </param>
        /// 
        /// <param name="origin">
        /// The origin of hte command trying to retrieve this cmdlet.
        /// </param>
        /// 
        /// <returns>
        /// The CmdletInfo representing the cmdlet.
        /// </returns>
        /// 
        internal CmdletInfo GetCmdlet(string cmdletName, CommandOrigin origin)
        {
            CmdletInfo result = null;
            if (String.IsNullOrEmpty(cmdletName))
            {
                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.GetCmdlet(cmdletName);

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

                    // 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 ((result.Options & ScopedItemOptions.Private) != 0 &&
                        scope != _currentScope)
                    {
                        result = null;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return result;
        } // GetCmdlet
Ejemplo n.º 8
0
 internal IDictionary<string, PSVariable> GetVariableTable()
 {
     SessionStateScopeEnumerator enumerator = new SessionStateScopeEnumerator(this.currentScope);
     Dictionary<string, PSVariable> result = new Dictionary<string, PSVariable>(StringComparer.OrdinalIgnoreCase);
     foreach (SessionStateScope scope in (IEnumerable<SessionStateScope>) enumerator)
     {
         this.GetScopeVariableTable(scope, result, scope == this.currentScope);
     }
     return result;
 }
Ejemplo n.º 9
0
 internal IDictionary GetFunctionTable()
 {
     SessionStateScopeEnumerator enumerator = new SessionStateScopeEnumerator(this.currentScope);
     Dictionary<string, FunctionInfo> dictionary = new Dictionary<string, FunctionInfo>(StringComparer.OrdinalIgnoreCase);
     foreach (SessionStateScope scope in (IEnumerable<SessionStateScope>) enumerator)
     {
         foreach (FunctionInfo info in scope.FunctionTable.Values)
         {
             if (!dictionary.ContainsKey(info.Name))
             {
                 dictionary.Add(info.Name, info);
             }
         }
     }
     return dictionary;
 }
Ejemplo n.º 10
0
 internal PSDriveInfo GetDrive(string name, string scopeID)
 {
     if (name == null)
     {
         throw PSTraceSource.NewArgumentNullException("name");
     }
     PSDriveInfo drive = null;
     if (!string.IsNullOrEmpty(scopeID))
     {
         SessionStateScope scopeByID = this.GetScopeByID(scopeID);
         drive = scopeByID.GetDrive(name);
         if (drive != null)
         {
             if (drive.IsAutoMounted && !this.ValidateOrRemoveAutoMountedDrive(drive, scopeByID))
             {
                 drive = null;
             }
             return drive;
         }
         if (scopeByID == this._globalScope)
         {
             drive = this.AutomountFileSystemDrive(name);
         }
         return drive;
     }
     SessionStateScopeEnumerator enumerator = new SessionStateScopeEnumerator(this.CurrentScope);
     foreach (SessionStateScope scope in (IEnumerable<SessionStateScope>) enumerator)
     {
         drive = scope.GetDrive(name);
         if (drive != null)
         {
             if (drive.IsAutoMounted && !this.ValidateOrRemoveAutoMountedDrive(drive, scope))
             {
                 drive = null;
             }
             if (drive != null)
             {
                 break;
             }
         }
     }
     if (drive == null)
     {
         drive = this.AutomountFileSystemDrive(name);
     }
     return drive;
 }
Ejemplo n.º 11
0
        } // GetCmdletAtScope

        /// <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;
        } // GetCmdletTable
Ejemplo n.º 12
0
 internal IEnumerator<CmdletInfo> GetCmdletInfo(string cmdletName, bool searchAllScopes)
 {
     PSSnapinQualifiedName instance = PSSnapinQualifiedName.GetInstance(cmdletName);
     if (instance != null)
     {
         SessionStateScopeEnumerator iteratorVariable1 = new SessionStateScopeEnumerator(this._context.EngineSessionState.CurrentScope);
         foreach (SessionStateScope iteratorVariable2 in (IEnumerable<SessionStateScope>) iteratorVariable1)
         {
             List<CmdletInfo> iteratorVariable3;
             if (iteratorVariable2.CmdletTable.TryGetValue(instance.ShortName, out iteratorVariable3))
             {
                 foreach (CmdletInfo iteratorVariable4 in iteratorVariable3)
                 {
                     if (!string.IsNullOrEmpty(instance.PSSnapInName))
                     {
                         if (string.Equals(iteratorVariable4.ModuleName, instance.PSSnapInName, StringComparison.OrdinalIgnoreCase))
                         {
                             yield return iteratorVariable4;
                             if (searchAllScopes)
                             {
                                 continue;
                             }
                             break;
                         }
                         if (InitialSessionState.IsEngineModule(iteratorVariable4.ModuleName) && string.Equals(iteratorVariable4.ModuleName, InitialSessionState.GetNestedModuleDllName(instance.PSSnapInName), StringComparison.OrdinalIgnoreCase))
                         {
                             yield return iteratorVariable4;
                             if (!searchAllScopes)
                             {
                                 break;
                             }
                         }
                     }
                     else
                     {
                         yield return iteratorVariable4;
                         if (!searchAllScopes)
                         {
                             break;
                         }
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 13
0
        /// <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
Ejemplo n.º 14
0
 internal void RemoveDrive(PSDriveInfo drive, bool force, string scopeID, CmdletProviderContext context)
 {
     bool flag = false;
     try
     {
         flag = this.CanRemoveDrive(drive, context);
     }
     catch (LoopFlowException)
     {
         throw;
     }
     catch (PipelineStoppedException)
     {
         throw;
     }
     catch (ActionPreferenceStopException)
     {
         throw;
     }
     catch (ProviderInvocationException)
     {
         if (!force)
         {
             throw;
         }
     }
     if (flag || force)
     {
         if (!string.IsNullOrEmpty(scopeID))
         {
             this.GetScopeByID(scopeID).RemoveDrive(drive);
             if (this.ProvidersCurrentWorkingDrive[drive.Provider] == drive)
             {
                 this.ProvidersCurrentWorkingDrive[drive.Provider] = null;
             }
         }
         else
         {
             SessionStateScopeEnumerator enumerator = new SessionStateScopeEnumerator(this.CurrentScope);
             foreach (SessionStateScope scope in (IEnumerable<SessionStateScope>) enumerator)
             {
                 try
                 {
                     PSDriveInfo info = scope.GetDrive(drive.Name);
                     if (info != null)
                     {
                         scope.RemoveDrive(drive);
                         if (this.ProvidersCurrentWorkingDrive[drive.Provider] == info)
                         {
                             this.ProvidersCurrentWorkingDrive[drive.Provider] = null;
                         }
                         break;
                     }
                 }
                 catch (ArgumentException)
                 {
                 }
             }
         }
     }
     else
     {
         PSInvalidOperationException replaceParentContainsErrorRecordException = PSTraceSource.NewInvalidOperationException("SessionStateStrings", "DriveRemovalPreventedByProvider", new object[] { drive.Name, drive.Provider });
         context.WriteError(new ErrorRecord(replaceParentContainsErrorRecordException.ErrorRecord, replaceParentContainsErrorRecordException));
     }
 }
Ejemplo n.º 15
0
 internal IDictionary<string, AliasInfo> GetAliasTable()
 {
     Dictionary<string, AliasInfo> dictionary = new Dictionary<string, AliasInfo>(StringComparer.OrdinalIgnoreCase);
     SessionStateScopeEnumerator enumerator = new SessionStateScopeEnumerator(this.currentScope);
     foreach (SessionStateScope scope in (IEnumerable<SessionStateScope>) enumerator)
     {
         foreach (AliasInfo info in scope.AliasTable)
         {
             if (!dictionary.ContainsKey(info.Name) && (((info.Options & ScopedItemOptions.Private) == ScopedItemOptions.None) || (scope == this.currentScope)))
             {
                 dictionary.Add(info.Name, info);
             }
         }
     }
     return dictionary;
 }
Ejemplo n.º 16
0
 internal IEnumerable<string> GetAliasesByCommandName(string command)
 {
     SessionStateScopeEnumerator iteratorVariable0 = new SessionStateScopeEnumerator(this.currentScope);
     foreach (SessionStateScope iteratorVariable1 in (IEnumerable<SessionStateScope>) iteratorVariable0)
     {
         foreach (string iteratorVariable2 in iteratorVariable1.GetAliasesByCommandName(command))
         {
             yield return iteratorVariable2;
         }
     }
 }
Ejemplo n.º 17
0
 internal Collection<PSDriveInfo> Drives(string scope)
 {
     Dictionary<string, PSDriveInfo> dictionary = new Dictionary<string, PSDriveInfo>();
     SessionStateScope currentScope = this.currentScope;
     if (!string.IsNullOrEmpty(scope))
     {
         currentScope = this.GetScopeByID(scope);
     }
     SessionStateScopeEnumerator enumerator = new SessionStateScopeEnumerator(currentScope);
     foreach (SessionStateScope scope3 in (IEnumerable<SessionStateScope>) enumerator)
     {
         foreach (PSDriveInfo info in scope3.Drives)
         {
             if (info != null)
             {
                 bool flag = true;
                 if (info.IsAutoMounted)
                 {
                     flag = this.ValidateOrRemoveAutoMountedDrive(info, scope3);
                 }
                 if (flag && !dictionary.ContainsKey(info.Name))
                 {
                     dictionary[info.Name] = info;
                 }
             }
         }
         if ((scope != null) && (scope.Length > 0))
         {
             break;
         }
     }
     try
     {
         foreach (DriveInfo info2 in DriveInfo.GetDrives())
         {
             if ((info2 != null) && (info2.DriveType != DriveType.Fixed))
             {
                 string key = OSHelper.IsUnix ? info2.Name : info2.Name.Substring(0, 1);
                 if (!dictionary.ContainsKey(key))
                 {
                     PSDriveInfo info3 = this.AutomountFileSystemDrive(info2);
                     if (info3 != null)
                     {
                         dictionary[info3.Name] = info3;
                     }
                 }
             }
         }
     }
     catch (IOException)
     {
     }
     catch (UnauthorizedAccessException)
     {
     }
     Collection<PSDriveInfo> collection = new Collection<PSDriveInfo>();
     foreach (PSDriveInfo info4 in dictionary.Values)
     {
         collection.Add(info4);
     }
     return collection;
 }
Ejemplo n.º 18
0
        private PSDriveInfo GetDrive(string name, bool automount)
        {
            if (name == null)
            {
                throw PSTraceSource.NewArgumentNullException("name");
            }

            PSDriveInfo result = null;

            // Start searching through the scopes for the drive until the drive
            // is found or the global scope is reached.

            SessionStateScopeEnumerator scopeEnumerator = new SessionStateScopeEnumerator(CurrentScope);

            int scopeID = 0;

            foreach (SessionStateScope processingScope in scopeEnumerator)
            {
                result = processingScope.GetDrive(name);

                if (result != null)
                {
                    if (result.IsAutoMounted)
                    {
                        // Validate or remove the auto-mounted drive

                        if (!ValidateOrRemoveAutoMountedDrive(result, processingScope))
                        {
                            result = null;
                        }
                    }

                    if (result != null)
                    {
                        s_tracer.WriteLine("Drive found in scope {0}", scopeID);
                        break;
                    }
                }

                // Increment the scope ID
                ++scopeID;
            } // foreach scope

            if (result == null && automount)
            {
                result = AutomountBuiltInDrive(name);
            }

            if (result == null && this == ExecutionContext.TopLevelSessionState)
            {
                result = AutomountFileSystemDrive(name);
            }

            if (result == null)
            {
                DriveNotFoundException driveNotFound =
                    new DriveNotFoundException(
                        name,
                        "DriveNotFound",
                        SessionStateStrings.DriveNotFound);

                throw driveNotFound;
            }

            return result;
        } // GetDrive
Ejemplo n.º 19
0
        } // RemoveCmdlet

        /// <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;
                    }
                }
            }
        } // RemoveCmdlet
Ejemplo n.º 20
0
 internal void RemoveAlias(string aliasName, bool force)
 {
     if (string.IsNullOrEmpty(aliasName))
     {
         throw PSTraceSource.NewArgumentException("aliasName");
     }
     SessionStateScopeEnumerator enumerator = new SessionStateScopeEnumerator(this.currentScope);
     foreach (SessionStateScope scope in (IEnumerable<SessionStateScope>) enumerator)
     {
         AliasInfo alias = scope.GetAlias(aliasName);
         if (alias != null)
         {
             if (((alias.Options & ScopedItemOptions.Private) != ScopedItemOptions.None) && (scope != this.currentScope))
             {
                 alias = null;
             }
             else
             {
                 scope.RemoveAlias(aliasName, force);
                 break;
             }
         }
     }
 }
Ejemplo n.º 21
0
        } // CanRemoveDrive

        #endregion RemoveDrive

        #region Drives

        /// <summary>
        /// Gets an enumerable list of the drives that are mounted in
        /// the specified scope.
        /// </summary>
        ///
        /// <param name="scope">
        /// The scope to retrieve the drives from. If null or empty,
        /// all drives from all scopes will be retrieved.
        /// </param>
        ///
        /// <exception cref="ArgumentException">
        /// If <paramref name="scope"/> is less than zero, or not
        /// a number and not "script", "global", "local", or "private"
        /// </exception>
        /// 
        /// <exception cref="ArgumentOutOfRangeException">
        /// If <paramref name="scopeID"/> is less than zero or greater than the number of currently
        /// active scopes.
        /// </exception>
        /// 
        internal Collection<PSDriveInfo> Drives(string scope)
        {
            Dictionary<String, PSDriveInfo> driveTable = new Dictionary<String, PSDriveInfo>();

            SessionStateScope startingScope = _currentScope;

            if (!String.IsNullOrEmpty(scope))
            {
                startingScope = GetScopeByID(scope);
            }

            SessionStateScopeEnumerator scopeEnumerator =
                new SessionStateScopeEnumerator(startingScope);
            DriveInfo[] alldrives = DriveInfo.GetDrives();
            Collection<String> driveNames = new Collection<String>();
            foreach (DriveInfo drive in alldrives)
            {
                driveNames.Add(drive.Name.Substring(0, 1));
            }


            foreach (SessionStateScope lookupScope in scopeEnumerator)
            {
                foreach (PSDriveInfo drive in lookupScope.Drives)
                {
                    // It is the correct behavior for child scope
                    // drives to overwrite parent scope drives of
                    // the same name.

                    if (drive != null)
                    {
                        bool driveIsValid = true;

                        // If the drive is auto-mounted, ensure that it still exists, or remove the drive.
#if !UNIX
                        if (drive.IsAutoMounted || IsAStaleVhdMountedDrive(drive))
                        {
                            driveIsValid = ValidateOrRemoveAutoMountedDrive(drive, lookupScope);
                        }
#endif
                        if (drive.Name.Length == 1)
                        {
                            if (!(driveNames.Contains(drive.Name)))
                                driveTable.Remove(drive.Name);
                        }



                        if (driveIsValid && !driveTable.ContainsKey(drive.Name))
                        {
                            driveTable[drive.Name] = drive;
                        }
                    }
                }

                // If the scope was specified then don't loop
                // through the other scopes

                if (scope != null && scope.Length > 0)
                {
                    break;
                }
            } // foreach scope

            // Now lookup all the file system drives and automount any that are not
            // present

            try
            {
                foreach (System.IO.DriveInfo fsDriveInfo in alldrives)
                {
                    if (fsDriveInfo != null)
                    {
                        string fsDriveName = fsDriveInfo.Name.Substring(0, 1);
                        if (!driveTable.ContainsKey(fsDriveName))
                        {
                            PSDriveInfo automountedDrive = AutomountFileSystemDrive(fsDriveInfo);
                            if (automountedDrive != null)
                            {
                                driveTable[automountedDrive.Name] = automountedDrive;
                            }
                        }
                    }
                }
            }
            // We don't want to have automounting cause an exception. We
            // rather it just fail silently as it wasn't a result of an
            // explicit request by the user anyway.
            catch (IOException)
            {
            }
            catch (UnauthorizedAccessException)
            {
            }

            Collection<PSDriveInfo> results = new Collection<PSDriveInfo>();
            foreach (PSDriveInfo drive in driveTable.Values)
            {
                results.Add(drive);
            }
            return results;
        } // Drives
Ejemplo n.º 22
0
 internal void RemoveCmdletEntry(string name, bool force)
 {
     if (string.IsNullOrEmpty(name))
     {
         throw PSTraceSource.NewArgumentException("name");
     }
     SessionStateScopeEnumerator enumerator = new SessionStateScopeEnumerator(this.currentScope);
     foreach (SessionStateScope scope in (IEnumerable<SessionStateScope>) enumerator)
     {
         CmdletInfo cmdlet = scope.GetCmdlet(name);
         if (cmdlet != null)
         {
             if (((cmdlet.Options & ScopedItemOptions.Private) != ScopedItemOptions.None) && (scope != this.currentScope))
             {
                 cmdlet = null;
             }
             else
             {
                 scope.RemoveCmdletEntry(name, force);
                 break;
             }
         }
     }
 }
Ejemplo n.º 23
0
        } // GetVariableValueAtScope

        internal object GetAutomaticVariableValue(AutomaticVariable variable)
        {
            var scopeEnumerator = new SessionStateScopeEnumerator(CurrentScope);
            object result = AutomationNull.Value;
            foreach (var scope in scopeEnumerator)
            {
                result = scope.GetAutomaticVariableValue(variable);
                if (result != AutomationNull.Value)
                {
                    break;
                }
            }

            return result;
        }
Ejemplo n.º 24
0
        } // RemoveDrive

        /// <summary>
        /// Removes the specified drive.
        /// </summary>
        /// 
        /// <param name="drive">
        /// The drive to be removed.
        /// </param>
        /// 
        /// <param name="force">
        /// Determines whether drive should be forcefully removed even if there was errors.
        /// </param>
        ///
        /// <param name="scopeID">
        /// The ID of the scope from which to remove the drive.
        /// If the scope ID is null or empty, the scope hierarchy will be searched
        /// starting at the current scope through all the parent scopes to the
        /// global scope until a drive of the given name is found to remove.
        /// </param>
        /// 
        /// <param name="context">
        /// The context which the core command is running.
        /// </param>
        /// 
        /// <exception cref="ArgumentOutOfRangeException">
        /// If <paramref name="scopeID"/> is less than zero or greater than the number of currently
        /// active scopes.
        /// </exception>
        /// 
        internal void RemoveDrive(
            PSDriveInfo drive,
            bool force,
            string scopeID,
            CmdletProviderContext context)
        {
            // Make sure that the CanRemoveDrive is called even if we are forcing
            // the removal because we want the provider to have a chance to
            // cleanup.

            bool canRemove = false;

            try
            {
                canRemove = CanRemoveDrive(drive, context);
            }
            catch (LoopFlowException)
            {
                throw;
            }
            catch (PipelineStoppedException)
            {
                throw;
            }
            catch (ActionPreferenceStopException)
            {
                throw;
            }
            catch (ProviderInvocationException)
            {
                if (!force)
                {
                    throw;
                }
            }

            // Now remove the drive if there was no error or we are forcing the removal

            if (canRemove || force)
            {
                // The scope ID wasn't defined or wasn't recognizable
                // so do a search through the scopes looking for the
                // drive.

                if (String.IsNullOrEmpty(scopeID))
                {
                    SessionStateScopeEnumerator scopeEnumerator =
                        new SessionStateScopeEnumerator(CurrentScope);

                    foreach (SessionStateScope scope in scopeEnumerator)
                    {
                        try
                        {
                            PSDriveInfo result = scope.GetDrive(drive.Name);
                            if (result != null)
                            {
                                scope.RemoveDrive(drive);

                                // If the drive is the current drive for the provider, remove
                                // it from the current drive list.

                                if (ProvidersCurrentWorkingDrive[drive.Provider] == result)
                                {
                                    ProvidersCurrentWorkingDrive[drive.Provider] = null;
                                }
                                break;
                            }
                        }
                        catch (ArgumentException)
                        {
                        }
                    }
                }
                else
                {
                    SessionStateScope scope = GetScopeByID(scopeID);
                    scope.RemoveDrive(drive);


                    // If the drive is the current drive for the provider, remove
                    // it from the current drive list.

                    if (ProvidersCurrentWorkingDrive[drive.Provider] == drive)
                    {
                        ProvidersCurrentWorkingDrive[drive.Provider] = null;
                    }
                }
            }
            else
            {
                PSInvalidOperationException e =
                    (PSInvalidOperationException)
                    PSTraceSource.NewInvalidOperationException(
                        SessionStateStrings.DriveRemovalPreventedByProvider,
                        drive.Name,
                        drive.Provider);

                context.WriteError(
                    new ErrorRecord(
                        e.ErrorRecord,
                        e));
            }
        } // RemoveDrive
Ejemplo n.º 25
0
 internal IDictionary<string, List<CmdletInfo>> GetCmdletTable()
 {
     Dictionary<string, List<CmdletInfo>> dictionary = new Dictionary<string, List<CmdletInfo>>(StringComparer.OrdinalIgnoreCase);
     SessionStateScopeEnumerator enumerator = new SessionStateScopeEnumerator(this.currentScope);
     foreach (SessionStateScope scope in (IEnumerable<SessionStateScope>) enumerator)
     {
         foreach (KeyValuePair<string, List<CmdletInfo>> pair in scope.CmdletTable)
         {
             if (!dictionary.ContainsKey(pair.Key))
             {
                 List<CmdletInfo> list = new List<CmdletInfo>();
                 foreach (CmdletInfo info in pair.Value)
                 {
                     if (((info.Options & ScopedItemOptions.Private) == ScopedItemOptions.None) || (scope == this.currentScope))
                     {
                         list.Add(info);
                     }
                 }
                 dictionary.Add(pair.Key, list);
             }
         }
     }
     return dictionary;
 }
Ejemplo n.º 26
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;
        }
Ejemplo n.º 27
0
 private PSDriveInfo GetDrive(string name, bool automount)
 {
     if (name == null)
     {
         throw PSTraceSource.NewArgumentNullException("name");
     }
     PSDriveInfo drive = null;
     SessionStateScopeEnumerator enumerator = new SessionStateScopeEnumerator(this.CurrentScope);
     int num = 0;
     foreach (SessionStateScope scope in (IEnumerable<SessionStateScope>) enumerator)
     {
         drive = scope.GetDrive(name);
         if (drive != null)
         {
             if (drive.IsAutoMounted)
             {
                 if (drive.IsAutoMountedManuallyRemoved)
                 {
                     System.Management.Automation.DriveNotFoundException exception = new System.Management.Automation.DriveNotFoundException(name, "DriveNotFound", SessionStateStrings.DriveNotFound);
                     throw exception;
                 }
                 if (!this.ValidateOrRemoveAutoMountedDrive(drive, scope))
                 {
                     drive = null;
                 }
             }
             if (drive != null)
             {
                 tracer.WriteLine("Drive found in scope {0}", new object[] { num });
                 break;
             }
         }
         num++;
     }
     if ((drive == null) && automount)
     {
         drive = this.AutomountBuiltInDrive(name);
     }
     if ((drive == null) && (this == this._context.TopLevelSessionState))
     {
         drive = this.AutomountFileSystemDrive(name);
     }
     if (drive == null)
     {
         System.Management.Automation.DriveNotFoundException exception2 = new System.Management.Automation.DriveNotFoundException(name, "DriveNotFound", SessionStateStrings.DriveNotFound);
         throw exception2;
     }
     return drive;
 }
Ejemplo n.º 28
0
 internal object GetAutomaticVariableValue(AutomaticVariable variable)
 {
     SessionStateScopeEnumerator enumerator = new SessionStateScopeEnumerator(this.CurrentScope);
     object automaticVariableValue = AutomationNull.Value;
     foreach (SessionStateScope scope in (IEnumerable<SessionStateScope>) enumerator)
     {
         automaticVariableValue = scope.GetAutomaticVariableValue(variable);
         if (automaticVariableValue != AutomationNull.Value)
         {
             return automaticVariableValue;
         }
     }
     return automaticVariableValue;
 }
Ejemplo n.º 29
0
        } // GetDrive

        /// <summary>
        /// Searches through the session state scopes looking
        /// for a drive of the specified name.
        /// </summary>
        /// 
        /// <param name="name">
        /// The name of the drive to return.
        /// </param>
        /// 
        /// <param name="scopeID">
        /// The scope ID of the scope to look in for the drive.
        /// If this parameter is null or empty the drive will be
        /// found by searching the scopes using the dynamic scoping
        /// rules.
        /// </param>
        /// 
        /// <returns>
        /// The drive for the given name in the given scope or null if
        /// the drive was not found.
        /// </returns>
        /// 
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="name"/> is null.
        /// </exception>
        /// 
        /// <exception cref="ArgumentException">
        /// If <paramref name="scopeID"/> is less than zero, or not
        /// a number and not "script", "global", "local", or "private"
        /// </exception>
        /// 
        /// <exception cref="ArgumentOutOfRangeException">
        /// If <paramref name="scopeID"/> is less than zero or greater than the number of currently
        /// active scopes.
        /// </exception>
        /// 
        internal PSDriveInfo GetDrive(string name, string scopeID)
        {
            if (name == null)
            {
                throw PSTraceSource.NewArgumentNullException("name");
            }

            PSDriveInfo result = null;

            // The scope ID wasn't defined or wasn't recognizable
            // so do a search through the scopes looking for the
            // drive.

            if (String.IsNullOrEmpty(scopeID))
            {
                SessionStateScopeEnumerator scopeEnumerator =
                    new SessionStateScopeEnumerator(CurrentScope);

                foreach (SessionStateScope scope in scopeEnumerator)
                {
                    result = scope.GetDrive(name);

                    if (result != null)
                    {
                        if (result.IsAutoMounted)
                        {
                            // Validate or remove the auto-mounted drive

                            if (!ValidateOrRemoveAutoMountedDrive(result, scope))
                            {
                                result = null;
                            }
                        }

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

                if (result == null)
                {
                    result = AutomountFileSystemDrive(name);
                }
            }
            else
            {
                SessionStateScope scope = GetScopeByID(scopeID);
                result = scope.GetDrive(name);

                if (result != null)
                {
                    if (result.IsAutoMounted)
                    {
                        // Validate or remove the auto-mounted drive

                        if (!ValidateOrRemoveAutoMountedDrive(result, scope))
                        {
                            result = null;
                        }
                    }
                }
                else
                {
                    if (scope == GlobalScope)
                    {
                        result = AutomountFileSystemDrive(name);
                    }
                }
            }

            return result;
        } // GetDrive
Ejemplo n.º 30
0
 internal CmdletInfo GetCmdlet(string cmdletName, CommandOrigin origin)
 {
     CmdletInfo valueToCheck = null;
     if (!string.IsNullOrEmpty(cmdletName))
     {
         SessionStateScopeEnumerator enumerator = new SessionStateScopeEnumerator(this.currentScope);
         foreach (SessionStateScope scope in (IEnumerable<SessionStateScope>) enumerator)
         {
             valueToCheck = scope.GetCmdlet(cmdletName);
             if (valueToCheck != null)
             {
                 SessionState.ThrowIfNotVisible(origin, valueToCheck);
                 if (((valueToCheck.Options & ScopedItemOptions.Private) == ScopedItemOptions.None) || (scope == this.currentScope))
                 {
                     return valueToCheck;
                 }
                 valueToCheck = null;
             }
         }
     }
     return valueToCheck;
 }
Ejemplo n.º 31
0
        } // RemoveVariableAtScope 

        /// <summary>
        /// Gets a flattened view of the variables that are visible using
        /// the current scope as a reference and filtering the variables in
        /// the other scopes based on the scoping rules.
        /// </summary>
        /// 
        /// <returns>
        /// An IDictionary representing the visible variables.
        /// </returns>
        /// 
        internal IDictionary<string, PSVariable> GetVariableTable()
        {
            SessionStateScopeEnumerator scopeEnumerator =
                new SessionStateScopeEnumerator(_currentScope);

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

            foreach (SessionStateScope scope in scopeEnumerator)
            {
                GetScopeVariableTable(scope, result, includePrivate: scope == _currentScope);
            }

            return result;
        }