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.

SetSecurityDescriptor.cs 2.2 KiB

7 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using Security2;
  2. using System;
  3. using System.Management.Automation;
  4. namespace NTFSSecurity
  5. {
  6. [Cmdlet(VerbsCommon.Set, "NTFSSecurityDescriptor")]
  7. [OutputType(typeof(FileSystemSecurity2))]
  8. public class SetSecurityDescriptor : BaseCmdletWithPrivControl
  9. {
  10. private SwitchParameter passThru;
  11. [Parameter(Mandatory = true, Position = 2, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
  12. public FileSystemSecurity2[] SecurityDescriptor
  13. {
  14. get { return securityDescriptors.ToArray(); }
  15. set
  16. {
  17. securityDescriptors.Clear();
  18. securityDescriptors.AddRange(value);
  19. }
  20. }
  21. [Parameter()]
  22. public SwitchParameter PassThru
  23. {
  24. get { return passThru; }
  25. set { passThru = value; }
  26. }
  27. protected override void BeginProcessing()
  28. {
  29. base.BeginProcessing();
  30. }
  31. protected override void ProcessRecord()
  32. {
  33. foreach (var sd in securityDescriptors)
  34. {
  35. try
  36. {
  37. sd.Write();
  38. if (passThru)
  39. {
  40. WriteObject(new FileSystemSecurity2(sd.Item));
  41. }
  42. }
  43. catch (UnauthorizedAccessException)
  44. {
  45. try
  46. {
  47. var ownerInfo = FileSystemOwner.GetOwner(sd.Item);
  48. var previousOwner = ownerInfo.Owner;
  49. FileSystemOwner.SetOwner(sd.Item, System.Security.Principal.WindowsIdentity.GetCurrent().User);
  50. sd.Write();
  51. FileSystemOwner.SetOwner(sd.Item, previousOwner);
  52. }
  53. catch (Exception ex2)
  54. {
  55. WriteError(new ErrorRecord(ex2, "WriteSdError", ErrorCategory.WriteError, sd.Item));
  56. continue;
  57. }
  58. }
  59. catch (Exception ex)
  60. {
  61. WriteError(new ErrorRecord(ex, "WriteSdError", ErrorCategory.WriteError, sd.Item));
  62. }
  63. }
  64. }
  65. }
  66. }