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.
 
 

75 lines
2.3 KiB

  1. using Alphaleonis.Win32.Filesystem;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Management.Automation;
  5. namespace NTFSSecurity
  6. {
  7. [Cmdlet(VerbsCommon.Get, "NTFSHardLink")]
  8. [OutputType(typeof(FileInfo), typeof(DirectoryInfo))]
  9. public class GetHardLink : BaseCmdlet
  10. {
  11. System.Reflection.MethodInfo modeMethodInfo = null;
  12. [Parameter(Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
  13. [ValidateNotNullOrEmpty]
  14. [Alias("FullName")]
  15. public string[] Path
  16. {
  17. get { return paths.ToArray(); }
  18. set
  19. {
  20. paths.Clear();
  21. paths.AddRange(value);
  22. }
  23. }
  24. protected override void BeginProcessing()
  25. {
  26. base.BeginProcessing();
  27. if (paths.Count == 0)
  28. {
  29. paths = new List<string>() { GetVariableValue("PWD").ToString() };
  30. }
  31. modeMethodInfo = typeof(FileSystemCodeMembers).GetMethod("Mode");
  32. }
  33. protected override void ProcessRecord()
  34. {
  35. foreach (var path in paths)
  36. {
  37. try
  38. {
  39. var root = System.IO.Path.GetPathRoot(GetRelativePath(path));
  40. //access the path to make sure it exists and is a file
  41. var item = GetFileSystemInfo2(path);
  42. if (item is DirectoryInfo)
  43. throw new ArgumentException("The item must be a file");
  44. var links = File.EnumerateHardlinks(item.FullName);
  45. foreach (var link in links)
  46. {
  47. var target = new PSObject(GetFileSystemInfo2(System.IO.Path.Combine(root, link.Substring(1))));
  48. target.Properties.Add(new PSCodeProperty("Mode", modeMethodInfo));
  49. WriteObject(target);
  50. }
  51. }
  52. catch (System.IO.FileNotFoundException ex)
  53. {
  54. WriteError(new ErrorRecord(ex, "FileNotFound", ErrorCategory.ObjectNotFound, path));
  55. }
  56. }
  57. }
  58. protected override void EndProcessing()
  59. {
  60. base.EndProcessing();
  61. }
  62. }
  63. }