1

I have a Windows service built which is being installed by .NET 2.0's installutil /i command. It installs the service as with the following account, with a password:

NT AUTHORITY\LocalService

When I run my service with net start <serviceName>, I get

Error 5: Access Denied

To remove it I've had to open up services.msc and from the Properties give the service

Logon As -> Local System Account
         -> Allow Service to interact with desktop.

Can I put this whole “clicky” business into code which is either native .NET C# code or WMI or some other Batch script? I'll be using a batch script anyways so either is fine.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254

1 Answers1

4

Figured out an Answer, thanks very much to the following Webpage to which I give full credit.

link text

Here's the Solution I have, just change your service name as needed. Throw it in a C# Console App and run it :)

static void Main(string[] args)
{
    string serviceName = "SERVICE_NAME_HERE"; 
    string objPath = string.Format("Win32_Service.Name='{0}'", serviceName);
    using (ManagementObject service = new ManagementObject(new ManagementPath(objPath)))
    {
        object[] wmiParams = new object[11];
        wmiParams[6] = "LocalSystem";
        wmiParams[7] = "";
        service.InvokeMethod("Change", wmiParams);
    }
}

}