/// <summary> /// Overridden method to remove a drive. /// </summary> /// <param name="drive">The drive to remove.</param> /// <returns>The removed drive.</returns> protected override PSDriveInfo RemoveDrive(PSDriveInfo drive) { if (drive == null) { WriteError(new ErrorRecord( new ArgumentNullException(nameof(drive)), "NullDrive", ErrorCategory.InvalidArgument, drive)); return(null); } ObjectManagerPSDriveInfo objmgr_drive = drive as ObjectManagerPSDriveInfo; if (objmgr_drive == null) { return(null); } objmgr_drive.Close(); return(objmgr_drive); }
/// <summary> /// Overridden method to create a new drive. /// </summary> /// <param name="drive">The template drive info.</param> /// <returns>The new drive info.</returns> protected override PSDriveInfo NewDrive(PSDriveInfo drive) { if (drive == null) { WriteError(new ErrorRecord( new ArgumentNullException(nameof(drive)), "NullDrive", ErrorCategory.InvalidArgument, null)); return(null); } if (string.IsNullOrWhiteSpace(drive.Root) && (!drive.Root.StartsWith(GLOBAL_ROOT) || !drive.Root.StartsWith(NAMESPACE_ROOT) || !drive.Root.StartsWith(KEY_ROOT))) { WriteError(new ErrorRecord( new ArgumentNullException("drive.Root"), "InvalidRoot", ErrorCategory.InvalidArgument, null)); return(null); } try { if (drive.Root.StartsWith(NAMESPACE_ROOT)) { using (var descriptor = BoundaryDescriptor.CreateFromString(drive.Root.Substring(NAMESPACE_ROOT.Length))) { using (NtDirectory dir = NtDirectory.OpenPrivateNamespace(descriptor)) { ObjectManagerPSDriveInfo objmgr_drive = new ObjectManagerPSDriveInfo(dir.Duplicate(), drive); return(objmgr_drive); } } } else if (drive.Root.StartsWith(GLOBAL_ROOT)) { using (NtDirectory root = NtDirectory.Open(@"\")) { using (NtDirectory dir = NtDirectory.Open(drive.Root.Substring(GLOBAL_ROOT.Length), root, DirectoryAccessRights.MaximumAllowed)) { ObjectManagerPSDriveInfo objmgr_drive = new ObjectManagerPSDriveInfo(dir.Duplicate(), drive); return(objmgr_drive); } } } else { using (NtKey root = NtKey.Open(@"\Registry", null, KeyAccessRights.MaximumAllowed)) { using (NtKey key = NtKey.Open(drive.Root.Substring(KEY_ROOT.Length).TrimStart('\\'), root, KeyAccessRights.MaximumAllowed)) { ObjectManagerPSDriveInfo objmgr_drive = new ObjectManagerPSDriveInfo(key.Duplicate(), drive); return(objmgr_drive); } } } } catch (NtException ex) { WriteError(new ErrorRecord( ex, "NoRoot", ErrorCategory.PermissionDenied, drive)); return(null); } }