You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GetDiskSpace.cs 1.5 KiB

8 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using Alphaleonis.Win32.Filesystem;
  2. using System.Linq;
  3. using System.Management.Automation;
  4. namespace NTFSSecurity
  5. {
  6. [Cmdlet(VerbsCommon.Get, "DiskSpace")]
  7. [OutputType(typeof(DiskSpaceInfo))]
  8. public class GetDiskSpace : PSCmdlet
  9. {
  10. private string[] driveLetter;
  11. [Parameter(Position = 1)]
  12. [ValidatePattern("^[A-Za-z]:$")]
  13. public string[] DriveLetter
  14. {
  15. get { return driveLetter; }
  16. set { driveLetter = value; }
  17. }
  18. protected override void BeginProcessing()
  19. {
  20. base.BeginProcessing();
  21. }
  22. protected override void ProcessRecord()
  23. {
  24. var volumes = Alphaleonis.Win32.Filesystem.Volume.EnumerateVolumes();
  25. if (driveLetter == null)
  26. {
  27. driveLetter = volumes.ToArray();
  28. }
  29. foreach (var letter in driveLetter)
  30. {
  31. var diskSpaceInfo = new DiskSpaceInfo(letter);
  32. try
  33. {
  34. diskSpaceInfo.Refresh();
  35. if (diskSpaceInfo.TotalNumberOfBytes > 0)
  36. {
  37. this.WriteObject(diskSpaceInfo);
  38. }
  39. }
  40. catch
  41. {
  42. this.WriteWarning(string.Format("Could not get drive details for '{0}'", letter));
  43. }
  44. }
  45. }
  46. protected override void EndProcessing()
  47. {
  48. base.EndProcessing();
  49. }
  50. }
  51. }