잊지 않겠습니다.

WMI Execute example

.NET Framework 2010. 8. 25. 15:26
WMI query를 이용해서 remote computer에 대한 system 작업을 할 때, .net으로 하는 방법 간단 예제.


try
{
    ConnectionOptions connectionOptions = new ConnectionOptions()
                                                {
                                                    Impersonation = ImpersonationLevel.Impersonate,
                                                    Username =
                                                        string.Format("{0}\\{1}", txDomain.Text,
                                                                    txUserName.Text),
                                                    Password = txPassword.Text,
                                                    Authentication = AuthenticationLevel.Default,
                                                    EnablePrivileges = true
                                                };

    ManagementScope scope = new ManagementScope(txNamespace.Text, connectionOptions);
    ObjectQuery query = new ObjectQuery(txQuery.Text);
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
    ManagementObjectCollection managementObjects = searcher.Get();

    txResult.Text = string.Empty;

    StringBuilder sb = new StringBuilder();
    foreach (var o in managementObjects)
    {
        System.Diagnostics.Debug.WriteLine(o.ToString());
        sb.AppendLine(o.ToString());
    }
    txResult.Text = sb.ToString();
    managementObjects.Dispose();
    searcher.Dispose();
}
catch(Exception ex)
{
    txResult.Text = string.Empty;
    txResult.Text = ex.Message + Environment.NewLine + ex.StackTrace;
}
Posted by Y2K
,