public bool TerminateProcess(string ServerAddress, UInt32 ProcessID) { // Open WMI Connection to Server if (_Parent.Settings.Username != "") { // Use supplied credentials var ConnectionOpt = new ConnectionOptions() { Username = _Parent.Settings.Username, Password = _Parent.Settings.Password, Impersonation = ImpersonationLevel.Impersonate, EnablePrivileges = true }; ManagementScope Server = new ManagementScope(string.Format(@"\\{0}\root\cimv2", ServerAddress), ConnectionOpt); } else { // Use logged-in authentication ManagementScope Server = new ManagementScope(string.Format(@"\\{0}\root\cimv2", ServerAddress)); } // Find the requested process SelectQuery WMIQuery2 = new SelectQuery(string.Format("SELECT * FROM Win32_Process WHERE ProcessId = {0}", ProcessID)); using (var SearchElem = new ManagementObjectSearcher(Server, WMIQuery2)) { foreach (ManagementObject ProcessData in SearchElem.Get()) { // Verify PID and Terminate Process var PID = Convert.ToUInt32(ProcessData["ProcessId"]); if (PID == ProcessID) { var ReturnCode = (UInt32)ProcessData.InvokeMethod("Terminate", null); return((ReturnCode == 0) ? true : false); } } } return(false); }
public void MonitorLoop() { while (_Running) { try { if (_Parent.ActiveTab != null) { var ServerAddress = _Parent.Settings.Servers.Single(q => (q.Name == _Parent.ActiveTab)).Address; var PerfDict = new Dictionary <UInt32, UInt64[]>(); var Data = new List <ProcessData>(); // Open WMI Connection to Server if (_Parent.Settings.Username != "") { // Use supplied credentials var ConnectionOpt = new ConnectionOptions() { Username = _Parent.Settings.Username, Password = _Parent.Settings.Password, Impersonation = ImpersonationLevel.Impersonate, EnablePrivileges = true }; ManagementScope Server = new ManagementScope(string.Format(@"\\{0}\root\cimv2", ServerAddress), ConnectionOpt); } else { // Use logged-in authentication ManagementScope Server = new ManagementScope(string.Format(@"\\{0}\root\cimv2", ServerAddress)); } // Query Win32_PerfFormattedData on remote server SelectQuery WMIQuery = new SelectQuery("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process"); using (var SearchElem = new ManagementObjectSearcher(Server, WMIQuery)) { foreach (ManagementObject ProcessData in SearchElem.Get()) { var prData = new UInt64[2] { Convert.ToUInt64(ProcessData["PercentProcessorTime"]), Convert.ToUInt64(ProcessData["WorkingSet"]) }; var ProcessID = Convert.ToUInt32(ProcessData["IDProcess"]); if (!PerfDict.ContainsKey(ProcessID)) { PerfDict.Add(ProcessID, prData); } } } // Query Win32_Process on remote server SelectQuery WMIQuery2 = new SelectQuery("SELECT * FROM Win32_Process"); using (var SearchElem = new ManagementObjectSearcher(Server, WMIQuery2)) { foreach (ManagementObject ProcessData in SearchElem.Get()) { var OwnerData = ProcessData.InvokeMethod("GetOwner", null, null); var ProcessID = Convert.ToUInt32(ProcessData["ProcessId"]); var pData = new ProcessData() { Username = string.Format("{0}\\{1}", OwnerData["Domain"], OwnerData["User"]), Description = ProcessData["Description"].ToString(), Name = ProcessData["Name"].ToString(), PID = ProcessID, Memory = "", CPU = 0 }; if (pData.Name == "System Idle Process" || pData.Name == "System") { continue; } if (PerfDict.ContainsKey(ProcessID)) { // Add Performance Data to Process Data class var PerfData = PerfDict[ProcessID]; pData.Memory = _FormatBytes((long)PerfData[1]); pData.CPU = PerfData[0]; } // Add process data to List Data.Add(pData); } } // Send event to update GUI ProgressUpdate(this, Data); } // Wait for next update interval _Sleeper.WaitOne(TimeSpan.FromSeconds(_Parent.Settings.UpdateInterval)); } catch (Exception Ex) { if (Ex.Message.Contains("found") || Ex.Message.Contains("no matching element")) { continue; // Thanks, WMI } MessageBox.Show(string.Format("Remote Management Error:\n{0}", Ex.Message), "Error", MessageBoxButton.OK, MessageBoxImage.Error); break; // Break out of running loop } } }