我有一个关于通过 asp.net 从计算机 A(Windows 2003 服务器)到计算机 B(Windows XP)的 WMI 连接的问题。
错误如下:
RPC 服务器不可用..
为了成功利用 WMI 连接,您必须执行几个步骤。基础是您当然必须允许对目标框进行远程管理。如果你不能 RDP 进入它,很可能你不能远程管理其他任何东西。这也可能包括 Windows 防火墙问题。确保您的请求甚至可以进入。
接下来,从简单开始。你甚至可以轮询那个盒子上正在运行的进程吗?尝试使用 System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetProcesses("machine-name") 输出目标框上所有正在运行的进程。如果您至少可以获得有关该框的一些信息,那么您收到的 RPC 消息可能与传入的参数不正确有关,也许?
无论如何,我最近编写了一个 Web 应用程序,它允许用户在 LAN 上查找服务器并杀死那里的目标进程或启动一个新进程。我是在 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
}
查看KB875605(“如何解决 Windows XP SP2 中与 WMI 相关的问题”)
您可以通过在目标的命令提示符下运行它来在任何目标机器上启用 RPC 服务器:
[/code] netsh 防火墙设置服务 RemoteAdmin [/code]
至少为我工作。:)
尝试使用 wmic 命令行从远程计算机获取信息,也可以安装Services+的代码并尝试连接和调试与服务器的连接,很可能是防火墙问题或 RPC 服务关闭或禁用。