Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
AliasProvider.cs356 linesDownload Raw Back to namespaces
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Collections;5using System.Collections.ObjectModel;6using System.Management.Automation;7using System.Management.Automation.Provider;8 9using Dbg = System.Management.Automation;10 11namespace Microsoft.PowerShell.Commands12{13    /// <summary>14    /// This provider is the data accessor for shell aliases. It uses15    /// the SessionStateProviderBase as the base class to produce a view on16    /// session state data.17    /// </summary>18    [CmdletProvider(AliasProvider.ProviderName, ProviderCapabilities.ShouldProcess)]19    [OutputType(typeof(AliasInfo), ProviderCmdlet = ProviderCmdlet.SetItem)]20    [OutputType(typeof(AliasInfo), ProviderCmdlet = ProviderCmdlet.RenameItem)]21    [OutputType(typeof(AliasInfo), ProviderCmdlet = ProviderCmdlet.CopyItem)]22    [OutputType(typeof(AliasInfo), ProviderCmdlet = ProviderCmdlet.GetChildItem)]23    [OutputType(typeof(AliasInfo), ProviderCmdlet = ProviderCmdlet.NewItem)]24    public sealed class AliasProvider : SessionStateProviderBase25    {26        /// <summary>27        /// Gets the name of the provider.28        /// </summary>29        public const string ProviderName = "Alias";30 31        #region Constructor32 33        /// <summary>34        /// The constructor for the provider that exposes variables to the user35        /// as drives.36        /// </summary>37        public AliasProvider()38        {39        }40 41        #endregion Constructor42 43        #region DriveCmdletProvider overrides44 45        /// <summary>46        /// Initializes the alias drive.47        /// </summary>48        /// <returns>49        /// An array of a single PSDriveInfo object representing the alias drive.50        /// </returns>51        protected override Collection<PSDriveInfo> InitializeDefaultDrives()52        {53            string description = SessionStateStrings.AliasDriveDescription;54 55            PSDriveInfo aliasDrive =56                new PSDriveInfo(57                    DriveNames.AliasDrive,58                    ProviderInfo,59                    string.Empty,60                    description,61                    null);62 63            Collection<PSDriveInfo> drives = new Collection<PSDriveInfo>();64            drives.Add(aliasDrive);65            return drives;66        }67 68        #endregion DriveCmdletProvider overrides69 70        #region Dynamic Parameters71 72        /// <summary>73        /// Gets the dynamic parameters for the NewItem cmdlet.74        /// </summary>75        /// <param name="path">76        /// Ignored.77        /// </param>78        /// <param name="type">79        /// Ignored.80        /// </param>81        /// <param name="newItemValue">82        /// Ignored.83        /// </param>84        /// <returns>85        /// An instance of AliasProviderDynamicParameters which is the dynamic parameters for86        /// NewItem.87        /// </returns>88        protected override object NewItemDynamicParameters(string path, string type, object newItemValue)89        {90            return new AliasProviderDynamicParameters();91        }92 93        /// <summary>94        /// Gets the dynamic parameters for the NewItem cmdlet.95        /// </summary>96        /// <param name="path">97        /// Ignored.98        /// </param>99        /// <param name="value">100        /// Ignored.101        /// </param>102        /// <returns>103        /// An instance of AliasProviderDynamicParameters which is the dynamic parameters for104        /// SetItem.105        /// </returns>106        protected override object SetItemDynamicParameters(string path, object value)107        {108            return new AliasProviderDynamicParameters();109        }110 111        #endregion Dynamic Parameters112 113        #region protected members114 115        /// <summary>116        /// Gets a alias from session state.117        /// </summary>118        /// <param name="name">119        /// The name of the alias to retrieve.120        /// </param>121        /// <returns>122        /// A DictionaryEntry that represents the value of the alias.123        /// </returns>124        internal override object GetSessionStateItem(string name)125        {126            Dbg.Diagnostics.Assert(127                !string.IsNullOrEmpty(name),128                "The caller should verify this parameter");129 130            AliasInfo value = SessionState.Internal.GetAlias(name, Context.Origin);131 132            return value;133        }134 135        /// <summary>136        /// Since items are often more than their value, this method should137        /// be overridden to provide the value for an item.138        /// </summary>139        /// <param name="item">140        /// The item to extract the value from.141        /// </param>142        /// <returns>143        /// The value of the specified item.144        /// </returns>145        /// <remarks>146        /// The default implementation will get147        /// the Value property of a DictionaryEntry148        /// </remarks>149        internal override object GetValueOfItem(object item)150        {151            Dbg.Diagnostics.Assert(152                item != null,153                "Caller should verify the item parameter");154 155            object value = item;156 157            AliasInfo aliasInfo = item as AliasInfo;158            if (aliasInfo != null)159            {160                value = aliasInfo.Definition;161            }162 163            return value;164        }165 166        /// <summary>167        /// Sets the alias of the specified name to the specified value.168        /// </summary>169        /// <param name="name">170        /// The name of the alias to set.171        /// </param>172        /// <param name="value">173        /// The new value for the alias.174        /// </param>175        /// <param name="writeItem">176        /// If true, the item that was set should be written to WriteItemObject.177        /// </param>178#pragma warning disable 0162179        internal override void SetSessionStateItem(string name, object value, bool writeItem)180        {181            Dbg.Diagnostics.Assert(182                !string.IsNullOrEmpty(name),183                "The caller should verify this parameter");184 185            AliasProviderDynamicParameters dynamicParameters =186                DynamicParameters as AliasProviderDynamicParameters;187 188            AliasInfo item = null;189 190            bool dynamicParametersSpecified = dynamicParameters != null && dynamicParameters.OptionsSet;191 192            if (value == null)193            {194                if (dynamicParametersSpecified)195                {196                    item = (AliasInfo)GetSessionStateItem(name);197                    item?.SetOptions(dynamicParameters.Options, Force);198                }199                else200                {201                    RemoveSessionStateItem(name);202                }203            }204            else205            {206                do // false loop207                {208                    string stringValue = value as string;209                    if (stringValue != null)210                    {211                        if (dynamicParametersSpecified)212                        {213                            item = SessionState.Internal.SetAliasValue(name, stringValue, dynamicParameters.Options, Force, Context.Origin);214                        }215                        else216                        {217                            item = SessionState.Internal.SetAliasValue(name, stringValue, Force, Context.Origin);218                        }219 220                        break;221                    }222 223                    AliasInfo alias = value as AliasInfo;224                    if (alias != null)225                    {226                        AliasInfo newAliasInfo =227                            new AliasInfo(228                                name,229                                alias.Definition,230                                this.Context.ExecutionContext,231                                alias.Options);232 233                        if (dynamicParametersSpecified)234                        {235                            newAliasInfo.SetOptions(dynamicParameters.Options, Force);236                        }237 238                        item = SessionState.Internal.SetAliasItem(newAliasInfo, Force, Context.Origin);239                        break;240                    }241 242                    throw PSTraceSource.NewArgumentException(nameof(value));243                } while (false);244            }245 246            if (writeItem && item != null)247            {248                WriteItemObject(item, item.Name, false);249            }250        }251#pragma warning restore 0162252 253        /// <summary>254        /// Removes the specified alias from session state.255        /// </summary>256        /// <param name="name">257        /// The name of the alias to remove from session state.258        /// </param>259        internal override void RemoveSessionStateItem(string name)260        {261            Dbg.Diagnostics.Assert(262                !string.IsNullOrEmpty(name),263                "The caller should verify this parameter");264 265            SessionState.Internal.RemoveAlias(name, Force);266        }267 268        /// <summary>269        /// Gets a flattened view of the alias in session state.270        /// </summary>271        /// <returns>272        /// An IDictionary representing the flattened view of the aliases in273        /// session state.274        /// </returns>275        internal override IDictionary GetSessionStateTable()276        {277            return (IDictionary)SessionState.Internal.GetAliasTable();278        }279 280        /// <summary>281        /// Determines if the item can be renamed. Derived classes that need282        /// to perform a check should override this method.283        /// </summary>284        /// <param name="item">285        /// The item to verify if it can be renamed.286        /// </param>287        /// <returns>288        /// true if the item can be renamed or false otherwise.289        /// </returns>290        internal override bool CanRenameItem(object item)291        {292            bool result = false;293 294            AliasInfo aliasInfo = item as AliasInfo;295            if (aliasInfo != null)296            {297                if ((aliasInfo.Options & ScopedItemOptions.Constant) != 0 ||298                    ((aliasInfo.Options & ScopedItemOptions.ReadOnly) != 0 && !Force))299                {300                    SessionStateUnauthorizedAccessException e =301                        new SessionStateUnauthorizedAccessException(302                            aliasInfo.Name,303                            SessionStateCategory.Alias,304                            "CannotRenameAlias",305                            SessionStateStrings.CannotRenameAlias);306 307                    throw e;308                }309 310                result = true;311            }312 313            return result;314        }315 316        #endregion protected members317    }318 319    /// <summary>320    /// The dynamic parameter object for the AliasProvider SetItem and NewItem commands.321    /// </summary>322    public class AliasProviderDynamicParameters323    {324        /// <summary>325        /// Gets or sets the option parameter for the alias.326        /// </summary>327        [Parameter]328        public ScopedItemOptions Options329        {330            get331            {332                return _options;333            }334 335            set336            {337                _optionsSet = true;338                _options = value;339            }340        }341 342        private ScopedItemOptions _options;343 344        /// <summary>345        /// Determines if the Options parameter was set.346        /// </summary>347        /// <value></value>348        internal bool OptionsSet349        {350            get { return _optionsSet; }351        }352 353        private bool _optionsSet = false;354    }355}356