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.

85 righe
2.8 KiB

  1. using Alphaleonis.Win32.Filesystem;
  2. using NTFSSecurity;
  3. using Security2;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Management.Automation;
  8. using System.Management.Automation.Runspaces;
  9. using System.Security.AccessControl;
  10. using System.Security.Principal;
  11. namespace TestClient
  12. {
  13. class Program
  14. {
  15. static void Main(string[] args)
  16. {
  17. var item1 = new FileInfo("D:\\file1.txt");
  18. var item2 = new DirectoryInfo("D:\\test3");
  19. var account1 = new List<IdentityReference2>() { (IdentityReference2)@"raandree1\randr_000" };
  20. FileSystemAccessRule2.AddFileSystemAccessRule(item1, account1, FileSystemRights2.FullControl, AccessControlType.Allow, InheritanceFlags.ContainerInherit, PropagationFlags.None);
  21. return;
  22. var path = @"C:\Windows";
  23. var account = @"raandree1\randr_000";
  24. var server = "localhost";
  25. var sd = Directory.GetAccessControl(path, AccessControlSections.Access);
  26. var id = new IdentityReference2(account);
  27. EffectiveAccess.GetEffectiveAccess(new FileInfo(path), id, "localhost");
  28. var result1 = InvokeCommand("gi2 c:\\windows");
  29. var result2 = InvokeCommand(@"gi -Path D:\SingleMachine\ | Get-EffectiveAccess")
  30. .Select(ace => ace.ImmediateBaseObject)
  31. .Cast<FileSystemAccessRule2>().ToList();
  32. foreach (var ace in result2)
  33. {
  34. Console.WriteLine(string.Format("{0};{1}", ace.Account, ace.IsInherited));
  35. }
  36. Console.ReadKey();
  37. }
  38. public static List<PSObject> InvokeCommand(string script)
  39. {
  40. var runspace = RunspaceFactory.CreateRunspace();
  41. runspace.Open();
  42. var powershell = PowerShell.Create();
  43. powershell.Runspace = runspace;
  44. powershell.Commands.AddScript(script);
  45. var result = powershell.Invoke();
  46. powershell.Dispose();
  47. runspace.Close();
  48. return result.ToList();
  49. }
  50. public static List<PSObject> InvokeCommand(string command, Dictionary<string, object> parameters)
  51. {
  52. var runspace = RunspaceFactory.CreateRunspace();
  53. runspace.Open();
  54. var powershell = PowerShell.Create();
  55. powershell.Runspace = runspace;
  56. var cmd = new Command(command);
  57. foreach (var parameter in parameters)
  58. {
  59. cmd.Parameters.Add(parameter.Key, parameter.Value);
  60. }
  61. powershell.Commands.AddCommand(cmd);
  62. var result = powershell.Invoke();
  63. powershell.Dispose();
  64. runspace.Close();
  65. return result.ToList();
  66. }
  67. }
  68. }