I have the windows console program code below that should show up NT Authoroty\Network Service but surprisingly it spells NetworkService without a space... When I check the account the SQLAgent is running on it's indeed Network Service.

Why does this code delete the space? it breaks all my solution down by doing so as NetworkService does not exist.

PS: I don't want to hard code the user; I must get it automatically.

class Program
{
    static string GetSQLAgentUsername()
    {
        string servicePath = string.Format("Win32_Service.Name='{0}'", "SQLSERVERAGENT");
        using (ManagementObject theService = new ManagementObject(new ManagementPath(servicePath)))
        {
            return theService.Properties["StartName"].Value.ToString();
        }
    }

    static void Main(string[] args)
    {
        System.Console.WriteLine(GetSQLAgentUsername());
        System.Console.ReadKey();
    }


}
有帮助吗?

解决方案

According to this MSDN page, it is because:

While the security subsystem localizes this account name, the SCM does not support localized names. Therefore, you will receive a localized name for this account from the LookupAccountSid function, but the name of the account must be NT AUTHORITY\NetworkService when you call CreateService or ChangeServiceConfig, regardless of the locale, or unexpected results can occur.

The value that you get when retrieving the name from WMO, you are getting the value that was passed to CreateService (AUTHORITY\NetworkService) verbatim.

I'd be surprised if there was another API that required "AUTHORITY\NETWORK SERVICE" though, can you give an example?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top