Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 

119 wiersze
3.9 KiB

  1. using Alphaleonis.Win32.Filesystem;
  2. using Security2;
  3. using System;
  4. using System.Management.Automation;
  5. namespace NTFSSecurity
  6. {
  7. [Cmdlet(VerbsCommon.Clear, "NTFSAccess", DefaultParameterSetName = "Path")]
  8. public class ClearAccess : BaseCmdletWithPrivControl
  9. {
  10. private SwitchParameter disableInheritance;
  11. [Parameter(Mandatory = true, Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "Path")]
  12. [ValidateNotNullOrEmpty]
  13. [Alias("FullName")]
  14. public string[] Path
  15. {
  16. get { return paths.ToArray(); }
  17. set
  18. {
  19. paths.Clear();
  20. paths.AddRange(value);
  21. }
  22. }
  23. [Parameter(Mandatory = true, Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "SD")]
  24. [ValidateNotNullOrEmpty]
  25. public FileSystemSecurity2[] SecurityDescriptor
  26. {
  27. get { return securityDescriptors.ToArray(); }
  28. set
  29. {
  30. securityDescriptors.Clear();
  31. securityDescriptors.AddRange(value);
  32. }
  33. }
  34. [Parameter]
  35. public SwitchParameter DisableInheritance
  36. {
  37. get { return disableInheritance; }
  38. set { disableInheritance = value; }
  39. }
  40. protected override void BeginProcessing()
  41. {
  42. base.BeginProcessing();
  43. }
  44. protected override void ProcessRecord()
  45. {
  46. if (ParameterSetName == "Path")
  47. {
  48. FileSystemInfo item = null;
  49. foreach (var path in paths)
  50. {
  51. try
  52. {
  53. item = GetFileSystemInfo2(path);
  54. }
  55. catch (Exception ex)
  56. {
  57. WriteError(new ErrorRecord(ex, "ReadFileError", ErrorCategory.OpenError, path));
  58. continue;
  59. }
  60. try
  61. {
  62. FileSystemAccessRule2.RemoveFileSystemAccessRuleAll(item);
  63. if (disableInheritance)
  64. FileSystemInheritanceInfo.DisableAccessInheritance(item, true);
  65. }
  66. catch (UnauthorizedAccessException)
  67. {
  68. try
  69. {
  70. var ownerInfo = FileSystemOwner.GetOwner(item);
  71. var previousOwner = ownerInfo.Owner;
  72. FileSystemOwner.SetOwner(item, System.Security.Principal.WindowsIdentity.GetCurrent().User);
  73. FileSystemAccessRule2.RemoveFileSystemAccessRuleAll(item);
  74. if (disableInheritance)
  75. FileSystemInheritanceInfo.DisableAccessInheritance(item, true);
  76. FileSystemOwner.SetOwner(item, previousOwner);
  77. }
  78. catch (Exception ex2)
  79. {
  80. WriteError(new ErrorRecord(ex2, "ClearAclError", ErrorCategory.WriteError, path));
  81. }
  82. }
  83. catch (Exception ex)
  84. {
  85. WriteError(new ErrorRecord(ex, "ClearAclError", ErrorCategory.WriteError, path));
  86. }
  87. }
  88. }
  89. else
  90. {
  91. foreach (var sd in securityDescriptors)
  92. {
  93. FileSystemAccessRule2.RemoveFileSystemAccessRuleAll(sd);
  94. if (disableInheritance)
  95. FileSystemInheritanceInfo.DisableAccessInheritance(sd, true);
  96. }
  97. }
  98. }
  99. protected override void EndProcessing()
  100. {
  101. base.EndProcessing();
  102. }
  103. }
  104. }