title: psexec does not work properly when called from xp_cmdshell
body: <p>I want to run a <strong>jarfile</strong> from an SQL Server to a different machine remotely. To do this, I found out I can use <strong>PSEXEC</strong> to send commands remotely to the target machine and use <strong>xp_cmdshell</strong> so I can call psexec command form a stored procedure.</p>
  
  <p>I already extracted PSTools I downloaded from <a href="https://technet.microsoft.com/en-us/sysinternals/pxexec.aspx" rel="nofollow noreferrer">technet website</a>. 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,</p>
  
  <pre><code>psexec \\mymachine -u mydomain\myuser -p mypassword cmd /c "java -jar C:\WriteToFile.jar"
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>EXEC master..xp_cmdshell 'psexec \\mymachine -u mydomain\myuser -p mypassword cmd /c "java -jar C:\WriteToFile.jar"'
  </code></pre>
  
  <p>Also a weird output is displayed in the Results pane of SSMS.
  <a href="https://i.stack.imgur.com/K8VM8.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/K8VM8.jpg" alt="enter image description here"></a></p>
  
  <p>I already added the <strong>SQL Service Agent</strong> in the folder security where WriteToFile_U4.jar updates a text file.</p>
  
  <p>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?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I tried ditching the xp_cmdshell idea and just created an <strong>SQL Server Agent job</strong> whose type is <strong>Operating System (CmdExec)</strong>. The contents of the step is,</p>
  
  <pre><code>cmd.exe /c "D:\runjarfileremote.bat"
  </code></pre>
  
  <p>The contents of the bat file is,</p>
  
  <pre><code>psexec \\computer-name -u domain\user -p password cmd /c "hostname"
  </code></pre>
  
  <p>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.
  <a href="https://i.stack.imgur.com/Xf7i1.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Xf7i1.jpg" alt="enter image description here"></a></p>
  
  <p>However, if I change the contents of the bat file to contain only <code>hostname</code>. 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?</p>