RPC 服务器不可用。调用 ManagementScope.Connect 时

发布于 2024-10-03 19:58:49 字数 487 浏览 1 评论 0原文

我需要远程创建一个服务,并且不想突破命令并运行“sc \remotemachine create ......”并且希望在 C# 代码中执行此操作。

但是,当运行代码时,即使 sc 命令工作得非常顺利,因此正在管理远程系统,但 ManagementScope.Connect 调用会引发异常“RPC 服务器不可用”。

执行此操作的代码片段是

string machineName = "othermachine";
string path = string.Format(@"\\{0}\root\cimv2", machineName);
ManagementScope scope = new ManagementScope(path);
scope.Connect();

因为这是旅程的第一步,所以我对下一步该去哪里有点困惑。 sc 命令有效,那么它有什么不同(除了不使用 .net 库 - 也许我需要更努力地使用 winapi?)

谢谢

I need to create a service remotely and would rather not break out to a command and run "sc \remotemachine create ......" and want to do it within the C# code.

However, when running the code even though the sc command works quite happily and thus is managing the remote system the ManagementScope.Connect call is throwing an exception with "The RPC server is unavailable."

A snippet of code that does this is

string machineName = "othermachine";
string path = string.Format(@"\\{0}\root\cimv2", machineName);
ManagementScope scope = new ManagementScope(path);
scope.Connect();

as this is about the first step on the journey I'm a bit stuck as to where to go next. The sc command works, so what is it doing different (other than not using .net libraries - maybe I need to hit the winapi harder?)

Thanks

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

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

发布评论

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

评论(2

べ繥欢鉨o。 2024-10-10 19:58:49

在思考了一会儿之后,看到分配给该任务的时间被侵蚀了,我拿出了旧的 WIN32 api 文档,并 [DllImport] 编辑了我需要的调用,以“老式的方式”执行此操作。作为一名老 C 狗,我曾经了解这些调用的方法,令人惊讶的是,即使在使用托管库多年之后,这些调用的返回量也令人惊讶。

我需要做的是能够创建一个服务,启动它,做有用的工作,停止它并删除它。创建、启动、停止和删除都是机制,我想专注于活动的“做有用的工作”部分。

我首先将该服务的 exe 复制到 ADMIN$ 共享并验证了该路径的本地路径(我们的服务器管理员无法得知!)。为此,我调用

["netapi32.dll"]NetShareGetInfo 

OpenService中的以下内容

  • ,然后使用 advapi32.dll OpenSCManager
  • CreateService
  • StartService
  • (当我想停止并删除它时 - 不要保持句柄打开,因为这可能需要一段时间才能保持自我状态)包含)
  • ControlService
  • QueryServiceStatus
  • CloseServiceHandle

即使跨 VPN 连接,这一切也能正常工作。

我只能猜测托管 api 试图做的事情远远超出了我实际需要的范围 - 使用托管 api 和 Windows api 所花费的时间差异相当大,并且不能保证托管 api 能够完成它这不是一个合适的前进方向。

After pondering this for a little while seeing the time assigned to this task being erroded away I got the old WIN32 api documentation out and [DllImport]'ed the calls I needed to do this the "old fashioned way". Being an old C dog I used to know my way around these calls, amazing how much comes back even after quite a few years of using the managed libraries.

What I needed to do was to be able to create a servcie, start it up, do useful work, stop it and delete it. The create, start, stop and delete are mechanics and I wanted to concentrate on the "do useful work" part of the activity.

I first copied the service's exe to the ADMIN$ share and verified the local path to that (with our server admins there is no telling!). To do this I call

["netapi32.dll"]NetShareGetInfo 

and then I used the following with from advapi32.dll

  • OpenSCManager
  • CreateService
  • StartService
  • OpenService (for when I want to stop and delete it - don't keep the handles open as this may take a while so cleaner to keep thngs self contained)
  • ControlService
  • QueryServiceStatus
  • CloseServiceHandle

This all works even across the VPN connection.

I can only guess that the managed api is trying to do far more than I actually need - the difference in time taken using the managed api and the Windows api is quite a lot, and with no guarantees that the managed api will get through it wasn't a suitable way forward.

葬花如无物 2024-10-10 19:58:49

显然,MSDN 中的股票代码并没有描绘出全貌。我得到和你一样的结果。

看看这个人做了什么

编辑:

我相信您正在工作组而不是域上尝试,对吗?这就是窍门,工作组需要更多的工作才能完成。上面的链接似乎有一个解决方法。今晚我会在家再试一次。

Obviously the stock code from MSDN doesn't paint the whole picture. I get the same results as you.

Check out what this guy did.

EDIT:

I believe you're trying it on a workgroup and not a domain, right? That's the trick, workgroups demand a bit more work to get through. The link above seems to have a workaround. I will try again from home tonight.

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