Issue sql server query and restart windows service

  • How can i best issue an sql query and also restart a windows service from a remote machine?

    “When I hear somebody sigh, ‘Life is hard,’ I am always tempted to ask, ‘Compared to what?’” - Sydney Harris

  • Powershell?

    What query are you attempting to run? And are you asking to restart a windows service on an external machine via TSQL?

    ______________________________________________________________________________Never argue with an idiot; Theyll drag you down to their level and beat you with experience

  • kapfundestanley (3/23/2014)


    How can i best issue an sql query and also restart a windows service from a remote machine?

    My first temptation is to issue a wiseacre answer like:

    1. SSMS

    2. Control Panel > Administrative Tools > Services > Connect to another computer

    But seriously...

    1. Are you trying to do both from a scripted situation, such as a scheduled task?

    2. If so have you considered a batch script running osql for the SQL and then a SC \\[machinename] STOP and SC\\[machinename] START

    Thanks

    John.

  • Yes ,I have managed to do a batch script:

    sqlcmd -i C:\scripts\C2.sql

    sc \\servername stop Servicename

    sc \\servername start Servicename.

    This is working if I want to run script ,start and stop service on the same server.When the server to run script and start/stop service is different the batch script is not working.

    “When I hear somebody sigh, ‘Life is hard,’ I am always tempted to ask, ‘Compared to what?’” - Sydney Harris

  • You need to tell SQLCMD which server to connect to in order to run the script you're passing it--you do that with the -S parameter (note that's a capital S, lowercase s does something else). You may also need to supply a username and password (using -U and -P) depending on the situation.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply