Guest User

Untitled

a guest
Aug 28th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. What Windows Class to use when I want to start a process remotely
  2. static public String RemoteConnect()
  3. {
  4. try
  5. {
  6. ConnectionOptions conn = new ConnectionOptions();
  7. conn.Username = @"JV";
  8. conn.Password = @"Nazpal6180";
  9. conn.EnablePrivileges = true;
  10. conn.Impersonation = System.Management.ImpersonationLevel.Impersonate;
  11. ManagementScope scope = new ManagementScope("\\phsd194-JV\root\cimv2", conn);
  12. //scope.Options.Impersonation = System.Management.ImpersonationLevel.Impersonate;
  13. //scope.Options.EnablePrivileges = true;
  14. scope.Connect();
  15.  
  16. ManagementPath managementPath = new ManagementPath("Win32_ScheduledJob");
  17.  
  18. ObjectGetOptions objectGetOptions = new ObjectGetOptions();
  19. ManagementClass classInstance = new ManagementClass(scope, managementPath, objectGetOptions);
  20.  
  21. object[] objectsIn = new object[7];
  22. objectsIn[0] = "calc.exe";
  23. objectsIn[1] = "********140000.000000+480";
  24. objectsIn[5] = true;
  25. object outParams = classInstance.InvokeMethod("Create", objectsIn);
  26. String response = "Creation of the process returned: " + outParams;
  27.  
  28. return response;
  29. }
  30. catch (ManagementException err)
  31. {
  32. String response = "An error occurred while trying to execute the WMI method: " + err.Message;
  33. //Console.WriteLine("An error occurred while trying to execute the WMI method: " + err.Message);
  34. return response;
  35. }
  36. }
  37.  
  38. using System;
  39. using System.Collections.Generic;
  40. using System.Text;
  41. using System.Management;
  42.  
  43. namespace ConsoleApplication11
  44. {
  45. class Program
  46. {
  47.  
  48. private static string DateTimetoUTC(DateTime dateParam)
  49. {
  50. string buffer = dateParam.ToString("********HHmmss.ffffff");
  51. TimeSpan tickOffset = TimeZone.CurrentTimeZone.GetUtcOffset(dateParam);
  52. buffer += (tickOffset.Ticks >= 0) ? '+' : '-';
  53. buffer += (Math.Abs(tickOffset.Ticks) / System.TimeSpan.TicksPerMinute).ToString("d3");
  54. return buffer;
  55. }
  56.  
  57. static void Main(string[] args)
  58. {
  59. try
  60. {
  61. ConnectionOptions conn = new ConnectionOptions();
  62. conn.Username = "theusername";
  63. conn.Password = "password";
  64. //connectoptions.Authority = "ntlmdomain:";
  65. conn.EnablePrivileges = true;
  66. ManagementScope scope = new ManagementScope(@"\192.168.52.128rootcimv2", conn);
  67. scope.Connect();
  68. Console.WriteLine("Connected");
  69.  
  70. ObjectGetOptions objectGetOptions = new ObjectGetOptions();
  71. ManagementPath managementPath = new ManagementPath("Win32_ScheduledJob");
  72. ManagementClass classInstance = new ManagementClass(scope, managementPath, objectGetOptions);
  73.  
  74. ManagementBaseObject inParams = classInstance.GetMethodParameters("Create");
  75. inParams["Command"] = @"notepad.exe";
  76. //the itme must be in UTC format
  77. string StartTime = DateTimetoUTC(DateTime.Now.AddMinutes(1));
  78. Console.WriteLine(StartTime);
  79. inParams["StartTime"] = StartTime;
  80.  
  81. ManagementBaseObject outParams = classInstance.InvokeMethod("Create", inParams, null);
  82. Console.WriteLine("JobId: " + outParams["JobId"]);
  83. Console.ReadKey();
  84. }
  85. catch(ManagementException err)
  86. {
  87. Console.WriteLine("An error occurred while trying to execute the WMI method: " + err.Message);
  88. Console.ReadKey();
  89. }
  90. }
  91. }
  92. }
Add Comment
Please, Sign In to add comment