0

So I want to enable xp_cmdshell through a linked server. This is my attempt:

set @execute = 'exec '+'['+@server_ip+']'+'.master..sp_configure ''xp_cmdshell'', 1;'
set @reconfigure = 'exec '+'['+@server_ip+']'+ 'reconfigure'

exec sp_executesql @execute
exec sp_executesql @reconfigure

I have to run the reconfigure command, as I receive this message:

'Configuration option 'xp_cmdshell' changed from 0 to 1. Run the RECONFIGURE statement to install.'

The reconfigure part does not work. I don't know how to activate the reconfigure command through a linked server. Thanks!

Claudiu Haidu
  • 817
  • 4
  • 12
  • 24
  • What does *doesn't work* mean? Do you get an error? Why are you trying to enable a dangerous feature like `xp_cmdshell` remotely instead of asking the server's DBA to do it on the server? If you lack such permissions, you won't be able to enable it in any way – Panagiotis Kanavos Oct 16 '14 at 07:07
  • @Panagiotis Kanavos : well, I want to do a stored procedure which works with xp_cmdshell on different servers, and I want to make sure xp_cmdshell is turned on (on the respective server) when the procedure runs. I don't want to do that manually every time locally. – Claudiu Haidu Oct 16 '14 at 07:21
  • Yes I get error, could not find stored procedure '[server_ip]' ... – Claudiu Haidu Oct 16 '14 at 07:22
  • and it is not duplicate, I already saw that post before and it does not refer to linked servers. – Claudiu Haidu Oct 16 '14 at 07:24
  • This means you have a typo on the second command, which produces `exec [10.10.10.10]reconfigure`. – Panagiotis Kanavos Oct 16 '14 at 07:34
  • Could not find stored procedure '10.10.10.10'. It would have been nice to work :) – Claudiu Haidu Oct 16 '14 at 07:39
  • 1
    It's still a typo. You are trying to execute a command as if it were a stored procedure. You can't write `exec [10.10.10.10]reconfigure` or `exec [10.10.10.10] reconfigure` or `exec [10.10.10.10].reconfigure`. Try to create an `exec` statement that works first, then try to use the ip as a parameter – Panagiotis Kanavos Oct 16 '14 at 07:41

2 Answers2

2

You have a typo on the second command, which produces exec [10.10.10.10]reconfigure. This is the wrong way to execute a command though. RECONFIGURE is a command just like EXEC, so you can't call it the same way you call a stored procedure

To execute a command on a linked server you need to use the EXEC ... AT linked_server_name syntax:

EXEC ('RECONFIGURE') AT my_server_name
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
0

Why dont you put them in the same session?

set @execute = 'exec '+'['+@server_ip+']'+'.master..sp_configure ''xp_cmdshell'', 1; exec '+'['+@server_ip+']'+ 'reconfigure'

exec sp_executesql @execute
SubqueryCrunch
  • 1,325
  • 11
  • 17