zoukankan      html  css  js  c++  java
  • WMI

    http://singlepine.cnblogs.com/articles/299457.html

    http://www.cnblogs.com/haiq/archive/2011/01/14/1935377.html

     http://msdn.microsoft.com/zh-cn/library/ms257337(v=vs.80).aspx

    public class WMICmdProcessor
        {
            private const int PollingDelay = 15000;
            internal const string DefaultUsername = "...";
            internal const string DefaultPassword = "...";
    
            uint processId;
            string server;
            string password;
            string userName;
            string command;
    
            public uint ExitCode { get; set; }
            public bool ProcessExited { get; set; }
    
            public WMICmdProcessor(string server, string command, string userName = DefaultUsername, string password = DefaultPassword)
            {
                this.server = server;
                this.userName = userName;
                this.password = password;
    
                if (command == null)
                {
                    throw new ArgumentNullException("command");
                }
    
                this.command = command.Replace("%SystemDrive%", this.SystemDrive);
            }
    
            string systemDrive;
            public string SystemDrive
            {
                get
                {
                    if (systemDrive == null)
                    {
                        ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_OperatingSystem");
                        foreach (ManagementObject queryObj in searcher.Get())
                        {
                            systemDrive = Convert.ToString(queryObj["SystemDrive"]);
                            break;
                        }
                    }
    
                    return systemDrive;
                }
            }
    
            private ManagementScope ManagementScope
            {
                get
                {
                    return new ManagementScope()
                    {
                        Options = new ConnectionOptions()
                        {
                            Username = userName,
                            Password = password
                        },
                        Path = new ManagementPath(@"\" + server + @"
    ootcimv2")
                    };
                }
            }
    
            public UInt32 RunCommand()
            {
                this.ExitCode = 0;
                this.ProcessExited = false;
                Log.Info("Starting command: {0} on remote sever {1}", this.command, this.server);
    
                // verify the parameters:
                if (String.IsNullOrEmpty(server))
                {
                    string message = "You must specify a non-empty server name.";
                    throw new ArgumentNullException("server", message);
                }
                if (String.IsNullOrEmpty(command))
                {
                    string message = "You must specify a non-empty command to run.";
                    throw new ArgumentNullException("command", message);
                }
                if (String.IsNullOrEmpty(userName))
                {
                    string message = "You must specify a non-empty username.";
                    throw new ArgumentNullException("username", message);
                }
                if (String.IsNullOrEmpty(password))
                {
                    string message = "You must specify a non-empty password.";
                    throw new ArgumentNullException("password", message);
                }
    
                Utility.PollCondition pollCondition = new Utility.PollCondition(
                    delegate()
                    {
                        using (ManagementClass managementClass = new ManagementClass("Win32_Process") { Scope = this.ManagementScope })
                        using (ManagementBaseObject input = managementClass.GetMethodParameters("Create"))
                        {
                            input.SetPropertyValue("CommandLine", command);
    
                            using (ManagementBaseObject output = managementClass.InvokeMethod("Create", input, null))
                            {
                                try
                                {
                                    this.processId = Convert.ToUInt32(output.GetPropertyValue("ProcessID").ToString());
                                }
                                catch (Exception)
                                {
                                    Log.Error("Failed to start process");
                                    return false;
                                }
                            }
                        }
    
                        return true;
                    });
    
                // retry 3 times if we don't get process id (process creation failure)
                Utility.PollWait(pollCondition, pollCondition, 3, TimeSpan.FromSeconds(60));
    
                return processId;
            }
    
            public void WaitForProcess()
            {
                WaitForProcess(TimeSpan.FromSeconds(30));
            }
    
            public void WaitForProcess(TimeSpan waitTimeout)
            {
                Log.Info("Waiting for process {0}, command: {1} to finish on remote sever {2}", this.processId, this.command, this.server);
    
                Utility.PollCondition pollCondition = new Utility.PollCondition(
                    delegate()
                    {
                        if (this.ProcessExists())
                        {
                            return false;
                        }
                        else
                        {
                            return true;
                        }
                    });
                Utility.PollWait(pollCondition, TimeSpan.FromSeconds(30), waitTimeout);
    
                Log.Info("Process id: " + processId + " exits on remote sever " + server + ".");
            }
    
            public bool ProcessExists()
            {
                string query = "SELECT * FROM WIN32_Process WHERE ProcessID=" + processId;
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(ManagementScope, new SelectQuery(query));
                ManagementObjectCollection processes = searcher.Get();
    
                return (processId != 0 && processes != null && processes.Count > 0);
            }
    
        }
  • 相关阅读:
    2-python数据分析-基于pandas的数据清洗、DataFrame的级联与合并操作
    1-2-python数据分析-DataFrame基础操作巩固-股票分析、双均线策略制定
    1-python数据分析-数据分析三剑客之Pandas基础操作
    单例模式
    装饰器模式
    适配器模式
    es通用工具类ElasticSearchUtil
    ELK+RabbitMq搭建日志系统
    redis能保证数据100%不丢失吗?
    面试官问你redis是单线程还是多线程该怎么回答?
  • 原文地址:https://www.cnblogs.com/Jessy/p/3538783.html
Copyright © 2011-2022 走看看