Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

GetOwner.cs 3.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using Alphaleonis.Win32.Filesystem;
  2. using Security2;
  3. using System;
  4. using System.Management.Automation;
  5. namespace NTFSSecurity.OwnerCmdlets
  6. {
  7. [Cmdlet(VerbsCommon.Get, "NTFSOwner", DefaultParameterSetName = "Path")]
  8. [OutputType(typeof(FileSystemOwner))]
  9. public class GetOwner : BaseCmdletWithPrivControl
  10. {
  11. [Parameter(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 = "SecurityDescriptor")]
  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. protected override void BeginProcessing()
  35. {
  36. base.BeginProcessing();
  37. }
  38. protected override void ProcessRecord()
  39. {
  40. if (ParameterSetName == "Path")
  41. {
  42. FileSystemInfo item = null;
  43. foreach (var path in paths)
  44. {
  45. try
  46. {
  47. item = GetFileSystemInfo2(path);
  48. }
  49. catch (Exception ex)
  50. {
  51. WriteError(new ErrorRecord(ex, "ReadFileError", ErrorCategory.OpenError, path));
  52. continue;
  53. }
  54. try
  55. {
  56. WriteObject(FileSystemOwner.GetOwner(item));
  57. }
  58. catch (UnauthorizedAccessException)
  59. {
  60. try
  61. {
  62. var ownerInfo = FileSystemOwner.GetOwner(item);
  63. var previousOwner = ownerInfo.Owner;
  64. FileSystemOwner.SetOwner(item, System.Security.Principal.WindowsIdentity.GetCurrent().User);
  65. WriteObject(FileSystemOwner.GetOwner(item));
  66. FileSystemOwner.SetOwner(item, previousOwner);
  67. }
  68. catch (Exception ex2)
  69. {
  70. WriteError(new ErrorRecord(ex2, "ReadSecurityError", ErrorCategory.WriteError, path));
  71. continue;
  72. }
  73. }
  74. catch (Exception ex)
  75. {
  76. WriteError(new ErrorRecord(ex, "ReadSecurityError", ErrorCategory.OpenError, path));
  77. continue;
  78. }
  79. }
  80. }
  81. else
  82. {
  83. foreach (var sd in securityDescriptors)
  84. {
  85. WriteObject(FileSystemOwner.GetOwner(sd));
  86. }
  87. }
  88. }
  89. }
  90. }