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.
 
 

129 rivejä
4.2 KiB

  1. using Alphaleonis.Win32.Filesystem;
  2. using Security2;
  3. using System;
  4. using System.Management.Automation;
  5. namespace NTFSSecurity
  6. {
  7. [Cmdlet(VerbsLifecycle.Disable, "NTFSAuditInheritance", DefaultParameterSetName = "Path")]
  8. public class DisableAuditInheritance : BaseCmdletWithPrivControl
  9. {
  10. private bool removeInheritedAccessRules;
  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 RemoveInheritedAccessRules
  37. {
  38. get { return removeInheritedAccessRules; }
  39. set { removeInheritedAccessRules = value; }
  40. }
  41. [Parameter]
  42. public SwitchParameter PassThru
  43. {
  44. get { return passThru; }
  45. set { passThru = 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.DisableAuditInheritance(item, removeInheritedAccessRules);
  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.DisableAuditInheritance(item, removeInheritedAccessRules);
  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.DisableAuditInheritance(sd, removeInheritedAccessRules);
  107. if (passThru)
  108. {
  109. FileSystemInheritanceInfo.GetFileSystemInheritanceInfo(sd);
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }