Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- C#
- 설계프로세스
- Gundam
- diskpart
- hadoop
- 지리산둘레길
- MindMap
- Xcode
- MindManager
- 글모음
- BCP
- T-SQL
- 프라모델
- Windows 7
- redmine
- 산출물
- ERWIN
- SQL
- ClickOnce
- 프로젝트관리
- hive
- .net
- WinForm
- AD
- bitnami
- garbage collection
- 일상
- Flume
- 소프트웨어공학
- union
Archives
- Today
- Total
Blue sky, wind, cloud and knulf
원격 시스템의 프로세스 쥑이기.... 본문
C#을 통해서 원격이나 로컬 시스템의 프로세스를 쥑여 버릴 수 있다. 뭐 아래 내용은 아주 심플하다. 대상시스템의 WMI객체에 접속해서 쿼리를 던지고, 해당 결과를 받아서 InvokeMethod를 통해서 각 프로세스를 쥑인다.
1. 도메인 인증 등을 통해 인증이 되어 있는 경우
/// <summary>
/// Terminate process on local or remote system
/// </summary>
/// <param name="serverName">Server name on local or remote</param>
/// <param name="processName">Process name</param>
private static void KillProcess(string serverName, string processName)
{
try
{
if (processName.IndexOf(".exe") > 0)
{
processName = processName.Substring(0, processName.IndexOf(".exe") - 1);
}
// ManagementScope
ManagementScope scope = new ManagementScope("\\\\" + serverName + "\\root\\cimv2");
// Query
ObjectQuery query = new ObjectQuery("select * from Win32_Process where Name = '" + processName + ".exe'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
// Get object
ManagementObjectCollection objCollection = searcher.Get();
foreach (ManagementObject obj in objCollection)
{
obj.InvokeMethod("Terminate", null);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
2. 인증이 되지 않아서 접속정보를 넣어야 하는 경우.
/// <summary>
/// Terminate process on local or remote system
/// </summary>
/// <param name="serverName">Server name on local or remote</param>
/// <param name="processName">Process name</param>
/// <param name="userName">user name for remote system</param>
/// <param name="userPasswd">Password for user</param>
private static void KillProcess(string serverName, string processName, string userName, string userPasswd)
{
try
{
if (processName.IndexOf(".exe") > 0)
{
processName = processName.Substring(0, processName.IndexOf(".exe") - 1);
}
ConnectionOptions opts = new ConnectionOptions();
opts.Username = userName;
opts.Password = userPasswd;
// ManagementScope
ManagementScope scope = new ManagementScope("\\\\" + serverName + "\\root\\cimv2", opts);
scope.Connect();
// Query
ObjectQuery query = new ObjectQuery("select * from Win32_Process where Name = '" + processName + ".exe'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
// Get object
ManagementObjectCollection objCollection = searcher.Get();
foreach (ManagementObject obj in objCollection)
{
obj.InvokeMethod("Terminate", null);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
Comments