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.

преди 8 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using Alphaleonis.Win32.Filesystem;
  2. using Security2;
  3. using System;
  4. using System.Linq;
  5. using System.Management.Automation;
  6. using System.Security.AccessControl;
  7. namespace NTFSSecurity
  8. {
  9. [Cmdlet(VerbsCommon.Remove, "NTFSAccess", DefaultParameterSetName = "PathComplex")]
  10. [OutputType(typeof(FileSystemAccessRule2))]
  11. public class RemoveAccess : BaseCmdletWithPrivControl
  12. {
  13. private IdentityReference2[] account;
  14. private FileSystemRights2 accessRights;
  15. private AccessControlType accessType = AccessControlType.Allow;
  16. private InheritanceFlags inheritanceFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
  17. private PropagationFlags propagationFlags = PropagationFlags.None;
  18. private ApplyTo appliesTo;
  19. private bool removeSpecific;
  20. private bool passThru;
  21. [Parameter(Mandatory = true, Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "PathSimple")]
  22. [Parameter(Mandatory = true, Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "PathComplex")]
  23. [ValidateNotNullOrEmpty]
  24. [Alias("FullName")]
  25. public string[] Path
  26. {
  27. get { return paths.ToArray(); }
  28. set
  29. {
  30. paths.Clear();
  31. paths.AddRange(value);
  32. }
  33. }
  34. [Parameter(Mandatory = true, Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "SDSimple")]
  35. [Parameter(Mandatory = true, Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "SDComplex")]
  36. [ValidateNotNullOrEmpty]
  37. public FileSystemSecurity2[] SecurityDescriptor
  38. {
  39. get { return securityDescriptors.ToArray(); }
  40. set
  41. {
  42. securityDescriptors.Clear();
  43. securityDescriptors.AddRange(value);
  44. }
  45. }
  46. [Parameter(Mandatory = true, Position = 2, ValueFromPipelineByPropertyName = true)]
  47. [Alias("IdentityReference, ID")]
  48. public IdentityReference2[] Account
  49. {
  50. get { return account; }
  51. set { account = value; }
  52. }
  53. [Parameter(Mandatory = true, Position = 3, ValueFromPipelineByPropertyName = true)]
  54. [Alias("FileSystemRights")]
  55. public FileSystemRights2 AccessRights
  56. {
  57. get { return accessRights; }
  58. set { accessRights = value; }
  59. }
  60. [Parameter(ValueFromPipelineByPropertyName = true)]
  61. [Alias("AccessControlType")]
  62. public AccessControlType AccessType
  63. {
  64. get { return accessType; }
  65. set { accessType = value; }
  66. }
  67. [Parameter(ValueFromPipelineByPropertyName = true, ParameterSetName = "PathComplex")]
  68. [Parameter(ValueFromPipelineByPropertyName = true, ParameterSetName = "SDComplex")]
  69. public InheritanceFlags InheritanceFlags
  70. {
  71. get { return inheritanceFlags; }
  72. set { inheritanceFlags = value; }
  73. }
  74. [Parameter(ValueFromPipelineByPropertyName = true, ParameterSetName = "PathComplex")]
  75. [Parameter(ValueFromPipelineByPropertyName = true, ParameterSetName = "SDComplex")]
  76. public PropagationFlags PropagationFlags
  77. {
  78. get { return propagationFlags; }
  79. set { propagationFlags = value; }
  80. }
  81. [Parameter(ValueFromPipelineByPropertyName = true, ParameterSetName = "PathSimple")]
  82. [Parameter(ValueFromPipelineByPropertyName = true, ParameterSetName = "SDSimple")]
  83. public ApplyTo AppliesTo
  84. {
  85. get { return appliesTo; }
  86. set { appliesTo = value; }
  87. }
  88. [Parameter]
  89. public SwitchParameter PassThru
  90. {
  91. get { return passThru; }
  92. set { passThru = value; }
  93. }
  94. protected override void BeginProcessing()
  95. {
  96. base.BeginProcessing();
  97. }
  98. protected override void ProcessRecord()
  99. {
  100. if (ParameterSetName.EndsWith("Simple"))
  101. {
  102. FileSystemSecurity2.ConvertToFileSystemFlags(appliesTo, out inheritanceFlags, out propagationFlags);
  103. }
  104. if (ParameterSetName.StartsWith("Path"))
  105. {
  106. foreach (var path in paths)
  107. {
  108. FileSystemInfo item = null;
  109. try
  110. {
  111. item = GetFileSystemInfo2(path);
  112. }
  113. catch (Exception ex)
  114. {
  115. WriteError(new ErrorRecord(ex, "ReadFileError", ErrorCategory.OpenError, path));
  116. }
  117. if (ParameterSetName == "PathSimple")
  118. {
  119. FileSystemSecurity2.ConvertToFileSystemFlags(appliesTo, out inheritanceFlags, out propagationFlags);
  120. }
  121. try
  122. {
  123. FileSystemAccessRule2.RemoveFileSystemAccessRule(item, account.ToList(), accessRights, accessType, inheritanceFlags, propagationFlags);
  124. }
  125. catch (UnauthorizedAccessException)
  126. {
  127. try
  128. {
  129. var ownerInfo = FileSystemOwner.GetOwner(item);
  130. var previousOwner = ownerInfo.Owner;
  131. FileSystemOwner.SetOwner(item, System.Security.Principal.WindowsIdentity.GetCurrent().User);
  132. FileSystemAccessRule2.RemoveFileSystemAccessRule(item, account.ToList(), accessRights, accessType, inheritanceFlags, propagationFlags);
  133. FileSystemOwner.SetOwner(item, previousOwner);
  134. }
  135. catch (Exception ex2)
  136. {
  137. WriteError(new ErrorRecord(ex2, "RemoveAceError", ErrorCategory.WriteError, path));
  138. }
  139. }
  140. catch (Exception ex)
  141. {
  142. WriteError(new ErrorRecord(ex, "RemoveAceError", ErrorCategory.WriteError, path));
  143. }
  144. if (passThru == true)
  145. {
  146. FileSystemAccessRule2.GetFileSystemAccessRules(item, true, true).ForEach(ace => WriteObject(ace));
  147. }
  148. }
  149. }
  150. else
  151. {
  152. foreach (var sd in securityDescriptors)
  153. {
  154. FileSystemAccessRule2.RemoveFileSystemAccessRule(sd, account.ToList(), accessRights, accessType, inheritanceFlags, propagationFlags);
  155. if (passThru == true)
  156. {
  157. FileSystemAccessRule2.GetFileSystemAccessRules(sd, true, true).ForEach(ace => WriteObject(ace));
  158. }
  159. }
  160. }
  161. }
  162. protected override void EndProcessing()
  163. {
  164. base.EndProcessing();
  165. }
  166. }
  167. }