using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections.ObjectModel; using System.Management.Automation; using System.Management.Automation.Provider; using System.Management.Automation.Runspaces; namespace Intel.Management.PSModule { public class DriveProvider : NavigationCmdletProvider, IPropertyCmdletProvider, IContentCmdletProvider { public DriveProvider() : base() { } public PSDriveInfo GetDriveInfo() { return this.PSDriveInfo; } public object GetDynamicParameters() { return DynamicParameters; } /// /// Sets or Changes the value of an item /// parameter. /// /// Specifies the path to the item /// Comma separated string of values protected override void SetItem(string path, object values) { DriveInfo info = (DriveInfo)PSDriveInfo; DriveItem item = info.GetItemFromPath(path); item.SetItem(values, this); } protected override void ClearItem(string path) { DriveInfo info = (DriveInfo)PSDriveInfo; DriveItem item = info.GetItemFromPath(path); item.ClearItem(this); } /// /// Create a new Item /// parameter. /// /// Specifies the path to the item /// The Type of the new Item /// The value of the new Item protected override void NewItem(string path, string itemTypeName, object newItemValue) { DriveInfo info = (DriveInfo)PSDriveInfo; DriveItem item = null; string searchPath = path; item = info.GetItemFromPath(searchPath); while (item == null) { searchPath = GetParentPath(searchPath, info.Root); if (searchPath == null) break; item = info.GetItemFromPath(searchPath); } //get remaining path if (item != null) { string itemName = path.Substring(searchPath.Length); if (itemName.StartsWith("\\") || itemName.StartsWith("/")) itemName = itemName.Substring(1); item = item.NewItem(itemName, itemTypeName, newItemValue, this); } if (item != null) WriteItemObject(item.GetReturnObject(), path, false); } protected override object NewItemDynamicParameters(string path, string itemTypeName, object newItemValue) { DriveInfo info = (DriveInfo)PSDriveInfo; DriveItem item = null; string searchPath = path; item = info.GetItemFromPath(searchPath); while (item == null) { searchPath = GetParentPath(searchPath, info.Root); if (searchPath == null) break; item = info.GetItemFromPath(searchPath); } if (item != null) return item.NewItemDynamicParameters(itemTypeName, newItemValue); return base.NewItemDynamicParameters(path, itemTypeName, newItemValue); } /// /// Removes an Item /// protected override void RemoveItem(string path, bool recurse) { DriveInfo info = (DriveInfo)PSDriveInfo; DriveItem item = info.GetItemFromPath(path); item.RemoveItem( this); } protected override void InvokeDefaultAction(string path) { DriveInfo info = (DriveInfo)PSDriveInfo; DriveItem item = info.GetItemFromPath(path); item.Invoke(this); //this.NewDriveDynamicParameters( //this. } protected override object InvokeDefaultActionDynamicParameters(string path) { DriveInfo info = (DriveInfo)PSDriveInfo; DriveItem item = info.GetItemFromPath(path); return item.InvokeDefaultActionDynamicParameters(); throw new InvalidOperationException(path); } /// /// Retrieves an item using the specified path. /// /// The path to the item to return. protected override void GetItem(string path) { DriveInfo info = (DriveInfo)PSDriveInfo; DriveItem item = info.GetItemFromPath(path,this); WriteItemObject(item.GetReturnObject(), path, item is DriveContainer); } /// /// Test to see if the specified item exists. /// /// The path to the item to verify. /// True if the item is found. protected override bool ItemExists(string path) { DriveInfo info = (DriveInfo)PSDriveInfo; DriveItem item = info.GetItemFromPath(path); return item != null; } /// /// Returns Children of a container /// protected override void GetChildItems(string path, bool recurse) { DriveInfo info = (DriveInfo)PSDriveInfo; //IsContainer already passed DriveContainer parent = (DriveContainer)info.GetItemFromPath(path); ChildWriter writer =new ChildWriter(parent,path,recurse,this); writer.Flush(); } /// /// Return the names of all child items. /// /// The root path. /// Not used. protected override void GetChildNames(string path, ReturnContainers returnContainers) { DriveInfo info = (DriveInfo)PSDriveInfo; DriveContainer parent = (DriveContainer)info.GetItemFromPath(path); ChildWriter writer = new ChildWriter(parent, null, false, this); writer.Flush(); foreach (DriveItem item in parent.GetChildItems()) { WriteItemObject(item.Name, path +"\\" +item.Name, item is DriveContainer); } } /// /// Determines if the specified path has child items. /// /// The path to examine. /// /// True if the specified path has child items. /// protected override bool HasChildItems(string path) { DriveInfo info = (DriveInfo)PSDriveInfo; DriveItem item = info.GetItemFromPath(path); return item is DriveContainer; } /// /// Determine if the path specified is that of a container. /// /// The path to check. /// True if the path specifies a container. protected override bool IsItemContainer(string path) { DriveInfo info = (DriveInfo)PSDriveInfo; DriveItem item = info.GetItemFromPath(path); return item is DriveContainer; } /// /// Throws an argument exception stating that the specified path does /// not represent either a table or a row /// /// path which is invalid private void ThrowTerminatingInvalidPathException(string path) { StringBuilder message = new StringBuilder("Invalid AmtSystem path:"); message.Append(path); throw new ArgumentException(message.ToString()); } /// /// Get the name of the leaf element in the specified path /// /// /// /// The full or partial provider specific path /// /// /// /// The leaf element in the path /// protected override string GetChildName(string path) { return base.GetChildName(path); } protected override void RenameItem(string path, string newName) { DriveInfo info = (DriveInfo)PSDriveInfo; DriveItem item = info.GetItemFromPath(path); item.RenameItem(newName); } /// /// Removes the child segment of the path and returns the remaining /// parent portion /// /// /// /// A full or partial provider specific path. The path may be to an /// item that may or may not exist. /// /// /// /// The fully qualified path to the root of a drive. This parameter /// may be null or empty if a mounted drive is not in use for this /// operation. If this parameter is not null or empty the result /// of the method should not be a path to a container that is a /// parent or in a different tree than the root. /// /// /// protected override string GetParentPath(string path, string root) { return base.GetParentPath(path, root); // return parentpath } protected override object SetItemDynamicParameters(string path, object value) { DriveInfo info = (DriveInfo)PSDriveInfo; DriveItem item = info.GetItemFromPath(path); return item.SetItemDynamicParameters(); } public void ClearProperty(string path, Collection propertyToClear) { DriveInfo info = (DriveInfo)PSDriveInfo; DriveItem item = info.GetItemFromPath(path); item.ClearProperty(propertyToClear, this); } public object ClearPropertyDynamicParameters(string path, Collection propertyToClear) { return null; } public void GetProperty(string path, Collection providerSpecificPickList) { DriveInfo info = (DriveInfo)PSDriveInfo; DriveItem item = info.GetItemFromPath(path); WritePropertyObject( item.GetProperty(providerSpecificPickList,this), path); } public void SetProperty(string path, PSObject propertyValue) { DriveInfo info = (DriveInfo)PSDriveInfo; DriveItem item = info.GetItemFromPath(path); item.SetProperty(propertyValue, this); WritePropertyObject(propertyValue, path); } public object GetPropertyDynamicParameters(string path, Collection providerSpecificPickList) { return null; } public object SetPropertyDynamicParameters(string path, PSObject propertyValue) { return null; } public object ClearContentDynamicParameters(string path) { return null; } public void ClearContent(string path) { DriveInfo info = (DriveInfo)PSDriveInfo; DriveItem item = info.GetItemFromPath(path); item.ClearContent(this); } public object GetContentWriterDynamicParameters(string path) { DriveInfo info = (DriveInfo)PSDriveInfo; DriveItem item = info.GetItemFromPath(path); return item.GetContentWriterDynamicParameters(); } public IContentWriter GetContentWriter(string path) { DriveInfo info = (DriveInfo)PSDriveInfo; DriveItem item = info.GetItemFromPath(path); return item.GetContentWriter( this); } public object GetContentReaderDynamicParameters(string path) { DriveInfo info = (DriveInfo)PSDriveInfo; DriveItem item = info.GetItemFromPath(path); return item.GetContentReaderDynamicParameters(); } public IContentReader GetContentReader(string path) { DriveInfo info = (DriveInfo)PSDriveInfo; DriveItem item = info.GetItemFromPath(path); return item.GetContentReader( this); } /* private void WriteErrors(Exception exp) { //WriteErrors(exp, // ErrorCategory. // if (exp is HECIClass.UnauthorizedException) { // WriteError(new ErrorRecord( // exp, // exp.GetType().Name, // ErrorCategory.PermissionDenied, /// PSDriveInfo)); } // else { WriteError(new ErrorRecord( exp, exp.GetType().Name, ErrorCategory.NotSpecified, PSDriveInfo)); } }*/ /// /// Checks if a given path is actually a drive name. /// /// The path to check. /// /// True if the path given represents a drive, false otherwise. /// private bool PathIsDrive(string path) { // check for root path (with or without separators) if (string.IsNullOrEmpty(path) || string.Compare(path, PSDriveInfo.Name, true) == 0 || string.Compare(path, PSDriveInfo.Name + "\\", true) == 0 || string.Compare(path, PSDriveInfo.Name + "/", true) == 0 || string.Compare(path, "/", true) == 0 || string.Compare(path, "\\", true) == 0) { return true; } else { return false; } } // PathIsDrive /// /// Test to see if the specified path is syntactically valid. /// /// The path to validate. /// True if the specified path is valid. protected override bool IsValidPath(string path) { DriveInfo info = (DriveInfo)PSDriveInfo; object item = info.GetItemFromPath(path); return item != null; } } }