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.
 
 

108 wiersze
3.2 KiB

  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. }