c# - Getting Access Denied issue while using wmi process to restart a process -
i trying restart service remotely finding process id through wmi commands. able process , restart service when use individual id. when use generic id, getting access denied exception. generic id having permanent admin access server. attaching code piece. getting exception @ starting line of foreach
.
public bool process(string processname, string servername, string serveruserid, string serveridpassword,mdmjobmanager mdmjobmanagerobj,string _logfile) { var processname = "java.exe"; // var processtest = "xxxxxx"; var processtest = processname; var connectoptions = new connectionoptions(); connectoptions.username = @"nu\" + serveruserid; connectoptions.password = serveridpassword; string ipaddress = getipaddress(servername); bool status = false; mdmjobmanagerobj.writetologfile("trying connect server :" + servername, _logfile); managementscope scope = new managementscope(@"\\" + ipaddress + @"\root\cimv2", connectoptions); string querystring = "select name, processid, caption, executablepath,commandline" + " win32_process name='" + processname + "'"; // wmi query // var query = new selectquery("select * win32_process name ='" + processname + "'"); var query = new selectquery(querystring); // using (var searcher = new managementobjectsearcher(scope, query)) using (var searcher = new managementobjectsearcher(scope, query)) { foreach (managementobject process in searcher.get()) // fixed line { string command = process["commandline"].tostring(); if (command.contains(processtest)) { mdmjobmanagerobj.writetologfile("the runtime service has been identified in process list " , _logfile); // use processid kill process taskkill objectgetoptions processobjgetopt = new objectgetoptions(); managementpath processpath = new managementpath("win32_process"); managementclass processclass = new managementclass(scope, processpath, processobjgetopt); managementbaseobject processinparams = processclass.getmethodparameters("create"); processinparams["commandline"] = string.format("cmd /c \"taskkill /f /pid {0}\"", process["processid"].tostring()); managementbaseobject outparams = processclass.invokemethod("create", processinparams, null); int returncode = system.convert.toint32(outparams["returnvalue"]); switch (returncode) { case 0: mdmjobmanagerobj.writetologfile("the restart successfull ", _logfile); status = true; break; case 2: mdmjobmanagerobj.writetologfile("the restart failed reason : access denied", _logfile); throw new exception("access denied"); case 3: mdmjobmanagerobj.writetologfile("the restart failed reason :insufficient privilege ", _logfile); throw new exception("insufficient privilege"); case 8: mdmjobmanagerobj.writetologfile("the restart failed reason : unknown failure ", _logfile); throw new exception("unknown failure"); case 9: mdmjobmanagerobj.writetologfile("the restart failed reason : path not found", _logfile); throw new exception("path not found"); case 21: mdmjobmanagerobj.writetologfile("the restart failed reason : invalid parameter", _logfile); throw new exception("invalid parameter"); default: mdmjobmanagerobj.writetologfile("the restart failed reason : terminate failed error code" + returncode.tostring(), _logfile); throw new exception("terminate failed error code " + returncode.tostring()); }
Comments
Post a Comment