MegaBites-AI/Windows-powershell
0308
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Collections.Generic;5using System.Collections.ObjectModel;6using System.Management.Automation.Provider;7 8using Dbg = System.Management.Automation;9 10namespace System.Management.Automation11{12 /// <summary>13 /// Exposes the APIs to manage the Cmdlet Providers the Cmdlet base class. The methods of this class14 /// get and set provider data in session state.15 /// </summary>16 public sealed class CmdletProviderManagementIntrinsics17 {18 #region Constructors19 20 /// <summary>21 /// Hide the default constructor since we always require an instance of SessionState.22 /// </summary>23 private CmdletProviderManagementIntrinsics()24 {25 Dbg.Diagnostics.Assert(26 false,27 "This constructor should never be called. Only the constructor that takes an instance of SessionState should be called.");28 }29 30 /// <summary>31 /// The facade for managing providers.32 /// </summary>33 /// <param name="sessionState">34 /// The session to which this is a facade.35 /// </param>36 /// <exception cref="ArgumentNullException">37 /// If <paramref name="sessionState"/> is null.38 /// </exception>39 internal CmdletProviderManagementIntrinsics(SessionStateInternal sessionState)40 {41 if (sessionState == null)42 {43 throw PSTraceSource.NewArgumentNullException(nameof(sessionState));44 }45 46 _sessionState = sessionState;47 }48 49 #endregion Constructors50 51 #region Public methods52 53 /// <summary>54 /// Gets the specified provider(s).55 /// </summary>56 /// <param name="name">57 /// Either the fully-qualified or friendly name for the provider.58 /// </param>59 /// <returns>60 /// The provider information for the specified provider.61 /// </returns>62 /// <exception cref="ArgumentException">63 /// If <paramref name="name"/> is null or empty.64 /// </exception>65 /// <exception cref="ProviderNotFoundException">66 /// If the provider specified by <paramref name="name"/> is not currently67 /// loaded.68 /// </exception>69 public Collection<ProviderInfo> Get(string name)70 {71 Dbg.Diagnostics.Assert(72 _sessionState != null,73 "The only constructor for this class should always set the sessionState field");74 75 // Parameter validation is done in the session state object76 77 return _sessionState.GetProvider(name);78 }79 80 /// <summary>81 /// Gets the specified provider(s).82 /// </summary>83 /// <param name="name">84 /// Either the fully-qualified or friendly name for the provider.85 /// </param>86 /// <returns>87 /// The provider information for the specified provider.88 /// </returns>89 /// <exception cref="ArgumentException">90 /// If <paramref name="name"/> is null or empty.91 /// </exception>92 /// <exception cref="ProviderNameAmbiguousException">93 /// If <paramref name="name"/> is not PSSnapin-qualified and more than one provider94 /// exists with the specified name.95 /// </exception>96 /// <exception cref="ProviderNotFoundException">97 /// If the provider specified by <paramref name="name"/> is not currently98 /// loaded.99 /// </exception>100 public ProviderInfo GetOne(string name)101 {102 Dbg.Diagnostics.Assert(103 _sessionState != null,104 "The only constructor for this class should always set the sessionState field");105 106 // Parameter validation is done in the session state object107 108 return _sessionState.GetSingleProvider(name);109 }110 111 /// <summary>112 /// Gets all the Cmdlet Providers that are loaded.113 /// </summary>114 public IEnumerable<ProviderInfo> GetAll()115 {116 Dbg.Diagnostics.Assert(117 _sessionState != null,118 "The only constructor for this class should always set the sessionState field");119 120 return _sessionState.ProviderList;121 }122 123 #endregion Public methods124 125 #region Internal methods126 127 /// <summary>128 /// Determines if the specified provider has the specified capability.129 /// </summary>130 /// <param name="capability">131 /// The capability to check the provider for.132 /// </param>133 /// <param name="provider">134 /// The provider information to use for the check.135 /// </param>136 /// <returns>137 /// True, if the provider has the capability, false otherwise.138 /// </returns>139 internal static bool CheckProviderCapabilities(140 ProviderCapabilities capability,141 ProviderInfo provider)142 {143 // Check the capability144 145 return (provider.Capabilities & capability) != 0;146 }147 148 /// <summary>149 /// Gets the count of the number of providers that are loaded.150 /// </summary>151 internal int Count152 {153 get154 {155 return _sessionState.ProviderCount;156 }157 }158 159 #endregion Internal methods160 161 #region private data162 163 private readonly SessionStateInternal _sessionState;164 165 #endregion private data166 }167}168 