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:
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?