0

I need to check and edit the windows service's StartName.

I have founded this solution throught the WMI API:

using (var service = new ManagementObject($"Win32_Service.Name = '{my_name}'"))
{
    using (var inParams = service.GetMethodParameters("Change"))
    {
        inParams["StartName"] = value;
        ManagementBaseObject outParams = service.InvokeMethod("Change", inParams, null);
        outParams.Dispose();
    }
}

When I invoke this code by a new console project application with the Visual Studio that had been lauched as administrator it works fine.

But when I invoke it by the windows service that had loged as LocalSystem i got an exception:

System.Management.ManagementException: Provider Load Failur. in System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode) in System.Management.ManagementObject.InvokeMethod(String methodName, ManagementBaseObject inParameters, InvokeMethodOptions options) ...

What have I missed in this case? What permissions/application state or whatever do I must to change to edit the service StartName by the windows service?

Presto
  • 888
  • 12
  • 30
Kamerton
  • 315
  • 3
  • 9
  • Can you not just edit the Start Name in the service project properties and re-build it? – Wheels73 Jul 02 '18 at 10:32
  • I need in runtime properties edit. Project and installer properties had been setted as well but customers had changed them somehow so i need to check this case now. – Kamerton Jul 02 '18 at 10:43
  • I see. Well seems to me that your LocalSystem account doesn't have the necessary permissions. Can you change the service account to one that has admin rights? – Wheels73 Jul 02 '18 at 10:47
  • Trying to imagine why the startname of your service needs to change after installation.. You can edit the registry however you do need sufficient privilages. – BugFinder Jul 02 '18 at 10:56
  • Not after installation actualy. It had been changed by windows policies automaticly or by someone of an service engeneirs on place.Yes it is not usual case but we wanted to control it and i was surprised that i have no permissions for that by the servise itself. – Kamerton Jul 02 '18 at 10:59

1 Answers1

0

I think that the better approach would be change the name in te registry. You will probably have to start Visual Studio as Administrator. Link: https://stackoverflow.com/a/3887455/637840

The Change() method on WMI got a lot of overhead you don´t need. You will probably make a mistake on some parameter and potentially your service register is going to be broken. Take a look at everything you need to set in order to just change the name by WMI: https://learn.microsoft.com/es-es/windows/desktop/TermServ/win32-terminalservice-change

Anyway, if you need to do it by all means using WMI. You can use ORMi. You have a tutorial on the repo that will show you how to do method working.

Also you can use this as reference: https://medium.com/@luque.nicolas/compare-ormi-and-traditional-net-wmi-implementation-f00db26d10a3

NicoRiff
  • 4,803
  • 3
  • 25
  • 54