简体   繁体   中英

C# - Windows Service - Remote WMI query throws error: RPC not found

I am developing a WMI query windows service to query network servers. If I run the application in console, it works as expected but the service fails to complete the WMI query. Is there any way I can setup the service so the RPC doesn't fail due to insufficient privileges? I am using credentials in the WMI query to connect to the remote PC so that should not be a problem.

Thanks

Probable reason:

  • Firewall configuration (RPC connections blockage)

  • You don't have enough permission to run WMI queries.

Second point is valid if you are trying to run queries on remote machines. You can use wbemtest to verify.

  1. Windows+R (run command)

  2. Type wbemtest

You have to connect ManagementScope and check for it's validity scope.IsConnected . It is just a snippet of code, you might have to provide a structure to it.

ConnectionOptions cOption = new ConnectionOptions();
    ManagementScope scope = new ManagementScope("\\\\" + machine + "\\" + nameSpaceRoot + "\\" + managementScope, cOption);
           scope.Options.Username = UserName;
            scope.Options.Password = passWord;
            scope.Options.EnablePrivileges = true;
            scope.Options.Authentication = AuthenticationLevel.PacketPrivacy;
            //scope.Options.Timeout = TimeSpan.FromSeconds(180);
            //cOption.Timeout = TimeSpan.FromSeconds(180);
            scope.Options.Impersonation = ImpersonationLevel.Impersonate;
            scope.Connect();
            return scope;



    if (scope.IsConnected && scope != null)
    {
    query = new ObjectQuery(@"Select * from Win32_SCSIController");
                            searcher = new ManagementObjectSearcher(scope, query); searcher.Options.Timeout = new TimeSpan(0, 0, wbemConnectFlagUseMaxWait);
                            ManagementObjectCollection qWin32_SCSIController = searcher.Get();
    foreach (ManagementObject item in qWin32_SCSIController)
    {    
    <Some code here>
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM