5

This is about sc.exe

I want to run my program as a service in Windows. I can do it using command prompt

My program is placed in system32 folder, so first I go to system32 and then I use these commands

c:\windows\system32>sc create demo binpath= "pg.exe" type= own start= auto DisplayName= "autostart"
c:\windows\system32>sc config demo binpath= "cmd.exe /c c:\windows\system32>pg.exe" type= own start= auto DisplayName= "autostart"

after executing these commands, when I restart my PC, my program starts running as a service which is fine.

Now, I want to make an autoit script which will execute these commands but unable to understand how I do it.

I tried this

#include <RunCMD.au3>
$cmd = "sc create demo1 binpath= c:\pg.exe type= own start= auto"
_RunCMD($cmd)
$cmd = "sc config demo1 binpath= "cmd.exe /c c:\pg.exe" type= own start= auto"
_RunCMD($cmd)

The first command executes successfully, but the 2nd command doesn't.

phuclv
  • 30,396
  • 15
  • 136
  • 260

3 Answers3

1

How to execute cmd commands thru AutoIT

You can use the AutoIT Run function to execute \ run an external program with the Run function and below is an example with the logic you provided.

#RequireAdmin
Run('sc create demo1 binpath= c:\pg.exe type= own start= auto')
Run('sc config demo1 binpath= "cmd.exe /c c:\pg.exe" type= own start= auto')

The issue you're likely experiencing with getting the second command to run as expected with the _RunCMD function Snippet is due to the double quotes being used around and also within the value of the variable being set.

Here's an example of using the single quotes around the variable value rather than the double quotes since the actual value of the variable contains double quotes.

$cmd = 'sc config demo1 binpath= "cmd.exe /c c:\pg.exe" type= own start= auto'

enter image description here enter image description here

0

CMD commands are best executed from a Windows batch file, if you set the batch file to run at start up with the commands in a batch file it should work just fine.

  • If your running Windows 7 in the start menu there is a folder called "start up" drag the batch file with your commands into this folder, then when Windows starts so does it and it will execute the commands.
  • If you are running Windows 8 or 8.1 the file path is: C:\Users(your user)\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

Once the batch file is in the folder on start it runs executes your commands.

Format for the batch file:

@echo off
(your command)
(your command)
exit

Best of luck, if that doesn't work play around with the startup folder.

phuclv
  • 30,396
  • 15
  • 136
  • 260
0

Have a look at ComSpec https://www.autoitscript.com/wiki/Snippets_%28_CMD_%29

The syntax is a bit tricky due to escape characters at times but it's possible to do.

You may also find that you can replace some command line with built in functions in AutoIT

Codes
  • 1