Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

EnableAuditInheritance.cs 4.2 KiB

7 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using Alphaleonis.Win32.Filesystem;
  2. using Security2;
  3. using System;
  4. using System.Management.Automation;
  5. namespace NTFSSecurity
  6. {
  7. [Cmdlet(VerbsLifecycle.Enable, "NTFSAuditInheritance", DefaultParameterSetName = "Path")]
  8. public class EnableAuditInheritance : BaseCmdletWithPrivControl
  9. {
  10. private bool removeExplicitAccessRules;
  11. private bool passThru;
  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. [Parameter]
  36. public SwitchParameter PassThru
  37. {
  38. get { return passThru; }
  39. set { passThru = value; }
  40. }
  41. [Parameter]
  42. public SwitchParameter RemoveExplicitAccessRules
  43. {
  44. get { return removeExplicitAccessRules; }
  45. set { removeExplicitAccessRules = value; }
  46. }
  47. protected override void BeginProcessing()
  48. {
  49. base.BeginProcessing();
  50. EnableFileSystemPrivileges(true);
  51. }
  52. protected override void ProcessRecord()
  53. {
  54. if (ParameterSetName == "Path")
  55. {
  56. foreach (var path in paths)
  57. {
  58. FileSystemInfo item = null;
  59. try
  60. {
  61. item = GetFileSystemInfo2(path);
  62. }
  63. catch (Exception ex)
  64. {
  65. WriteError(new ErrorRecord(ex, "ReadFileError", ErrorCategory.OpenError, path));
  66. continue;
  67. }
  68. try
  69. {
  70. FileSystemInheritanceInfo.EnableAuditInheritance(item, removeExplicitAccessRules);
  71. }
  72. catch (UnauthorizedAccessException)
  73. {
  74. try
  75. {
  76. var ownerInfo = FileSystemOwner.GetOwner(item);
  77. var previousOwner = ownerInfo.Owner;
  78. FileSystemOwner.SetOwner(item, System.Security.Principal.WindowsIdentity.GetCurrent().User);
  79. FileSystemInheritanceInfo.EnableAuditInheritance(item, removeExplicitAccessRules);
  80. FileSystemOwner.SetOwner(item, previousOwner);
  81. }
  82. catch (Exception ex2)
  83. {
  84. WriteError(new ErrorRecord(ex2, "ModifySdError", ErrorCategory.WriteError, path));
  85. continue;
  86. }
  87. }
  88. catch (Exception ex)
  89. {
  90. WriteError(new ErrorRecord(ex, "ModifySdError", ErrorCategory.WriteError, path));
  91. continue;
  92. }
  93. finally
  94. {
  95. if (passThru)
  96. {
  97. FileSystemInheritanceInfo.GetFileSystemInheritanceInfo(item);
  98. }
  99. }
  100. }
  101. }
  102. else
  103. {
  104. foreach (var sd in securityDescriptors)
  105. {
  106. FileSystemInheritanceInfo.EnableAuditInheritance(sd, removeExplicitAccessRules);
  107. if (passThru)
  108. {
  109. FileSystemInheritanceInfo.GetFileSystemInheritanceInfo(sd);
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }