Thursday, March 31, 2011

Set Programatically a Windows Service Log on to Local System Account with Desktop interact.

Hello Everyone :)

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 and Password:

NT AUTHORITY\LocalService

running my service with

net start

brings a Error 5: Access Denied Message.

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

Thanks for the help!

From stackoverflow
  • 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);
        }
    }
    

    }

    Phil : You forgot the "Desktop interact" part. Just add wmiParams[5] = true;

0 comments:

Post a Comment