Advertisement
Guest User

Untitled

a guest
Jan 29th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. public static Dictionary<string, string> GetProcs(string server, string sessID)
  2. {
  3. SecureString ss = CreatePW();
  4. ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/C tasklist /S " + server + " /FI "SESSION eq " + sessID + "" /FO CSV /NH")
  5. {
  6. WindowStyle = ProcessWindowStyle.Hidden,
  7. UseShellExecute = false,
  8. RedirectStandardOutput = true,
  9. RedirectStandardError = true,
  10. CreateNoWindow = true,
  11.  
  12. WorkingDirectory = @"C:windowssystem32",
  13. Verb = "runas",
  14. Domain = "BARDOM1",
  15. UserName = "zzkillcitrix",
  16. Password = ss
  17. };
  18.  
  19. List<string> procList = new List<string>();
  20. Process proc = Process.Start(startInfo);
  21. proc.OutputDataReceived += (x, y) => procList.Add(y.Data);
  22. proc.BeginOutputReadLine();
  23. proc.WaitForExit();
  24.  
  25. // Create a new ditionary ...
  26. Dictionary<string, string> procDict = new Dictionary<string, string>();
  27.  
  28. for (int i = 0; i < procList.Count - 1; i++)
  29. {
  30. if (procDict.ContainsKey(procList[i].Split(',')[0].Trim('"')))
  31. {
  32. // Do nothing
  33. }
  34.  
  35. else
  36. {
  37. procDict.Add(procList[i].Split(',')[0].Trim('"'), procList[i].Split(',')[1].Trim('"'));
  38.  
  39. }
  40. }
  41.  
  42. return procDict;
  43. }
  44.  
  45. public static List<Proc> GetProcList(Session session)
  46. {
  47. // Get the current tasks
  48. List<string> processQueryResult = TaskList(session);
  49. List<Proc> procList = new List<Proc>();
  50.  
  51. foreach (var processDetails in processQueryResult)
  52. {
  53. // Only create the Proc if the process is in the 'valid' array ...
  54. // Get the procname
  55. string procName = processDetails.Split(',')[0].Trim('"').ToUpper();
  56.  
  57. // Make sure it's position is not -1 ...
  58. int pos = Array.IndexOf(MyGlobals.ProcArray, procName);
  59. if (pos > -1)
  60. {
  61. int procId = Int32.Parse(processDetails.Split(',')[1].Trim('"'));
  62.  
  63. Proc p = new Proc(procName, procId, session.ServerName, session.ID);
  64. procList.Add(p);
  65.  
  66. SupportMI.Trace = "--adding" + p.Name + "--";
  67. }
  68. }
  69.  
  70. return procList;
  71. }
  72.  
  73. private static List<string> TaskList(Session session)
  74. {
  75. string cmdIn = "tasklist /S " + session.ServerName + " /FI "SESSION eq " + session.ID + "" /FO CSV /NH";
  76. List<string> cmdOut = Cmd.StdOutAdminList(cmdIn);
  77.  
  78. return cmdOut;
  79. }
  80.  
  81. public static List<string> StdOutAdminList(string args)
  82. {
  83. List<string> cmdOut = new List<string>();
  84.  
  85. SecureString ss = pw();
  86. ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/C " + args)
  87. {
  88. WindowStyle = ProcessWindowStyle.Hidden,
  89. UseShellExecute = false,
  90. RedirectStandardOutput = true,
  91. RedirectStandardError = true,
  92. CreateNoWindow = true,
  93.  
  94. WorkingDirectory = @"C:windowssystem32",
  95. Verb = "runas",
  96. Domain = "BARDOM1",
  97. UserName = "zzkillcitrix",
  98. Password = ss
  99. };
  100.  
  101. cmdOut = ExecuteListCommand(startInfo);
  102. return cmdOut;
  103. }
  104.  
  105. private static List<string> ExecuteListCommand(ProcessStartInfo startInfo)
  106. {
  107. List<string> procList = new List<string>();
  108.  
  109. Process p = Process.Start(startInfo);
  110. p.OutputDataReceived += (x, y) => procList.Add(y.Data);
  111. p.BeginOutputReadLine();
  112. p.WaitForExit();
  113.  
  114. return procList;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement