/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using Dbg = System.Management.Automation;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Provider;
namespace Microsoft.PowerShell.Commands
{
///
/// This provider is the data accessor for environment variables. It uses
/// the SessionStateProviderBase as the base class to produce a view on
/// session state data.
///
[CmdletProvider(EnvironmentProvider.ProviderName, ProviderCapabilities.ShouldProcess)]
public sealed class EnvironmentProvider : SessionStateProviderBase
{
///
/// Gets the name of the provider
///
public const string ProviderName = "Environment";
#region Constructor
///
/// The constructor for the provider that exposes environment variables to the user
/// as drives.
///
public EnvironmentProvider()
{
} // constructor
#endregion Constructor
#region DriveCmdletProvider overrides
///
/// Initializes the alias drive
///
///
///
/// An array of a single PSDriveInfo object representing the alias drive.
///
///
protected override Collection InitializeDefaultDrives()
{
string description = SessionStateStrings.EnvironmentDriveDescription;
PSDriveInfo envDrive =
new PSDriveInfo(
DriveNames.EnvironmentDrive,
ProviderInfo,
String.Empty,
description,
null);
Collection drives = new Collection();
drives.Add(envDrive);
return drives;
} // InitializeDefaultDrives
#endregion DriveCmdletProvider overrides
#region protected members
///
/// Gets a environment variable from session state
///
///
///
/// The name of the environment variable to retrieve.
///
///
///
/// A DictionaryEntry that represents the value of the environment variable.
///
///
internal override object GetSessionStateItem(string name)
{
Dbg.Diagnostics.Assert(
!String.IsNullOrEmpty(name),
"The caller should verify this parameter");
object result = null;
string value = Environment.GetEnvironmentVariable(name);
if (value != null)
{
result = new DictionaryEntry(name, value);
}
return result;
} // GetSessionStateItem
///
/// Sets the environment variable of the specified name to the specified value
///
///
///
/// The name of the environment variable to set.
///
///
///
/// The new value for the environment variable.
///
///
///
/// If true, the item that was set should be written to WriteItemObject.
///
///
internal override void SetSessionStateItem(string name, object value, bool writeItem)
{
Dbg.Diagnostics.Assert(
!String.IsNullOrEmpty(name),
"The caller should verify this parameter");
if (value == null)
{
Environment.SetEnvironmentVariable(name, null);
}
else
{
// First see if we got a DictionaryEntry which represents
// an item for this provider. If so, use the value from
// the dictionary entry.
if (value is DictionaryEntry)
{
value = ((DictionaryEntry)value).Value;
}
string stringValue = value as string;
if (stringValue == null)
{
// try using ETS to convert to a string.
PSObject wrappedObject = PSObject.AsPSObject(value);
stringValue = wrappedObject.ToString();
}
Environment.SetEnvironmentVariable(name, stringValue);
DictionaryEntry item = new DictionaryEntry(name, stringValue);
if (writeItem)
{
WriteItemObject(item, name, false);
}
}
} // SetSessionStateItem
///
/// Removes the specified environment variable from session state.
///
///
///
/// The name of the environment variable to remove from session state.
///
///
internal override void RemoveSessionStateItem(string name)
{
Dbg.Diagnostics.Assert(
!String.IsNullOrEmpty(name),
"The caller should verify this parameter");
Environment.SetEnvironmentVariable(name, null);
} // RemoveSessionStateItem
///
/// Gets a flattened view of the environment variables in session state
///
///
///
/// An IDictionary representing the flattened view of the environment variables in
/// session state.
///
///
internal override IDictionary GetSessionStateTable()
{
// Environment variables are case-sensitive on Unix and
// case-insensitive on Windows
#if UNIX
Dictionary providerTable =
new Dictionary(StringComparer.Ordinal);
#else
Dictionary providerTable =
new Dictionary(StringComparer.OrdinalIgnoreCase);
#endif
// The environment variables returns a dictionary of keys and values that are
// both strings. We want to return a dictionary with the key as a string and
// the value as the DictionaryEntry containing both the name and env variable
// value.
IDictionary environmentTable = Environment.GetEnvironmentVariables();
foreach (DictionaryEntry entry in environmentTable)
{
providerTable.Add((string)entry.Key, entry);
}
return providerTable;
} // GetSessionStateTable
///
/// Gets the Value property of the DictionaryEntry item
///
///
///
/// The item to get the value from.
///
///
///
/// The value of the item.
///
///
internal override object GetValueOfItem(object item)
{
Dbg.Diagnostics.Assert(
item != null,
"Caller should verify the item parameter");
object value = item;
if (item is DictionaryEntry)
{
value = ((DictionaryEntry)item).Value;
}
return value;
} // GetValueOfItem
#endregion protected members
} // EnvironmentProvider
}