Starting a windows service with net.exe without showing the console window

0

I want to start a service silently with the net.exe command from a non elevated bat file. But I only want the UAC prompt to be asked for the net.exe and after that I don't want the console window to appear. I know there is a way to hide the console window with a lot of programs like nircmd:

nircmd elevatecmd execmd net start [service]

But the UAC prompt will be asked for the nircmd program.

Is it possible?

liamZ

Posted 2018-06-29T10:16:51.940

Reputation: 101

Any time you elevate a command, the UAC will be displayed. The UAC is being displayed for nircmd so it can run net start with the proper elevated privileges (which are required by Windows). – Anaksunaman – 2018-06-30T02:09:56.157

if you read my question you'll see that what I want is that the UAC being asked for the net.exe program, not another program like nircmd, and after that I want the console window to be hidden. – liamZ – 2018-06-30T18:15:17.993

Answers

0

I only want the UAC prompt to be asked for the net.exe[.] Is it possible?

To the best of my knowledge, the answer is likely "No".

The UAC is being displayed for nircmd so it can run net start with the proper elevated privileges (which are required by Windows). This elevation must occur before net start is run. In my experience, this is generally true of many third-party programs or scripts that perform a similar function.

Elevate is an exception (i.e. it will display Net Command in the UAC), but then again, it doesn't suppress the console window like nircmd does.

After that I don't want the console window to appear.

Though a console window might appear with other solutions (e.g. elevate.exe, a .vbs script), a new console window does not appear after running nircmd elevatecmd execmd net start example_service.

Suppressing The Console Window

While it likely isn't possible to display net.exe in the UAC, you can suppress the console window for the batch file (which it honestly sounds like is your true goal, if I am not misreading things too badly):

  1. Create a .bat file e.g. start_service.bat with something like the following:

    C:\path\to\nircmd.exe elevatecmd execmd net start example_service
    
  2. Create a new shortcut somewhere to run this .bat file. Make the target similar to the following:

    C:\path\to\nircmd.exe execmd C:\path\to\start_service.bat
    

For this example, when this shortcut is used (not the .bat file directly), only the UAC prompt will be displayed (no console window will appear).

Anaksunaman

Posted 2018-06-29T10:16:51.940

Reputation: 9 278

if you run from a non elevated console "net start service" the UAC will be displayed for the net.exe command. Maybe the way to go is running net.exe from the SYSTEM user in a scheduled task? WHat I do not like of nircmd is that it is not signed. – liamZ – 2018-07-01T14:18:45.737

sorry, what I was trying to say is that if you elevate net.exe from a nonelevated console (using nircmd.exe, elevate.exe, vbscript...) the UAC will be displayed for the net.exe. – liamZ – 2018-07-01T14:35:22.240

If you run net start service from a non-elevated console, no UAC will be displayed at all, it will simply return an Access Denied message. If you elevate net.exe from a non-elevated console using nircmd.exe, the UAC prompt will be for nircmd. Regarding .vbs, the scripts I am familiar with do not prompt for net.exe either. Using elevate.exe, you are correct in that it will display Net Command (for the net.exe) but it does not suppress the console window, unfortunately. – Anaksunaman – 2018-07-01T19:22:22.123

I couldn't comment much on the task scheduler solution. Obviously you could give it a shot. That said, while I understand the hesitancy regarding nircmd being unsigned, it is a perfectly safe piece of software in my experience. – Anaksunaman – 2018-07-01T19:25:24.050

With 'nircmd elevate net start service' UAC will be prompted for the net.exe command. As far as I know, nircmd is safe but not everybody knows that, and it will be better that in 2018 would be a signed program. Anyway, thanks for your help. – liamZ – 2018-07-02T15:52:17.227

Signing is definitely a good idea. And you are welcome. =) – Anaksunaman – 2018-07-02T16:59:47.900

0

I finally found a way to start and stop a service silently without showing the console window, and UAC displayed for a Microsoft signed program (wscript.exe), with VBScript that uses WMI to start and stop a service.

Start a service: Create a file StartService.vbs with this code and change the first line with the service you want to start.

strServiceName = "YourServiceName"

If Not WScript.Arguments.Named.Exists("elevate") Then
  CreateObject("Shell.Application").ShellExecute WScript.FullName _
    , """" & WScript.ScriptFullName & """ /elevate", "", "runas", 1
  WScript.Quit
End If

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'")
For Each objService in colListOfServices
objService.StartService()
Next

to start the service:

wscript pathto\StartService.vbs

Stop a service: Create a file StopService.vbs with the code:

strServiceName = "YourServiceName"

If Not WScript.Arguments.Named.Exists("elevate") Then
  CreateObject("Shell.Application").ShellExecute WScript.FullName _
    , """" & WScript.ScriptFullName & """ /elevate", "", "runas", 1
  WScript.Quit
End If

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'")
For Each objService in colListOfServices
objService.StopService()
Next

to stop the service:

wscript pathto\StopService.vbs

liamZ

Posted 2018-06-29T10:16:51.940

Reputation: 101