I'm executing the following command to stop a service on a remote server from within a stored procedure in SQL Server.
----------------
-- Stop Service
----------------
SET @Command = 'sc \\' + @Server + ' stop ' + @ServiceName;
EXEC master.dbo.xp_cmdshell @Command;
Depending on the service sometimes this executes successfully but other times I receive the following error:
[SC] OpenService FAILED 5:
NULL
Access is denied.
NULL
NULL
Im curious what user executes the command. Is it the user I login/connect with to the database? Or is it the user Im logged onto the physical server with? Can I pass in different authentication with the xp_cmdshell
?
EXEC sys.xp_cmdshell 'whoami';