使用 WMI 和 C# 连接到远程计算机

发布于 2024-10-26 13:58:52 字数 781 浏览 1 评论 0原文

嘿...我正在尝试使用 WMI 和 C# 连接到远程计算机。 我收到错误:RPC 服务器不可用。 (异常结果 HRESULT:0x800706BA)。 我不知道这是否与代码相关,所以这就是我正在使用的:

serverN = InputText.Text;//serverN=IPAddress
userN = userName.Text;
passN = passName.Text;
if (String.IsNullOrEmpty(serverN))
                serverN = ".";
ManagementClass manC = new ManagementClass("Win32_LogicalDisk");
string strScope = string.Format(@"\\{0}\root\cimv2", serverN);
ConnectionOptions conOpt = new ConnectionOptions();
conOpt.Username = userN;
conOpt.Password = passN;
manC.Scope = new ManagementScope(strScope, conOpt);

当我尝试从 manC 获取实例时,我捕获了 RPC 不可用的异常。 在本地它可以工作,所以我猜测我必须在远程计算机(操作系统:Windows XP sp2)上进行一些设置。 我已检查它是否允许远程连接,并且已将命令 netshfirewall set service RemoteAdmin 插入命令提示符。 我需要设置域名或networkid吗? 还是我还缺少其他东西?

Hy... I am trying to connect to a remote computer using WMI and C#.
I get an error: The RPC server is unavailable. (Exception result HRESULT: 0x800706BA).
I don't know if that is code related so this is what I'm using:

serverN = InputText.Text;//serverN=IPAddress
userN = userName.Text;
passN = passName.Text;
if (String.IsNullOrEmpty(serverN))
                serverN = ".";
ManagementClass manC = new ManagementClass("Win32_LogicalDisk");
string strScope = string.Format(@"\\{0}\root\cimv2", serverN);
ConnectionOptions conOpt = new ConnectionOptions();
conOpt.Username = userN;
conOpt.Password = passN;
manC.Scope = new ManagementScope(strScope, conOpt);

When I try to get instances from manC I catch the exception with the RPC being unavailable.
Locally it works so I'm guessing that I have to make some settings on the remote machine (OS: Windows XP sp2).
I have checked so that it allows remote connections and I have inserted command netsh firewall set service RemoteAdmin into command prompt.
Do I need to set a domain name or a networkid?
Or it is something else I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

听风念你 2024-11-02 13:58:53

我知道这对你来说可能已经晚了,但对于其他可能偶然发现这篇文章的人来说:
如果你确定不是用户权限问题(你知道该文件夹有权限)
检查您为 ConnectionOptions 对象提供的 AuthenticationLevel。我迭代了其中的每一个,对我来说唯一有效的是 PacketPrivacy 选项。

ConnectionOptions conOp = new ConnectionOptions();
conOp.Authentication = System.Management.AuthenticationLevel.PacketPrivacy;

I know it might be late for you, but for others who might stumble on this post:
If you're sure it is not a user rights issue (you know the folder has the permissions)
Check what you're giving as AuthenticationLevel to the ConnectionOptions object. I iterated through every one of them and for me the only that works is the PacketPrivacy option.

ConnectionOptions conOp = new ConnectionOptions();
conOp.Authentication = System.Management.AuthenticationLevel.PacketPrivacy;
庆幸我还是我 2024-11-02 13:58:53

可能需要有关您的设置的更多信息。两台计算机是否在同一个域中?

您可以先测试 WMI 是否正常工作,然后再在 C# 中尝试。从命令提示符处尝试以下操作,看看是否有效。

wmic /node:路径 Win32_逻辑磁盘

wmic 还有一个 /user 选项,您可以使用它来测试您的用户名和密码。

您可以尝试更改 ConnectionOptions。这通常会因环境​​而异,并可能导致连接失败。

Probably need more information on your setup. Are both computers on the same domain?

You could start by testing if the WMI is working before trying it in C#. Try the following from a command prompt to see if that works.

wmic /node: path Win32_logicaldisk

wmic also has a /user option you can use to test with your username and password.

You might try changing the Authentication and Impersonation property on the ConnectionOptions. This often changes from environment to environment and can cause the connection to fail.

无言温柔 2024-11-02 13:58:53

不想显得粗鲁,但是:您确定 InputText.Text 中的值正确吗?获得“RPC 服务器不可用”的一种简单方法是为服务器提供一个错误的名称。在 string.Format 调用之后,可能值得检查调试器中“scope”的值。

顺便说一句,我认为这只是一个拼写错误,您正在创建并初始化一个名为“scope”的字符串,但将一个名为“strScope”的变量传递给 ManagementScope 构造函数?

Not to seem rude but: are you sure the value in InputText.Text is correct? One easy way to get "The RPC server is unavailable" is to supply a bad name for the server. It might be worth examining the value of 'scope' in the debugger after the string.Format call.

BTW I assume it's just a typo that you are creating and initialising a string called 'scope' but passing a variable called 'strScope'to the ManagementScope constructor?

沫雨熙 2024-11-02 13:58:53

听起来像是权限问题恕我直言。根据我的经验,您需要设置一些显式安全权限才能进行远程 WMI 调用。

有一篇 TechNet 文章可能会有所帮助:http://technet .microsoft.com/en-us/library/cc787533%28WS.10%29.aspx 您是否设置了适当的权限?

Sounds like a permissions issue IMHO. From my experience, you need to set up some explicit security permissions to make remote WMI calls.

There's a TechNet article which may help: http://technet.microsoft.com/en-us/library/cc787533%28WS.10%29.aspx have you set the appropriate permissions?

望喜 2024-11-02 13:58:53

该代码可以在本地运行,因为您已经登录。远程 WMI 连接始终需要额外的安全权限。您的第一个代码更新应包括impersonationLevel:
http://msdn.microsoft .com/en-us/library/windows/desktop/aa389288%28v=vs.85%29.aspx

如果 impersonationLevel 不够,那么下一步应该是指定显式凭据。那会起作用的。还有一个选项,但我不推荐它:“授权委托”,它允许这样的帐户冒充域中的任何帐户。

The code works locally because you're already logged on. Remote WMI connections always require additional security rights. Your first code update should include impersonationLevel:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa389288%28v=vs.85%29.aspx

If impersonationLevel isn't enough, then your next step should be to specify explicit credentials. That will work. There is one more option but I don't recommend it: "delegate for authority", which allows such an account to pretend to be any account on the domain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文