Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

GetHardLink.cs 2.3 KiB

7 anni fa
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. }