WMI Win32_Process
-
Hi I am having small problem in WMI related question. The folowing function will accept username,password and process executable path as input parameter, and it will launch a new process in the host.and finally I will fetch Process ID and process name. This code is working fine for the current user, that means passing username and password as null. My requrement is I wand to run a process for a different user( with username and password) Can any one guide me in this rehgard? void LaunchProcess(string filename,string username,string password) { bool currentUser = false; string processName = ""; uint processID = 0; if( (username != null)&& (password != null)) currentUser = true; // LaunchProcess got username and Password. ConnectionOptions objConnectionOptions = new ConnectionOptions(); if(currentUser ==true) { objConnectionOptions.Username = username; objConnectionOptions.Password = password; } ManagementScope objManagementScope = new ManagementScope("root\\cimv2", objConnectionOptions); objManagementScope.Connect(); ManagementClass processClass = new ManagementClass("Win32_Process"); processClass.Scope = objManagementScope; //Get an input parameters object for this method ManagementBaseObject inParams = processClass.GetMethodParameters("Create"); //Fill in input parameter values inParams["CommandLine"] = filename; // this will execute the command. ManagementBaseObject outParams = processClass.InvokeMethod("Create",inParams, null); // Wait for 1 Second to start the Process System.Threading.Thread.Sleep(1000); // Get the Unique Process ID after Process Creation processID = (uint)outParams["processId"]; //Query based on the received processID and get the Process Name . ObjectQuery objObjectQuery = new ObjectQuery("Select name from Win32_Process Where ProcessID = '" + processID + "'"); ManagementObjectSearcher objMagObjSearcher = new ManagementObjectSearcher( objManagementScope, objObjectQuery ); ManagementObjectCollection objMagObjCollection = objMagObjSearcher.Get(); foreach( ManagementObject objManagementObject in objMagObjCollection ) { if(objManagementObject["name"] != null) processName = objManagementObject["name"].ToString(); } // foreach } }