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

96 řádky
3.1 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. //List<AccessControlSections> fileSystemRights = new List<AccessControlSections>();
  18. //var values = Enum.GetValues(typeof(AccessControlSections)).Cast <AccessControlSections>().ToList();
  19. //var v1 = values.Where(v => v == AccessControlSections.All).FirstOrDefault
  20. var x = Enum.
  21. var item1 = new FileInfo("D:\\file1.txt");
  22. var item2 = new DirectoryInfo("D:\\test3");
  23. var account1 = new List<IdentityReference2>() { (IdentityReference2)@"raandree1\randr_000" };
  24. FileSystemAccessRule2.AddFileSystemAccessRule(item1, account1, FileSystemRights2.FullControl, AccessControlType.Allow, InheritanceFlags.ContainerInherit, PropagationFlags.None);
  25. return;
  26. var path = @"C:\Windows";
  27. var account = @"raandree1\randr_000";
  28. var server = "localhost";
  29. var sd = Directory.GetAccessControl(path, AccessControlSections.Access);
  30. var id = new IdentityReference2(account);
  31. EffectiveAccess.GetEffectiveAccess(new FileInfo(path), id, "localhost");
  32. var result1 = InvokeCommand("gi2 c:\\windows");
  33. var result2 = InvokeCommand(@"gi -Path D:\SingleMachine\ | Get-EffectiveAccess")
  34. .Select(ace => ace.ImmediateBaseObject)
  35. .Cast<FileSystemAccessRule2>().ToList();
  36. foreach (var ace in result2)
  37. {
  38. Console.WriteLine(string.Format("{0};{1}", ace.Account, ace.IsInherited));
  39. }
  40. Console.ReadKey();
  41. }
  42. public static List<PSObject> InvokeCommand(string script)
  43. {
  44. var runspace = RunspaceFactory.CreateRunspace();
  45. runspace.Open();
  46. var powershell = PowerShell.Create();
  47. powershell.Runspace = runspace;
  48. powershell.Commands.AddScript(script);
  49. var result = powershell.Invoke();
  50. powershell.Dispose();
  51. runspace.Close();
  52. return result.ToList();
  53. }
  54. public static List<PSObject> InvokeCommand(string command, Dictionary<string, object> parameters)
  55. {
  56. var runspace = RunspaceFactory.CreateRunspace();
  57. runspace.Open();
  58. var powershell = PowerShell.Create();
  59. powershell.Runspace = runspace;
  60. var cmd = new Command(command);
  61. foreach (var parameter in parameters)
  62. {
  63. cmd.Parameters.Add(parameter.Key, parameter.Value);
  64. }
  65. powershell.Commands.AddCommand(cmd);
  66. var result = powershell.Invoke();
  67. powershell.Dispose();
  68. runspace.Close();
  69. return result.ToList();
  70. }
  71. }
  72. }