I've written the sp below to start/stop windows services.Can it be optimized any further and is the use of cmshell OK for this?alter procedure spServiceController @Server varchar(250), @Command varchar(250), @Service varchar(250)asSET NOCOUNT ONCREATE table #result(data varchar(100))-- MSSQLServerOLAPServicedeclare @SCCmd VARCHAR(1000) = ''' sc \\' + @Server + ' ' + @Command + ' ' + @Service + '''',@State varchar(250) = CASE WHEN @Command = 'start' then 'RUNNING' WHEN @Command = 'stop' then 'STOPPED' END EXEC ('exec master..xp_cmdshell ' + @SCCmd + ', ''no_output''' )--Get Service StatusDECLARE @STATUS VARCHAR(100) = 'OTHER'DECLARE @TimeOutInit datetime = current_timestampSET @SCCmd = ''' sc \\' + @Server + ' query ' + @Service + ' |find "STATE"'''WHILE @STATUS <> @State AND DATEDIFF(SECOND, @TimeOutInit, current_timestamp) <= 120BEGIN EXEC ('INSERT INTO #result exec master..xp_cmdshell ' + @SCCmd) SELECT TOP 1 @STATUS = CASE WHEN data like '%'+ @State +'%' THEN @State ELSE @STATUS END FROM #result WHERE data IS NOT NULL AND data like '%'+ @State +'%' ENDSELECT 'SERVICE: ' + @Service + ' ON SERVER: ' + @Server + ' IS ' + @STATUS
--PhB