Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- What Windows Class to use when I want to start a process remotely
- static public String RemoteConnect()
- {
- try
- {
- ConnectionOptions conn = new ConnectionOptions();
- conn.Username = @"JV";
- conn.Password = @"Nazpal6180";
- conn.EnablePrivileges = true;
- conn.Impersonation = System.Management.ImpersonationLevel.Impersonate;
- ManagementScope scope = new ManagementScope("\\phsd194-JV\root\cimv2", conn);
- //scope.Options.Impersonation = System.Management.ImpersonationLevel.Impersonate;
- //scope.Options.EnablePrivileges = true;
- scope.Connect();
- ManagementPath managementPath = new ManagementPath("Win32_ScheduledJob");
- ObjectGetOptions objectGetOptions = new ObjectGetOptions();
- ManagementClass classInstance = new ManagementClass(scope, managementPath, objectGetOptions);
- object[] objectsIn = new object[7];
- objectsIn[0] = "calc.exe";
- objectsIn[1] = "********140000.000000+480";
- objectsIn[5] = true;
- object outParams = classInstance.InvokeMethod("Create", objectsIn);
- String response = "Creation of the process returned: " + outParams;
- return response;
- }
- catch (ManagementException err)
- {
- String response = "An error occurred while trying to execute the WMI method: " + err.Message;
- //Console.WriteLine("An error occurred while trying to execute the WMI method: " + err.Message);
- return response;
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Management;
- namespace ConsoleApplication11
- {
- class Program
- {
- private static string DateTimetoUTC(DateTime dateParam)
- {
- string buffer = dateParam.ToString("********HHmmss.ffffff");
- TimeSpan tickOffset = TimeZone.CurrentTimeZone.GetUtcOffset(dateParam);
- buffer += (tickOffset.Ticks >= 0) ? '+' : '-';
- buffer += (Math.Abs(tickOffset.Ticks) / System.TimeSpan.TicksPerMinute).ToString("d3");
- return buffer;
- }
- static void Main(string[] args)
- {
- try
- {
- ConnectionOptions conn = new ConnectionOptions();
- conn.Username = "theusername";
- conn.Password = "password";
- //connectoptions.Authority = "ntlmdomain:";
- conn.EnablePrivileges = true;
- ManagementScope scope = new ManagementScope(@"\192.168.52.128rootcimv2", conn);
- scope.Connect();
- Console.WriteLine("Connected");
- ObjectGetOptions objectGetOptions = new ObjectGetOptions();
- ManagementPath managementPath = new ManagementPath("Win32_ScheduledJob");
- ManagementClass classInstance = new ManagementClass(scope, managementPath, objectGetOptions);
- ManagementBaseObject inParams = classInstance.GetMethodParameters("Create");
- inParams["Command"] = @"notepad.exe";
- //the itme must be in UTC format
- string StartTime = DateTimetoUTC(DateTime.Now.AddMinutes(1));
- Console.WriteLine(StartTime);
- inParams["StartTime"] = StartTime;
- ManagementBaseObject outParams = classInstance.InvokeMethod("Create", inParams, null);
- Console.WriteLine("JobId: " + outParams["JobId"]);
- Console.ReadKey();
- }
- catch(ManagementException err)
- {
- Console.WriteLine("An error occurred while trying to execute the WMI method: " + err.Message);
- Console.ReadKey();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment