WMI远程连接

时间:2009-01-28 15:29:25

标签: wmi

我有一个关于通过asp.net从计算机A(Windows 2003服务器)到计算机B(Windows XP)的WMI连接问题。

错误如下:

  

RPC服务器不可用..

4 个答案:

答案 0 :(得分:3)

为了成功利用WMI连接,您必须执行几个步骤。基本的是你必须允许在目标盒上进行远程管理。如果你不能进入RDP,很可能,你不能远程管理任何其他东西。这也可能包括Windows防火墙问题。确保您的请求甚至可以进入。

接下来,开始简单。你甚至可以在那个盒子上查看正在运行的进程吗?尝试使用System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetProcesses(“machine-name”)输出目标框上的所有正在运行的进程。如果你至少可以在盒子上获得一些信息,那么你得到的RPC消息可能与传入的错误参数有关吗?

无论如何,我最近编写了一个Web应用程序,允许用户在局域网中找到服务器并在那里终止目标进程或启动一个新进程。我是用C#做的,所以下面的代码片段就是我用过的代码片段。它不是最好的,但它现在在生产中工作:

public static class RemoteProcessAccess
{

    public static void KillProcessByProcessID(string NameOfServer, string DomainName, string LogIn, string Password, int processID)
    {
        //#1 The vars for this static method
        #region /// <variables> ...
            string userName;
            string password;
            string machineName;
            string myDomain;
            Hashtable hs = new Hashtable();
            ManagementScope mScope;
            ConnectionOptions cnOptions;
            ManagementObjectSearcher objSearcher;
            ManagementOperationObserver opsObserver;
            ManagementClass manageClass;
            DirectoryEntry entry;
            DirectorySearcher searcher;
            DirectorySearcher userSearcher;
        #endregion

        //#2 Set the basics sent into the method
            machineName = NameOfServer;
            myDomain = DomainName;
            userName = LogIn;
            password = Password;

            cnOptions = new ConnectionOptions();
            cnOptions.Impersonation = ImpersonationLevel.Impersonate;
            cnOptions.EnablePrivileges = true;
            cnOptions.Username = myDomain + "\\" + userName;
            cnOptions.Password = password;

            mScope = new ManagementScope(@"\\" + machineName + @"\ROOT\CIMV2", cnOptions);

        //#3 Begin Connection to Remote Box
            mScope.Connect();
            objSearcher = new ManagementObjectSearcher(String.Format("Select * from Win32_Process Where ProcessID = {0}", processID)); 
            opsObserver = new ManagementOperationObserver();
            objSearcher.Scope = mScope;
            string[] sep = { "\n", "\t" };

        //#4 Loop through 
        foreach (ManagementObject obj in objSearcher.Get())
        {
            string caption = obj.GetText(TextFormat.Mof);
            string[] split = caption.Split(sep, StringSplitOptions.RemoveEmptyEntries);

            // Iterate through the splitter
            for (int i = 0; i < split.Length; i++)
            {
                if (split[i].Split('=').Length > 1)
                {
                    string[] procDetails = split[i].Split('=');
                    procDetails[1] = procDetails[1].Replace(@"""", "");
                    procDetails[1] = procDetails[1].Replace(';', ' ');
                    switch (procDetails[0].Trim().ToLower())
                    {
                        //You could look for any of the properties here and do something else,
                        case "processid":
                            int tmpProc = Convert.ToInt32(procDetails[1].ToString());
                            //if the process id equals the one passed in....
                            //(this is redundant since we should have limited the return 
                            //by the query where above, but we're paranoid here

                            if (tmpProc.Equals(processID))
                            {
                                obj.InvokeMethod(opsObserver, "Terminate", null);                                    
                            }
                            break;

                    }//end process ID switch...

                }//end our if statement...

            }//end our for loop...

        }//end our for each loop...



    }//end static method 
}

答案 1 :(得分:1)

查看KB875605(“如何解决Windows XP SP2中与WMI相关的问题”)

答案 2 :(得分:1)

您可以在目标的命令提示符上运行此服务器,从而在任何目标计算机上启用RPC服务器:

[/代码] netsh防火墙设置服务RemoteAdmin [/代码]

至少为我工作。 :)

答案 3 :(得分:0)

尝试使用wmic命令行从远程计算机获取信息,也可以安装Services+的代码并尝试连接和调试与服务器的连接,很可能是防火墙问题或RPC服务已关闭或已停用。