C# 以编程方式更改 Windows 服务用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/961719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:19:22  来源:igfitidea点击:

Change Windows Service user programmatically

c#.netwindows-services

提问by Anne

I need to change Logon user for a Windows service programmatically. And I am using the following code to do that:

我需要以编程方式更改 Windows 服务的登录用户。我正在使用以下代码来做到这一点:

string objPath = string.Format("Win32_Service.Name='{0}'", ServiceName);
using (ManagementObject service = new ManagementObject(new ManagementPath(objPath)))
{
object[] wmiParams = new object[11];

if (PredefinedAccount)
    {
        wmiParams[6] = "LocalSystem";
            wmiParams[7] = "";
    }
    else
    {
        wmiParams[6] = ServiceUsername; // provided by user
            wmiParams[7] = ServicePassword; // provided by user
    }

    object invokeResult = service.InvokeMethod("Change", wmiParams);

// handle invokeResult - no error up to this point
}

This code works in 90% of situations, but in some situations service cannot be started due to logon failure. There is usually no error on InvokeMetod but when we try to start the service we get the following error:

此代码在 90% 的情况下都有效,但在某些情况下,由于登录失败,服务无法启动。InvokeMetod 通常没有错误,但是当我们尝试启动服务时,会出现以下错误:

System.InvalidOperationException: Cannot start service X on computer '.'. --> System.ComponentModel.Win32Exception: The service did not start due to a logon failure.

System.InvalidOperationException: 无法在计算机“.”上启动服务 X。--> System.ComponentModel.Win32Exception: 由于登录失败,服务没有启动。

The workaround solution is simple, we just need to enter the same credentials via Windows interface and problem is solved.

解决方法很简单,我们只需要通过 Windows 界面输入相同的凭据即可解决问题。

So my question is, has anybody experienced the similar problem with ManagementObject because it seems that in some situation it does not relate Username and password to windows service?

所以我的问题是,有没有人遇到过与 ManagementObject 类似的问题,因为在某些情况下它似乎没有将用户名和密码与 Windows 服务相关联?

回答by flipdoubt

Do you notice any patterns amongst those failures? Same machine? Same OS? Same user? Does the user have "logon as service" or "logon interactively" rights? Personally, I am not familiar with this method of specifying the user for a service. I would have thought you would have to restart the service, but I guess not if it works 90% of the time.

您是否注意到这些失败中的任何模式?同一台机器?相同的操作系统?同一个用户?用户是否具有“作为服务登录”或“交互登录”的权限?就我个人而言,我不熟悉这种为服务指定用户的方法。我原以为您必须重新启动该服务,但如果它在 90% 的时间内都可以正常工作,我想不会。

回答by trudger

It's because the account has no "Log On as service" privilege. You need to use LsaAddAccountRights to add such privilege to the account.

这是因为该帐户没有“作为服务登录”的权限。您需要使用 LsaAddAccountRights 将此类权限添加到帐户。

View this article please:

请查看这篇文章:

How To Manage User Privileges Programmatically in Windows NT

如何在 Windows NT 中以编程方式管理用户权限