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.

GetItem2.cs 1.7 KiB

7 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Alphaleonis.Win32.Filesystem;
  2. using System.Collections.Generic;
  3. using System.Management.Automation;
  4. namespace NTFSSecurity
  5. {
  6. [Cmdlet(VerbsCommon.Get, "Item2")]
  7. [OutputType(typeof(FileInfo), typeof(DirectoryInfo))]
  8. public class GetItem2 : BaseCmdlet
  9. {
  10. System.Reflection.MethodInfo modeMethodInfo = null;
  11. [Parameter(Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
  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. protected override void BeginProcessing()
  24. {
  25. base.BeginProcessing();
  26. if (paths.Count == 0)
  27. {
  28. paths = new List<string>() { GetVariableValue("PWD").ToString() };
  29. }
  30. modeMethodInfo = typeof(FileSystemCodeMembers).GetMethod("Mode");
  31. }
  32. protected override void ProcessRecord()
  33. {
  34. foreach (var path in paths)
  35. {
  36. try
  37. {
  38. var item = new PSObject(GetFileSystemInfo2(path));
  39. item.Properties.Add(new PSCodeProperty("Mode", modeMethodInfo));
  40. WriteObject(item);
  41. }
  42. catch (System.IO.FileNotFoundException ex)
  43. {
  44. WriteError(new ErrorRecord(ex, "FileNotFound", ErrorCategory.ObjectNotFound, path));
  45. }
  46. }
  47. }
  48. protected override void EndProcessing()
  49. {
  50. base.EndProcessing();
  51. }
  52. }
  53. }