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.
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.
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?