Advertisement
Guest User

Untitled

a guest
Jan 27th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.41 KB | None | 0 0
  1. public void backgroundReset_DoWork(object sender, DoWorkEventArgs e)
  2. {
  3. // Allow page change when whilst on a thread other than the Form's
  4. pageControl1.Invoke(new Action(() =>
  5. {
  6. pageControl1.SelectedIndex = 10;
  7. }));
  8.  
  9. // _currentsession is initialised right at the beginning, in a separate backgroundWorker (if there is no session, one is created and assigned to _currentsession)
  10. Session oldsession = _currentSession;
  11. Session newsession = oldsession.Reset();
  12.  
  13. e.Result = newsession;
  14. }
  15.  
  16. public void backgroundReset_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  17. {
  18. Session newsession = (Session)e.Result;
  19.  
  20. if (newsession.ID == null)
  21. {
  22. // No session - revert to emergency support page
  23. pageControl1.SelectedIndex = 5;
  24. }
  25. else
  26. {
  27. // Assign the new session to our session field, and navigate to the "work done" page
  28. _currentSession = newsession;
  29. pageControl1.SelectedIndex = 3;
  30. }
  31. }
  32.  
  33. class Session
  34. {
  35. #region Fields
  36. private string _username;
  37. private string _pcname;
  38. private int? _id; // the ? allows it to be nullable ... to make checking easier
  39. private string _server;
  40. #endregion
  41.  
  42. #region Properties
  43. public string UserName
  44. {
  45. get { return _username; }
  46. set { _username = value; }
  47. }
  48. public string PCName
  49. {
  50. get { return _pcname; }
  51. set { _pcname = value; }
  52. }
  53. public int? ID
  54. {
  55. get { return _id; }
  56. set { _id = value; }
  57. }
  58. public string ServerName
  59. {
  60. get { return _server; }
  61. set { _server = value; }
  62. }
  63. #endregion
  64.  
  65. public Session()
  66. {
  67. // When a session is instantiated, retrieve the details immediately:
  68. PCName = Environment.MachineName;
  69. UserName = WindowsIdentity.GetCurrent().Name.Split('\')[1];
  70.  
  71. SessionHelper.GetSession(this);
  72. }
  73.  
  74. public Session Reset()
  75. {
  76. SessionHelper.Reset(this);
  77. return this;
  78. }
  79. }
  80.  
  81. static class SessionHelper
  82. {
  83. public static string AppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  84. private static string AppDataCitrix = AppData + "\Citrix\SelfService\";
  85.  
  86. //////////////////////////////////////////////////////////// Destroy - Log session off
  87. static public void Destroy(Session session)
  88. {
  89. string cmdIn = "Logoff " + session.ID + " /SERVER:" + session.ServerName;
  90. Cmd.Exec(cmdIn);
  91. }
  92. //////////////////////////////////////////////////////////// Reset
  93. public static Session Reset(Session oldsession)
  94. {
  95. Destroy(oldsession);
  96.  
  97. while (oldsession.ID != null)
  98. {
  99. // If there is no session, CheckSession will update the details with null
  100. CheckSession(oldsession);
  101. }
  102.  
  103. System.Threading.Thread.Sleep(5000);
  104.  
  105. // Start outlook
  106. Start();
  107.  
  108. Session newsession = GetSession(oldsession);
  109.  
  110. // Query all servers until we find a session
  111. while (newsession.ID == null)
  112. {
  113. newsession = GetSession(oldsession);
  114. }
  115.  
  116. // While lync isn't running, sleep 1 second then check again
  117. while (IsRunning(newsession, "lync.exe") == false)
  118. {
  119. System.Threading.Thread.Sleep(1000);
  120. }
  121.  
  122. return newsession;
  123. }
  124. //////////////////////////////////////////////////////////// Is Running
  125. public static bool IsRunning(Session session, string processName)
  126. {
  127. string strcmdIn = "tasklist /S " + session.ServerName + " /FI "SESSION eq " + session.ID + """;
  128. string cmdOut = Cmd.StdOutAdmin(strcmdIn);
  129.  
  130. if (cmdOut.Contains(processName))
  131. {
  132. return true;
  133. }
  134. else
  135. {
  136. return false;
  137. }
  138. }
  139. //////////////////////////////////////////////////////////// Start Session
  140. public static void Start()
  141. {
  142. Process.Start(AppDataCitrix + "Outlook.exe");
  143. }
  144. //////////////////////////////////////////////////////////// Check Session - Checks whether the given session is active, and updates it (whether it is active or not)
  145. static public void CheckSession(Session session)
  146. {
  147. string queryResult = Query(session, session.ServerName);
  148.  
  149. if (queryResult.Contains(session.UserName))
  150. {
  151. try
  152. {
  153. session.ID = Int32.Parse(queryResult.Substring(119, 4).Trim());
  154. session.ServerName = session.ServerName;
  155. }
  156. catch (FormatException)
  157. {
  158. // The query output did not have a session id, or the session id was at a different index in the string
  159. }
  160. }
  161. else
  162. {
  163. session.ID = null;
  164. session.ServerName = null;
  165. }
  166. }
  167. //////////////////////////////////////////////////////////// Query - queries the given server against the session's user name and return the results
  168. static private string Query(Session session, string server)
  169. {
  170. string cmdIn = "query session " + session.UserName + " /SERVER:" + server;
  171. string cmdOut = Cmd.StdOut(cmdIn);
  172.  
  173. return cmdOut;
  174. }
  175. //////////////////////////////////////////////////////////// Get Session - check all servers to find the current session
  176. public static Session GetSession(Session session)
  177. {
  178. // Session session = new Session();
  179. string queryResult;
  180.  
  181. session = CheckFile(session);
  182.  
  183. // If the CheckFile method returned the current session ...
  184. if (session.ID != null)
  185. {
  186. return session;
  187. }
  188.  
  189. foreach (string server in Servers.List)
  190. {
  191. queryResult = Query(session, server);
  192.  
  193. if (queryResult.Contains(session.UserName))
  194. {
  195. try
  196. {
  197. session.ID = Int32.Parse(queryResult.Substring(119, 4).Trim());
  198. session.ServerName = server;
  199.  
  200. return session;
  201. }
  202. catch (FormatException)
  203. {
  204. // ID not in valid int format
  205. }
  206. }
  207. }
  208.  
  209. return session;
  210. }
  211. //////////////////////////////////////////////////////////// Revive session - when there is no session but there should be
  212. public static Session Revive(Session session)
  213. {
  214. Start();
  215.  
  216. // Query all servers until we find a session
  217. while (session.ID == null/* && ++count < 15*/)
  218. {
  219. session = GetSession(session);
  220. }
  221.  
  222. // While outlook isn't running, sleep 1 second then check again
  223. while (IsRunning(session, "lync.exe") == false)
  224. {
  225. // System.Threading.Thread.Sleep(1000);
  226. }
  227.  
  228. return session;
  229. }
  230. //////////////////////////////////////////////////////////// Check Server File - file created by the log on script
  231. private static Session CheckFile(Session session)
  232. {
  233. string[] allFiles = System.IO.Directory.GetFiles("\\file\Home$\" + session.UserName + "\Application Data");
  234. string serverFileId = "qxjz";
  235. string servName;
  236. string queryResult;
  237.  
  238. foreach (string file in allFiles)
  239. {
  240. // Found the file containing the code
  241. if (file.Contains(serverFileId))
  242. {
  243. string[] nameArr = file.Split('\');
  244. servName = nameArr[nameArr.Length - 1];
  245. servName = servName.Substring(4, servName.Length - 8);
  246.  
  247. queryResult = Query(session, servName);
  248.  
  249. // It's the right server file!
  250. if (queryResult.Length != 0)
  251. {
  252. try
  253. {
  254. session.ID = Int32.Parse(queryResult.Substring(119, 4).Trim());
  255. session.ServerName = servName;
  256.  
  257. return session;
  258. }
  259. catch (FormatException)
  260. {
  261. // Wrong format for the session ID
  262. }
  263. }
  264.  
  265. // File exists, but its the wrong server
  266. break;
  267. }
  268. }
  269.  
  270. return session;
  271. }
  272. }
  273.  
  274. static class Cmd
  275. {
  276. /////////////////////////////////////////////////// EXECUTE COMMAND
  277. public static void Exec(string args)
  278. {
  279. ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/C " + args)
  280. {
  281. WindowStyle = ProcessWindowStyle.Hidden,
  282. UseShellExecute = false,
  283. CreateNoWindow = true
  284. };
  285.  
  286. Process p = Process.Start(startInfo);
  287. p.Start();
  288. }
  289.  
  290. /////////////////////////////////////////////////// EXECUTE COMMAND AS ADMIN
  291. public static void ExecAdmin(string args)
  292. {
  293. ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/C " + args)
  294. {
  295. WindowStyle = ProcessWindowStyle.Hidden,
  296. UseShellExecute = false,
  297. CreateNoWindow = true,
  298.  
  299. WorkingDirectory = @"C:windowssystem32",
  300. Verb = "runas",
  301. Domain = "BARDOM1",
  302. UserName = "zzkillcitrix",
  303. Password = pw()
  304. };
  305.  
  306. Process p = Process.Start(startInfo);
  307. p.Start();
  308. }
  309.  
  310. /////////////////////////////////////////////////// GET THE STDOUT
  311. public static string StdOut(string args)
  312. {
  313. string cmdOut = "";
  314.  
  315. ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/C " + args)
  316. {
  317. WindowStyle = ProcessWindowStyle.Hidden,
  318. UseShellExecute = false,
  319. RedirectStandardOutput = true,
  320. RedirectStandardError = true,
  321. CreateNoWindow = true
  322. };
  323.  
  324. cmdOut = ExecuteCommand(cmdOut, startInfo);
  325.  
  326. return cmdOut;
  327. }
  328.  
  329. /////////////////////////////////////////////////// GET THE STDOUT AS ADMIN
  330. public static string StdOutAdmin(string args)
  331. {
  332. string cmdOut = "";
  333.  
  334. ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/C " + args)
  335. {
  336. WindowStyle = ProcessWindowStyle.Hidden,
  337. UseShellExecute = false,
  338. RedirectStandardOutput = true,
  339. RedirectStandardError = true,
  340. CreateNoWindow = true,
  341.  
  342. WorkingDirectory = @"C:windowssystem32",
  343. Verb = "runas",
  344. Domain = "BARDOM1",
  345. UserName = "zzkillcitrix",
  346. Password = pw()
  347. };
  348.  
  349. cmdOut = ExecuteCommand(cmdOut, startInfo);
  350.  
  351. return cmdOut;
  352. }
  353.  
  354. /////////////////////////////////////////////////// GET THE STDOUT AS ADMIN IN LIST FORMAT
  355. public static List<string> StdOutAdminList(string args)
  356. {
  357. List<string> cmdOut = new List<string>();
  358.  
  359. ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/C " + args)
  360. {
  361. WindowStyle = ProcessWindowStyle.Hidden,
  362. UseShellExecute = false,
  363. RedirectStandardOutput = true,
  364. RedirectStandardError = true,
  365. CreateNoWindow = true,
  366.  
  367. WorkingDirectory = @"C:windowssystem32",
  368. Verb = "runas",
  369. Domain = "BARDOM1",
  370. UserName = "zzkillcitrix",
  371. Password = pw()
  372. };
  373.  
  374. cmdOut = ExecuteCommand(cmdOut, startInfo);
  375.  
  376. return cmdOut;
  377. }
  378.  
  379. /////////////////////////////////////////////////// EXECUTE COMMAND
  380. private static string ExecuteCommand(string cmdOut, ProcessStartInfo startInfo)
  381. {
  382. Process p = Process.Start(startInfo);
  383. p.OutputDataReceived += (x, y) => cmdOut += y.Data;
  384. p.BeginOutputReadLine();
  385. p.WaitForExit();
  386. return cmdOut;
  387. }
  388.  
  389. /////////////////////////////////////////////////// EXECUTE A LIST COMMAND
  390. private static List<string> ExecuteCommand(List<string> cmdOut, ProcessStartInfo startInfo)
  391. {
  392. Process p = Process.Start(startInfo);
  393. p.OutputDataReceived += (x, y) => cmdOut.Add(y.Data);
  394. p.BeginOutputReadLine();
  395. p.WaitForExit();
  396. return cmdOut;
  397. }
  398.  
  399. private static SecureString pw()
  400. {
  401.  
  402. SecureString ss = new SecureString();
  403.  
  404. ss.AppendChar('p');
  405. ss.AppendChar('a');
  406. ss.AppendChar('s');
  407. ss.AppendChar('s');
  408. ss.AppendChar('w');
  409. ss.AppendChar('o');
  410. ss.AppendChar('r');
  411. ss.AppendChar('d');
  412. ss.AppendChar('1');
  413. ss.AppendChar('2');
  414. ss.AppendChar('3');
  415.  
  416. return ss;
  417. }
  418. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement