Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Cassia;
  4. using Microsoft.Win32;
  5.  
  6. namespace SessionInfo
  7. {
  8. internal class Program
  9. {
  10. private static readonly ITerminalServicesManager _manager = new TerminalServicesManager();
  11.  
  12. private static void Main(string[] args)
  13. {
  14. try
  15. {
  16. if (args.Length == 0)
  17. {
  18. ShowCurrentSession();
  19. return;
  20. }
  21. switch (args[0].ToLower())
  22. {
  23. case "waitforevents":
  24. WaitForEvents();
  25. return;
  26. case "current":
  27. ShowCurrentSession();
  28. return;
  29. case "console":
  30. ShowActiveConsoleSession();
  31. return;
  32. case "get":
  33. GetSessionInfo(args);
  34. return;
  35. case "listservers":
  36. ListServers(args);
  37. return;
  38. case "listsessions":
  39. ListSessions(args);
  40. return;
  41. case "listsessionsinfo":
  42. ListSessionsInfo(args);
  43. return;
  44. case "listsessionprocesses":
  45. ListSessionProcesses(args);
  46. return;
  47. case "listprocesses":
  48. ListProcesses(args);
  49. return;
  50. case "killprocess":
  51. KillProcess(args);
  52. return;
  53. case "logoff":
  54. LogoffSession(args);
  55. return;
  56. case "disconnect":
  57. DisconnectSession(args);
  58. return;
  59. case "message":
  60. SendMessage(args);
  61. return;
  62. case "ask":
  63. AskQuestion(args);
  64. return;
  65. case "shutdown":
  66. Shutdown(args);
  67. return;
  68. case "startremotecontrol":
  69. StartRemoteControl(args);
  70. return;
  71. case "stopremotecontrol":
  72. StopRemoteControl(args);
  73. return;
  74. case "connect":
  75. Connect(args);
  76. return;
  77. }
  78. Console.WriteLine("Unknown command: " + args[0]);
  79. }
  80. catch (Exception ex)
  81. {
  82. Console.WriteLine(ex);
  83. Console.ReadLine();
  84. }
  85. }
  86.  
  87. private static void Connect(string[] args)
  88. {
  89. if (args.Length < 4)
  90. {
  91. Console.WriteLine("Usage: SessionInfo connect [source session id] [target session id] [password]");
  92. return;
  93. }
  94. using (var server = _manager.GetLocalServer())
  95. {
  96. server.Open();
  97. var source = server.GetSession(int.Parse(args[1]));
  98. var target = server.GetSession(int.Parse(args[2]));
  99. var password = args[3];
  100. source.Connect(target, password, true);
  101. }
  102. }
  103.  
  104. private static void StopRemoteControl(string[] args)
  105. {
  106. if (args.Length < 2)
  107. {
  108. Console.WriteLine("Usage: SessionInfo stopremotecontrol [session id]");
  109. return;
  110. }
  111. using (var server = _manager.GetLocalServer())
  112. {
  113. server.Open();
  114. var session = server.GetSession(int.Parse(args[1]));
  115. session.StopRemoteControl();
  116. }
  117. }
  118.  
  119. private static void StartRemoteControl(string[] args)
  120. {
  121. if (args.Length < 5)
  122. {
  123. Console.WriteLine("Usage: SessionInfo startremotecontrol [server] [session id] [modifier] [hotkey]");
  124. return;
  125. }
  126. using (var server = GetServerFromName(args[1]))
  127. {
  128. server.Open();
  129. var session = server.GetSession(int.Parse(args[2]));
  130. var modifier =
  131. (RemoteControlHotkeyModifiers)
  132. Enum.Parse(typeof(RemoteControlHotkeyModifiers), args[3].Replace('+', ','), true);
  133. var hotkey = (ConsoleKey) Enum.Parse(typeof(ConsoleKey), args[4], true);
  134. session.StartRemoteControl(hotkey, modifier);
  135. }
  136. }
  137.  
  138. private static void ShowActiveConsoleSession()
  139. {
  140. Console.WriteLine("Active console session:");
  141. WriteSessionInfo(_manager.ActiveConsoleSession);
  142. }
  143.  
  144. private static void WaitForEvents()
  145. {
  146. Console.WriteLine("Waiting for events; press Enter to exit.");
  147. SystemEvents.SessionSwitch +=
  148. delegate(object sender, SessionSwitchEventArgs args) { Console.WriteLine(args.Reason); };
  149. Console.ReadLine();
  150. }
  151.  
  152. private static void Shutdown(string[] args)
  153. {
  154. if (args.Length < 3)
  155. {
  156. Console.WriteLine("Usage: SessionInfo shutdown [server] [shutdown type]");
  157. return;
  158. }
  159. using (var server = GetServerFromName(args[1]))
  160. {
  161. server.Open();
  162. var type = (ShutdownType) Enum.Parse(typeof(ShutdownType), args[2], true);
  163. server.Shutdown(type);
  164. }
  165. }
  166.  
  167. private static void KillProcess(string[] args)
  168. {
  169. if (args.Length < 4)
  170. {
  171. Console.WriteLine("Usage: SessionInfo killprocess [server] [process id] [exit code]");
  172. return;
  173. }
  174. var processId = int.Parse(args[2]);
  175. var exitCode = int.Parse(args[3]);
  176. using (var server = GetServerFromName(args[1]))
  177. {
  178. server.Open();
  179. var process = server.GetProcess(processId);
  180. process.Kill(exitCode);
  181. }
  182. }
  183.  
  184. private static void ListProcesses(string[] args)
  185. {
  186. if (args.Length < 2)
  187. {
  188. Console.WriteLine("Usage: SessionInfo listprocesses [server]");
  189. return;
  190. }
  191. using (var server = GetServerFromName(args[1]))
  192. {
  193. server.Open();
  194. WriteProcesses(server.GetProcesses());
  195. }
  196. }
  197.  
  198. private static void WriteProcesses(IEnumerable<ITerminalServicesProcess> processes)
  199. {
  200. foreach (ITerminalServicesProcess process in processes)
  201. {
  202. WriteProcessInfo(process);
  203. }
  204. }
  205.  
  206. private static void WriteProcessInfo(ITerminalServicesProcess process)
  207. {
  208. Console.WriteLine("Session ID: " + process.SessionId);
  209. Console.WriteLine("Process ID: " + process.ProcessId);
  210. Console.WriteLine("Process Name: " + process.ProcessName);
  211. Console.WriteLine("SID: " + process.SecurityIdentifier);
  212. Console.WriteLine("Working Set: " + process.UnderlyingProcess.WorkingSet64);
  213. }
  214.  
  215. private static void ListServers(string[] args)
  216. {
  217. var domainName = (args.Length > 1 ? args[1] : null);
  218. foreach (ITerminalServer server in _manager.GetServers(domainName))
  219. {
  220. Console.WriteLine(server.ServerName);
  221. }
  222. }
  223.  
  224. private static void AskQuestion(string[] args)
  225. {
  226. if (args.Length < 8)
  227. {
  228. Console.WriteLine(
  229. "Usage: SessionInfo ask [server] [session id] [icon] [caption] [text] [timeout] [buttons]");
  230. return;
  231. }
  232. var seconds = int.Parse(args[6]);
  233. var sessionId = int.Parse(args[2]);
  234. using (var server = GetServerFromName(args[1]))
  235. {
  236. server.Open();
  237. var session = server.GetSession(sessionId);
  238. var icon = (RemoteMessageBoxIcon) Enum.Parse(typeof(RemoteMessageBoxIcon), args[3], true);
  239. var buttons = (RemoteMessageBoxButtons) Enum.Parse(typeof(RemoteMessageBoxButtons), args[7], true);
  240. var result = session.MessageBox(args[5], args[4], buttons, icon, default(RemoteMessageBoxDefaultButton),
  241. default(RemoteMessageBoxOptions), TimeSpan.FromSeconds(seconds), true);
  242. Console.WriteLine("Response: " + result);
  243. }
  244. }
  245.  
  246. private static void SendMessage(string[] args)
  247. {
  248. if (args.Length < 6)
  249. {
  250. Console.WriteLine("Usage: SessionInfo message [server] [session id] [icon] [caption] [text]");
  251. return;
  252. }
  253. var sessionId = int.Parse(args[2]);
  254. using (var server = GetServerFromName(args[1]))
  255. {
  256. server.Open();
  257. var session = server.GetSession(sessionId);
  258. var icon = (RemoteMessageBoxIcon) Enum.Parse(typeof(RemoteMessageBoxIcon), args[3], true);
  259. session.MessageBox(args[5], args[4], icon);
  260. }
  261. }
  262.  
  263. private static void GetSessionInfo(string[] args)
  264. {
  265. if (args.Length < 3)
  266. {
  267. Console.WriteLine("Usage: SessionInfo get [server] [session id]");
  268. return;
  269. }
  270. var sessionId = int.Parse(args[2]);
  271. using (var server = GetServerFromName(args[1]))
  272. {
  273. server.Open();
  274. WriteSessionInfo(server.GetSession(sessionId));
  275. }
  276. }
  277.  
  278. private static void ListSessionProcesses(string[] args)
  279. {
  280. if (args.Length < 3)
  281. {
  282. Console.WriteLine("Usage: SessionInfo listsessionprocesses [server] [session id]");
  283. return;
  284. }
  285. var sessionId = int.Parse(args[2]);
  286. using (var server = GetServerFromName(args[1]))
  287. {
  288. server.Open();
  289. var session = server.GetSession(sessionId);
  290. WriteProcesses(session.GetProcesses());
  291. }
  292. }
  293.  
  294. private static void ListSessions(string[] args)
  295. {
  296. if (args.Length < 2)
  297. {
  298. Console.WriteLine("Usage: SessionInfo listsessions [server]");
  299. return;
  300. }
  301. using (var server = GetServerFromName(args[1]))
  302. {
  303. server.Open();
  304. foreach (ITerminalServicesSession session in server.GetSessions())
  305. {
  306. WriteSessionInfo(session);
  307. //WriteProcesses(session.GetProcesses());
  308. }
  309. }
  310.  
  311. Console.ReadLine();
  312. }
  313.  
  314. private static void ListSessionsInfo(string[] args)
  315. {
  316. if (args.Length < 2)
  317. {
  318. Console.WriteLine("Usage: SessionInfo listsessionsinfo [server]");
  319. return;
  320. }
  321.  
  322. try
  323. {
  324. using (var server = GetServerFromName(args[1]))
  325. {
  326. server.Open();
  327. foreach (ITerminalServicesSession session in server.GetSessions())
  328. {
  329. if (session.SessionId > 0 && session.SessionId != 65536)
  330. {
  331. WriteSessionInfo(session);
  332. }
  333. //WriteProcesses(session.GetProcesses());
  334. }
  335. }
  336. }
  337. catch(Exception ex)
  338. {
  339. Console.WriteLine(ex.Message.ToString());
  340. }
  341. Console.ReadLine();
  342. }
  343.  
  344. private static void ShowCurrentSession()
  345. {
  346. Console.WriteLine("Current session:");
  347. WriteSessionInfo(_manager.CurrentSession);
  348. }
  349.  
  350. private static void LogoffSession(string[] args)
  351. {
  352. if (args.Length < 3)
  353. {
  354. Console.WriteLine("Usage: SessionInfo logoff [server] [session id]");
  355. return;
  356. }
  357. var serverName = args[1];
  358. var sessionId = int.Parse(args[2]);
  359. using (var server = GetServerFromName(serverName))
  360. {
  361. server.Open();
  362. var session = server.GetSession(sessionId);
  363. session.Logoff();
  364. }
  365. }
  366.  
  367. private static ITerminalServer GetServerFromName(string serverName)
  368. {
  369. return (serverName.Equals("local", StringComparison.InvariantCultureIgnoreCase)
  370. ? _manager.GetLocalServer()
  371. : _manager.GetRemoteServer(serverName));
  372. }
  373.  
  374. private static void DisconnectSession(string[] args)
  375. {
  376. if (args.Length < 3)
  377. {
  378. Console.WriteLine("Usage: SessionInfo disconnect [server] [session id]");
  379. return;
  380. }
  381. var serverName = args[1];
  382. var sessionId = int.Parse(args[2]);
  383. using (var server = GetServerFromName(serverName))
  384. {
  385. server.Open();
  386. var session = server.GetSession(sessionId);
  387. session.Disconnect();
  388. }
  389. }
  390.  
  391. private static void WriteSessionInfo(ITerminalServicesSession session)
  392. {
  393. if (session == null)
  394. {
  395. return;
  396. }
  397. try {
  398. Console.WriteLine("Session ID: " + session.SessionId);
  399. if (session.UserAccount != null)
  400. {
  401. Console.WriteLine("User: " + session.UserAccount);
  402. }
  403. if (session.ClientIPAddress != null)
  404. {
  405. Console.WriteLine("IP Address: " + session.ClientIPAddress);
  406. }
  407. /*
  408. if (session.RemoteEndPoint != null)
  409. {
  410. Console.WriteLine("Remote endpoint: " + session.RemoteEndPoint);
  411. }
  412. */
  413. Console.WriteLine("Window Station: " + session.WindowStationName);
  414. Console.WriteLine("Client Directory: " + session.ClientDirectory);
  415. Console.WriteLine("Client Build Number: " + session.ClientBuildNumber);
  416. Console.WriteLine("Client Hardware ID: " + session.ClientHardwareId);
  417. Console.WriteLine("Client Product ID: " + session.ClientProductId);
  418. Console.WriteLine("Client Protocol Type: " + session.ClientProtocolType);
  419. if (session.ClientProtocolType != ClientProtocolType.Console)
  420. {
  421. // These properties often throw exceptions for the console session.
  422. Console.WriteLine("Application Name: " + session.ApplicationName);
  423. Console.WriteLine("Initial Program: " + session.InitialProgram);
  424. Console.WriteLine("Initial Working Directory: " + session.WorkingDirectory);
  425. }
  426. Console.WriteLine("State: " + session.ConnectionState);
  427. Console.WriteLine("Connect Time: " + session.ConnectTime);
  428. Console.WriteLine("Logon Time: " + session.LoginTime);
  429. Console.WriteLine("Last Input Time: " + session.LastInputTime);
  430. Console.WriteLine("Idle Time: " + session.IdleTime);
  431. Console.WriteLine(string.Format("Client Display: {0}x{1} with {2} bits per pixel",
  432. session.ClientDisplay.HorizontalResolution,
  433. session.ClientDisplay.VerticalResolution, session.ClientDisplay.BitsPerPixel));
  434. if (session.IncomingStatistics != null)
  435. {
  436. Console.Write("Incoming protocol statistics: ");
  437. WriteProtocolStatistics(session.IncomingStatistics);
  438. Console.WriteLine();
  439. }
  440. if (session.OutgoingStatistics != null)
  441. {
  442. Console.Write("Outgoing protocol statistics: ");
  443. WriteProtocolStatistics(session.OutgoingStatistics);
  444. Console.WriteLine();
  445. }
  446. Console.WriteLine();
  447. }
  448. catch(Exception ex)
  449. {
  450. Console.WriteLine(ex.Message.ToString());
  451. }
  452. }
  453.  
  454. private static void WriteProtocolStatistics(IProtocolStatistics statistics)
  455. {
  456. Console.Write("Bytes: " + statistics.Bytes + " Frames: " + statistics.Frames + " Compressed: " +
  457. statistics.CompressedBytes);
  458. }
  459. }
  460. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement