Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

SetOwner.cs 3.2 KiB

před 8 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using Alphaleonis.Win32.Filesystem;
  2. using Security2;
  3. using System;
  4. using System.Management.Automation;
  5. namespace NTFSSecurity
  6. {
  7. [Cmdlet(VerbsCommon.Set, "NTFSOwner", DefaultParameterSetName = "Path")]
  8. [OutputType(typeof(FileSystemOwner))]
  9. public class SetOwner : BaseCmdletWithPrivControl
  10. {
  11. private SwitchParameter passThru;
  12. private IdentityReference2 account;
  13. [Parameter(Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "Path")]
  14. [ValidateNotNullOrEmpty]
  15. [Alias("FullName")]
  16. public string[] Path
  17. {
  18. get { return paths.ToArray(); }
  19. set
  20. {
  21. paths.Clear();
  22. paths.AddRange(value);
  23. }
  24. }
  25. [Parameter(Mandatory = true, Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "SecurityDescriptor")]
  26. [ValidateNotNullOrEmpty]
  27. public FileSystemSecurity2[] SecurityDescriptor
  28. {
  29. get { return securityDescriptors.ToArray(); }
  30. set
  31. {
  32. securityDescriptors.Clear();
  33. securityDescriptors.AddRange(value);
  34. }
  35. }
  36. [Parameter(Mandatory = true, Position = 2, ValueFromPipelineByPropertyName = true)]
  37. public IdentityReference2 Account
  38. {
  39. get { return account; }
  40. set { account = value; }
  41. }
  42. [Parameter]
  43. public SwitchParameter PassThru
  44. {
  45. get { return passThru; }
  46. set { passThru = value; }
  47. }
  48. protected override void BeginProcessing()
  49. {
  50. base.BeginProcessing();
  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. FileSystemOwner.SetOwner(item, account);
  71. WriteDebug("Owner set on item {0}", item.FullName);
  72. if (passThru)
  73. {
  74. WriteObject(FileSystemOwner.GetOwner(item));
  75. }
  76. }
  77. catch (Exception ex)
  78. {
  79. WriteError(new ErrorRecord(ex, "SetOwnerError", ErrorCategory.WriteError, path));
  80. continue;
  81. }
  82. }
  83. }
  84. else
  85. {
  86. foreach (var sd in securityDescriptors)
  87. {
  88. FileSystemOwner.SetOwner(sd, account);
  89. if (passThru)
  90. {
  91. WriteObject(FileSystemOwner.GetOwner(sd));
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }