开发者

Connect to remote computer using WMI and C#

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 th开发者_StackOverflow社区e 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?


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;


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.


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?


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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜