【问题标题】:Cannot open Service Control Manager on computer无法在计算机上打开服务控制管理器
【发布时间】:2014-08-27 11:26:47
【问题描述】:

我正在尝试编写一个程序来连接到远程服务器并显示服务的某些状态。该服务也是由我们(我的公司)编写的。

为此,我编写了一个控制台应用程序,代码是

static void Main(string[] args)
    {
        ConnectionOptions options = new ConnectionOptions();
        options.Password = "mypassword";
        options.Username = "Administrator";
        options.Impersonation =
            System.Management.ImpersonationLevel.Impersonate;

        ManagementScope scope =
            new ManagementScope(
            "\\\\ip_of_the_server\\root\\cimv2", options);
        scope.Connect();

        ServiceController svc = new ServiceController("My_Service_Name", "ip_of_the_server");

        var status = svc.Status.ToString();
        Console.WriteLine(svc.DisplayName + " : " status);

    }

但我不能让它工作。我得到的错误是:

Cannot open Service Control Manager on computer 'ip_of_the_server'. This operation might require other privileges.

内部异常:“拒绝访问”。

堆栈跟踪:

   at System.ServiceProcess.ServiceController.GetDataBaseHandleWithAccess(String     machineName, Int32 serviceControlManaqerAccess)
   at System.ServiceProcess.ServiceController.GetDataBaseHandleWithConnectAccess()
   at System.ServiceProcess.ServiceController.GenerateNames()
   at System.ServiceProcess.ServiceController.get_ServiceName()
   at System.ServiceProcess.ServiceController.GenerateStatus()
   at System.ServiceProcess.ServiceController.get_Status()
   at ServiceConsole.Program.Main(String[] args) in c:\Users\Kandroid\Documents\Visual Studio     2013\Projects\ServiceConsole\ServiceConsole\Program.cs:line 33
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity,   String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

知道怎么解决吗?

【问题讨论】:

  • 使用具有正确权限的帐户?
  • :) 是的。但是我正在使用“管理员”。还有我需要什么样的许可?
  • 您设置了一个管理范围,但我看不到它在您的 ServiceController 中的任何地方,对吧?
  • 好的。你的意思是我必须以某种方式将范围与 servicecontroller 一起使用?

标签: c# .net windows service windows-server-2012


【解决方案1】:

正如您已经设置了管理范围,只需使用 WQL 向 WMI 提供程序查询正确的 WMI 对象,在本例中为 WIN32_Service,如下所示:

var svc = new ManagementObjectSearcher(
    scope,
    new ObjectQuery("Select Status,State from Win32_Service  where Name='My_Service_Name'"))
        .Get()
        .GetEnumerator();

    if (svc.MoveNext())
    {
          var status = svc.Current["Status"].ToString();
          var state = svc.Current["State"].ToString();
          Console.WriteLine("service status {0}", status);
          // if not running, StartService
          if (!String.Equals(state, "running",StringComparison.InvariantCultureIgnoreCase) {
               ( (ManagementObject) svc.Current ).InvokeMethod("StartService", new object[] {});
          }
    }
    else 
    {
           Console.WriteLine("service not found");
    }

ManagementObjectSearcher 负责检索 WMI 托管对象的集合。 ObjectQuery 将返回作用域中的类实例。我们可以执行基本的 sql select 语句来选择和投影结果集。

迭代器返回一个ManagementObjectBase,它具有Item 访问器以从返回的实例中检索属性。

【讨论】:

  • 谢谢...您的代码有效。但是这样我该如何启动和停止服务呢?
  • 我添加了调用 StartService 操作的代码
  • 非常感谢..现在我可以从这里拿走了...:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-30
  • 1970-01-01
  • 1970-01-01
  • 2022-11-13
  • 2015-12-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多