0

I am trying to install SQL Server 2012 using inno setup. The installation part works fine. I am using a custom port to install the SQL server, so I change the port number in Windows Registry after the installation using RegWriteStringValue and trying to restart the SQL Server service in order to effect the changes.

But for some reason my logic to restart the service is not working through my code. I use the below procedure to achieve the same.

procedure RestartService;
var

    CommandLine: String;
    MyResultCode: Integer;
begin       
        CommandLine := 'cmd /c NET STOP MSSQLSERVER /y < "C:\test\yes.txt" & NET START SQLSERVERAGENT > C:\test\Output.txt';    
        Log('Command line is.. ' + CommandLine);
        Exec('cmd.exe', CommandLine, '', SW_HIDE, ewWaitUntilTerminated, MyResultCode);
        Sleep(20000);
end;

The file yes.txt contains the character Y and a new line since restarting the service requires a prompt to restart the dependent service.

When I open the Command prompt and run the command in the CommandLine directly, it works perfectly fine and gives me the following output.

The following services are dependent on the SQL Server (MSSQLSERVER) service.
Stopping the SQL Server (MSSQLSERVER) service will also stop these services.

   SQL Server Agent (MSSQLSERVER)

The SQL Server Agent (MSSQLSERVER) service is stopping.
The SQL Server Agent (MSSQLSERVER) service was stopped successfully.

The SQL Server (MSSQLSERVER) service is stopping.
The SQL Server (MSSQLSERVER) service was stopped successfully.

But from the program, it does not seem to work and returns MyResultCode as 1 and also the file Output.txt which writes the output after running the command is empty. What am I missing?

Thanks in advance, DeeJay.

DeeJay007
  • 469
  • 4
  • 30
  • You are repeating `cmd` in `cmd` command-line. It should be `CommandLine := '/c ...'`. – Martin Prikryl Oct 24 '18 at 09:30
  • 1
    And then replace the `/C` with `/K`, like the answer to the linked question shows. And replace `SW_HIDE` with `SW_SHOW`. – Martin Prikryl Oct 24 '18 at 09:32
  • What does `/K` means? Also since repeating `cmd` in the commandline worked fine directly I assumed it will work. Will build and check if your suggestions solve the issue. – DeeJay007 Oct 24 '18 at 09:37
  • The linked question explains what the `/K` does. – Martin Prikryl Oct 24 '18 at 09:45
  • Thanks Martin, `/K` helped me find the issue. The file `yes.txt` was not created and hence the command failed. – DeeJay007 Oct 25 '18 at 05:13
  • 1
    You do not need a file to provide an input to a command, use `echo`, like: `cmd /c echo yes|NET STOP MSSQLSERVER /y` - Though what do you need `yes.txt` for? Doesn't the `/y` serve the same purpose? – Martin Prikryl Oct 25 '18 at 06:12
  • Thanks a lot. I didn't know that before. This is exactly what I am looking for. – DeeJay007 Oct 25 '18 at 06:24

0 Answers0