【问题标题】:WMI Remote connectionWMI 远程连接
【发布时间】:2016-03-26 19:20:34
【问题描述】:

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

错误如下:

RPC 服务器不可用..

【问题讨论】:

    标签: wmi


    【解决方案1】:

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

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

    无论如何,我最近编写了一个 Web 应用程序,它允许用户在 LAN 上查找服务器并杀死那里的目标进程或启动一个新进程。我是在 C# 中完成的,所以下面的代码 sn-p 正是我使用的。它不是最好的,但它现在正在生产中:

    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 
    }
    

    【讨论】:

    • 您确定您的 RDP 评论吗?我希望您需要启用远程管理但不需要完整的 RDP?
    • :) 不错。它的措辞很糟糕。这并不是说我的意思是需要 RDP 才能执行此任务,但考虑到问题的提出方式,我觉得他可能正在尝试连接到一个他永远无法连接的盒子。这似乎是一个很好的衡量标准。实际上,如果您可以 RDP,您应该可以做到这一点。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      您可以通过在目标的命令提示符下运行它来在任何目标机器上启用 RPC 服务器:

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

      至少对我有用。 :)

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-08
          • 2018-04-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多