Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

psexec does not work properly when called from xp_cmdshell

I want to run a jarfile from an SQL Server to a different machine remotely. To do this, I found out I can use PSEXEC to send commands remotely to the target machine and use xp_cmdshell so I can call psexec command form a stored procedure.

I already extracted PSTools I downloaded from technet website. I copied PsExec.exe, PsExec64.exe and Eula.txt in C:\Windows folder of the SQL Server. I already tested it in the SQL Server's command prompt with the following command,

psexec \\mymachine -u mydomain\myuser -p mypassword cmd /c "java -jar C:\WriteToFile.jar"

WriteToFile_U4.jar writes to a text file in a shared folder. This shared folder is actually physically located in the SQL Server. Running the command above updates the text file. However, when I try to run WriteToFile_U4.jar thru xp_cmdshell with the code below the file does not get updated.

EXEC master..xp_cmdshell 'psexec \\mymachine -u mydomain\myuser -p mypassword cmd /c "java -jar C:\WriteToFile.jar"'

Also a weird output is displayed in the Results pane of SSMS. enter image description here

I already added the SQL Service Agent in the folder security where WriteToFile_U4.jar updates a text file.

Does anyone know how to deal with this? Does it matter in my issue that T-SQL commands I'm executing are executed as the SQL Service Agent? and not as the server Administrator account?

UPDATE:

I tried ditching the xp_cmdshell idea and just created an SQL Server Agent job whose type is Operating System (CmdExec). The contents of the step is,

cmd.exe /c "D:\runjarfileremote.bat"

The contents of the bat file is,

psexec \\computer-name -u domain\user -p password cmd /c "hostname"

just to check if I can run psexec in an SQL Server Agent job. But the following is logged in the history of the job I created. enter image description here

However, if I change the contents of the bat file to contain only hostname. The job finishes successfully. So I concluded that it is the psexec not really running properly when called as an SQL Server Agent job. Is there any way this can work? Or am I just doing something wrong?

like image 991
Krish Avatar asked May 20 '26 23:05

Krish


1 Answers

psexec (as with all current sysinternals tools) prompts on first run for any user for acceptance of a license. It may be being held up by that as it is being run by service accounts.

add -accepteula to the psexec command

example

EXEC master..xp_cmdshell 'psexec -accepteula \\mymachine -u mydomain\myuser -p mypassword cmd /c "java -jar C:\WriteToFile.jar"'
like image 176
JBJ Avatar answered May 22 '26 13:05

JBJ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!