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