Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

GetInheritance.cs 4.0 KiB

7 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using Alphaleonis.Win32.Filesystem;
  2. using Security2;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Management.Automation;
  6. namespace NTFSSecurity
  7. {
  8. [Cmdlet(VerbsCommon.Get, "NTFSInheritance", DefaultParameterSetName = "Path")]
  9. [OutputType(typeof(FileSystemInheritanceInfo))]
  10. public class GetInheritance : BaseCmdletWithPrivControl
  11. {
  12. [Parameter(Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "Path")]
  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. [Parameter(Mandatory = true, Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "SecurityDescriptor")]
  25. [ValidateNotNullOrEmpty]
  26. public FileSystemSecurity2[] SecurityDescriptor
  27. {
  28. get { return securityDescriptors.ToArray(); }
  29. set
  30. {
  31. securityDescriptors.Clear();
  32. securityDescriptors.AddRange(value);
  33. }
  34. }
  35. protected override void BeginProcessing()
  36. {
  37. base.BeginProcessing();
  38. EnableFileSystemPrivileges(true);
  39. if (paths.Count == 0)
  40. {
  41. paths = new List<string>() { GetVariableValue("PWD").ToString() };
  42. }
  43. }
  44. protected override void ProcessRecord()
  45. {
  46. if (ParameterSetName == "Path")
  47. {
  48. foreach (var path in paths)
  49. {
  50. FileSystemInfo item = null;
  51. FileSystemInheritanceInfo inheritanceInfo = null;
  52. try
  53. {
  54. item = GetFileSystemInfo2(path);
  55. }
  56. catch (Exception ex)
  57. {
  58. WriteError(new ErrorRecord(ex, "ReadFileError", ErrorCategory.OpenError, path));
  59. continue;
  60. }
  61. try
  62. {
  63. inheritanceInfo = FileSystemInheritanceInfo.GetFileSystemInheritanceInfo(item);
  64. }
  65. catch (UnauthorizedAccessException)
  66. {
  67. try
  68. {
  69. var ownerInfo = FileSystemOwner.GetOwner(item);
  70. var previousOwner = ownerInfo.Owner;
  71. FileSystemOwner.SetOwner(item, System.Security.Principal.WindowsIdentity.GetCurrent().User);
  72. inheritanceInfo = FileSystemInheritanceInfo.GetFileSystemInheritanceInfo(item);
  73. FileSystemOwner.SetOwner(item, previousOwner);
  74. }
  75. catch (Exception ex2)
  76. {
  77. WriteError(new ErrorRecord(ex2, "ReadSecurityError", ErrorCategory.WriteError, path));
  78. continue;
  79. }
  80. }
  81. catch (Exception ex)
  82. {
  83. WriteError(new ErrorRecord(ex, "ReadSecurityError", ErrorCategory.OpenError, path));
  84. continue;
  85. }
  86. finally
  87. {
  88. if (inheritanceInfo != null)
  89. {
  90. WriteObject(inheritanceInfo);
  91. }
  92. }
  93. }
  94. }
  95. else
  96. {
  97. foreach (var sd in securityDescriptors)
  98. {
  99. var inheritanceInfo = FileSystemInheritanceInfo.GetFileSystemInheritanceInfo(sd);
  100. if (inheritanceInfo != null)
  101. {
  102. WriteObject(inheritanceInfo);
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }