MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.Collections;6using System.Collections.ObjectModel;7using System.Management.Automation;8using System.Management.Automation.Provider;9 10using Dbg = System.Management.Automation;11 12namespace Microsoft.PowerShell.Commands13{14 /// <summary>15 /// This provider is the data accessor for shell variables. It uses16 /// the HashtableProvider as the base class to get a hashtable as17 /// a data store.18 /// </summary>19 [CmdletProvider(VariableProvider.ProviderName, ProviderCapabilities.ShouldProcess)]20 [OutputType(typeof(PSVariable), ProviderCmdlet = ProviderCmdlet.SetItem)]21 [OutputType(typeof(PSVariable), ProviderCmdlet = ProviderCmdlet.RenameItem)]22 [OutputType(typeof(PSVariable), ProviderCmdlet = ProviderCmdlet.CopyItem)]23 [OutputType(typeof(PSVariable), ProviderCmdlet = ProviderCmdlet.GetItem)]24 [OutputType(typeof(PSVariable), ProviderCmdlet = ProviderCmdlet.NewItem)]25 public sealed class VariableProvider : SessionStateProviderBase26 {27 /// <summary>28 /// Gets the name of the provider.29 /// </summary>30 public const string ProviderName = "Variable";31 32 #region Constructor33 34 /// <summary>35 /// The constructor for the provider that exposes variables to the user36 /// as drives.37 /// </summary>38 public VariableProvider()39 {40 }41 42 #endregion Constructor43 44 #region DriveCmdletProvider overrides45 46 /// <summary>47 /// Initializes the variables drive.48 /// </summary>49 /// <returns>50 /// An array of a single PSDriveInfo object representing the variables drive.51 /// </returns>52 protected override Collection<PSDriveInfo> InitializeDefaultDrives()53 {54 string description = SessionStateStrings.VariableDriveDescription;55 56 PSDriveInfo variableDrive =57 new PSDriveInfo(58 DriveNames.VariableDrive,59 ProviderInfo,60 string.Empty,61 description,62 null);63 64 Collection<PSDriveInfo> drives = new Collection<PSDriveInfo>();65 drives.Add(variableDrive);66 return drives;67 }68 69 #endregion DriveCmdletProvider overrides70 71 #region protected members72 73 /// <summary>74 /// Gets a variable from session state.75 /// </summary>76 /// <param name="name">77 /// The name of the variable to retrieve.78 /// </param>79 /// <returns>80 /// A PSVariable that represents the variable.81 /// </returns>82 internal override object GetSessionStateItem(string name)83 {84 Dbg.Diagnostics.Assert(85 !string.IsNullOrEmpty(name),86 "The caller should verify this parameter");87 88 return (PSVariable)SessionState.Internal.GetVariable(name, Context.Origin);89 }90 91 /// <summary>92 /// Sets the variable of the specified name to the specified value.93 /// </summary>94 /// <param name="name">95 /// The name of the variable to set.96 /// </param>97 /// <param name="value">98 /// The new value for the variable.99 /// </param>100 /// <param name="writeItem">101 /// If true, the item that was set should be written to WriteItemObject.102 /// </param>103 internal override void SetSessionStateItem(string name, object value, bool writeItem)104 {105 Dbg.Diagnostics.Assert(106 !string.IsNullOrEmpty(name),107 "The caller should verify this parameter");108 109 PSVariable variable = null;110 111 if (value != null)112 {113 variable = value as PSVariable;114 if (variable == null)115 {116 variable = new PSVariable(name, value);117 }118 else119 {120 // ensure the name matches121 122 if (!string.Equals(name, variable.Name, StringComparison.OrdinalIgnoreCase))123 {124 PSVariable newVar = new PSVariable(name, variable.Value, variable.Options, variable.Attributes);125 newVar.Description = variable.Description;126 variable = newVar;127 }128 }129 }130 else131 {132 variable = new PSVariable(name, null);133 }134 135 PSVariable item = SessionState.Internal.SetVariable(variable, Force, Context.Origin) as PSVariable;136 137 if (writeItem && item != null)138 {139 WriteItemObject(item, item.Name, false);140 }141 }142 143 /// <summary>144 /// Removes the specified variable from session state.145 /// </summary>146 /// <param name="name">147 /// The name of the variable to remove from session state.148 /// </param>149 internal override void RemoveSessionStateItem(string name)150 {151 Dbg.Diagnostics.Assert(152 !string.IsNullOrEmpty(name),153 "The caller should verify this parameter");154 155 SessionState.Internal.RemoveVariable(name, Force);156 }157 158 /// <summary>159 /// Gets a flattened view of the variables in session state.160 /// </summary>161 /// <returns>162 /// An IDictionary representing the flattened view of the variables in163 /// session state.164 /// </returns>165 internal override IDictionary GetSessionStateTable()166 {167 return (IDictionary)SessionState.Internal.GetVariableTable();168 }169 170 /// <summary>171 /// Gets the value of the item that is returned from GetItem by172 /// extracting the PSVariable value.173 /// </summary>174 /// <param name="item">175 /// The item to extract the value from.176 /// </param>177 /// <returns>178 /// The value of the specified item.179 /// </returns>180 internal override object GetValueOfItem(object item)181 {182 Dbg.Diagnostics.Assert(183 item != null,184 "Caller should verify the item parameter");185 186 // Call the base class to unwrap the DictionaryEntry187 // if necessary188 189 object value = base.GetValueOfItem(item);190 191 PSVariable var = item as PSVariable;192 if (var != null)193 {194 value = var.Value;195 }196 197 return value;198 }199 200 /// <summary>201 /// Determines if the item can be renamed. Derived classes that need202 /// to perform a check should override this method.203 /// </summary>204 /// <param name="item">205 /// The item to verify if it can be renamed.206 /// </param>207 /// <returns>208 /// true if the item can be renamed or false otherwise.209 /// </returns>210 internal override bool CanRenameItem(object item)211 {212 bool result = false;213 214 PSVariable variable = item as PSVariable;215 if (variable != null)216 {217 if ((variable.Options & ScopedItemOptions.Constant) != 0 ||218 ((variable.Options & ScopedItemOptions.ReadOnly) != 0 && !Force))219 {220 SessionStateUnauthorizedAccessException e =221 new SessionStateUnauthorizedAccessException(222 variable.Name,223 SessionStateCategory.Variable,224 "CannotRenameVariable",225 SessionStateStrings.CannotRenameVariable);226 227 throw e;228 }229 230 result = true;231 }232 233 return result;234 }235 #endregion protected members236 237 }238}239 