<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: How do I execute a batch file using xp_cmdshell? in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828382#M4859225</link>
    <description>&lt;P&gt;@Volker It's a fine solution and I've already got somewhere I'm thinking of using it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; I was just trying to make the only downside I could see a bit more obvious &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 07 Oct 2010 12:36:15 GMT</pubDate>
    <dc:creator>former_SQLA_member1694874</dc:creator>
    <dc:date>2010-10-07T12:36:15Z</dc:date>
    <item>
      <title>How do I execute a batch file using xp_cmdshell?</title>
      <link>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaq-p/13828374</link>
      <description>&lt;P&gt;We are using ASA 11.0.1 build 2044.  The database runs as a windows service using the local system account.&lt;/P&gt;

&lt;P&gt;I need to copy data files from our web server to our database server before a stored procedure runs.  The xopy command is in a batch file and it works fine if I double-click on it or if I execute it from Start&amp;gt;&amp;gt;Run.  However, I need to call it as part of a procedure using xp_cmdsheell. When I try to run it this way, nothing happens.  &lt;/P&gt;

&lt;P&gt;Here is the simple script where 'XXXX' is a parameter I am passing to the batch file:&lt;/P&gt;

&lt;LI-CODE lang="sql"&gt;call xp_cmdshell('c:\\somefolder\\copy.bat' + ' XXXX', 'no_output');
&lt;/LI-CODE&gt;

&lt;P&gt;The copy.bat has the following syntax:&lt;/P&gt;

&lt;LI-CODE lang="sql"&gt;xcopy T:\\somefolder\\%1\\history\\*.txt c:\\somefolder\\%1\\history /s /y
&lt;/LI-CODE&gt;

&lt;P&gt;Again, this is being run on the database server and we are trying to get files from the web server.  The web server is mapped to the database server in windows explorer as follows: c$ on 'server1' (T:).  I've modified copy.bat in different ways to get it to work without success.  For example, I've tried the following in an attempt to account for UNC mapping:&lt;/P&gt;

&lt;LI-CODE lang="sql"&gt;xcopy \\\\\\\\server1\\\\somefolder\\\\%1\\\\history\\\\*.txt c:\\somefolder\\%1\\history /s /y  
&lt;/LI-CODE&gt;

&lt;P&gt;What is the correct syntax for the xcopy command in this situation?  Is this a rights/permissions issue on the folder I am trying to copy from?  Thanks, Tom&lt;/P&gt;</description>
      <pubDate>Wed, 06 Oct 2010 17:41:32 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaq-p/13828374</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-10-06T17:41:32Z</dc:date>
    </item>
    <item>
      <title>Re: How do I execute a batch file using xp_cmdshell?</title>
      <link>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828375#M4859218</link>
      <description>&lt;P&gt;The problem is that the local system account has no network rights by default. You can either change that, or run the service under a user with the necessary rights.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Oct 2010 17:57:38 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828375#M4859218</guid>
      <dc:creator>justin_willey</dc:creator>
      <dc:date>2010-10-06T17:57:38Z</dc:date>
    </item>
    <item>
      <title>Re: How do I execute a batch file using xp_cmdshell?</title>
      <link>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828376#M4859219</link>
      <description>&lt;P&gt;If you do not want to change the service username, you can try using the Sysinternals tool PSEXEC.  That will allow you to specify a username/password to use to run the command either as you specify or to run it on the remote system (web) back to the DB system.  &lt;/P&gt;

&lt;P&gt;It all depends on what control you have over the usernames and rights on each system.  &lt;/P&gt;

&lt;P&gt;&lt;A href="http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx" rel="nofollow"&gt;PSEXEC Link&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Oct 2010 21:02:51 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828376#M4859219</guid>
      <dc:creator>former_SQLA_member1694868</dc:creator>
      <dc:date>2010-10-06T21:02:51Z</dc:date>
    </item>
    <item>
      <title>Re: How do I execute a batch file using xp_cmdshell?</title>
      <link>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828378#M4859221</link>
      <description>&lt;P&gt;@All: For those who don't know anything about &lt;A href="http://sysinternals.com"&gt;http://sysinternals.com&lt;/A&gt;, not only has it always been a 100%-trustworthy website for downloading Windows utility programs, but it is now owned and operated by Microsoft itself.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Oct 2010 05:58:55 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828378#M4859221</guid>
      <dc:creator>Breck_Carter</dc:creator>
      <dc:date>2010-10-07T05:58:55Z</dc:date>
    </item>
    <item>
      <title>Re: How do I execute a batch file using xp_cmdshell?</title>
      <link>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828377#M4859220</link>
      <description>&lt;P&gt;In addtion to Justin's correct answer:&lt;/P&gt;

&lt;P&gt;A third (and in my mind better) solution is to leave the service under the local system account (and do not give that account network rights for reasons of security) but to make a temporary network connection as a specific domain user with the according rights, something like&lt;/P&gt;

&lt;LI-CODE lang="sql"&gt;xp_cmdshell("NET USE /USER:.......")
-- do the desired network access
xp_cmdshell("NET USE /DELETE ....")
&lt;/LI-CODE&gt;

&lt;P&gt;When these connect/disconnect calls are put into separate procedures, you could even use ALTER PROCEDURE SET HIDDEN to hide any credentials.&lt;/P&gt;

&lt;P&gt;We have used this method to copy backup files to a net share for years.&lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;In addition, we typically use the 2&amp;gt; option with &lt;EM&gt;xp_cmdshell&lt;/EM&gt; to pipe &lt;EM&gt;stderr&lt;/EM&gt; messages to a file, something like&lt;/P&gt;

&lt;LI-CODE lang="sql"&gt;nRetCode = call xp_cmdshell('XCOPY C:\\*.* D: 2&amp;gt; C:\\MyError.txt', 'no_output');
&lt;/LI-CODE&gt;

&lt;P&gt;and in case nRetCode is &amp;lt;&amp;gt;´0 (which shows an error condition), read any OS error messages with xp_readfile('C:\\MyError.txt').&lt;/P&gt;</description>
      <pubDate>Thu, 07 Oct 2010 07:38:44 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828377#M4859220</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2010-10-07T07:38:44Z</dc:date>
    </item>
    <item>
      <title>Re: How do I execute a batch file using xp_cmdshell?</title>
      <link>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828379#M4859222</link>
      <description>&lt;P&gt;This always catches me out when I'm trying to unload to a UNC path and the server has permissions to it but not sybase &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Oct 2010 08:10:55 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828379#M4859222</guid>
      <dc:creator>former_SQLA_member1694874</dc:creator>
      <dc:date>2010-10-07T08:10:55Z</dc:date>
    </item>
    <item>
      <title>Re: How do I execute a batch file using xp_cmdshell?</title>
      <link>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828380#M4859223</link>
      <description>&lt;P&gt;I'm not sure but doesn't that mean any connection to the Sybase server can then execute sql to access the network share while you are between the use...delete ? It's probably not a problem for most people (if it is even the case), but some might not like that.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Oct 2010 08:24:57 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828380#M4859223</guid>
      <dc:creator>former_SQLA_member1694874</dc:creator>
      <dc:date>2010-10-07T08:24:57Z</dc:date>
    </item>
    <item>
      <title>Re: How do I execute a batch file using xp_cmdshell?</title>
      <link>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828381#M4859224</link>
      <description>&lt;P&gt;@Daz: I agree. But the other alternatives (granting the database service network access) would allow network access &lt;EM&gt;permanently&lt;/EM&gt;, whereas the solution shown does only allow that for a small timeframe - a clear improvement, methinks. - Besides that, &lt;EM&gt;xp_cmdshell&lt;/EM&gt;, &lt;EM&gt;xp_writefile&lt;/EM&gt; etc. require DBA permission, so &lt;EM&gt;not all users&lt;/EM&gt; would have access.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Oct 2010 09:21:13 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828381#M4859224</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2010-10-07T09:21:13Z</dc:date>
    </item>
    <item>
      <title>Re: How do I execute a batch file using xp_cmdshell?</title>
      <link>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828382#M4859225</link>
      <description>&lt;P&gt;@Volker It's a fine solution and I've already got somewhere I'm thinking of using it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; I was just trying to make the only downside I could see a bit more obvious &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Oct 2010 12:36:15 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828382#M4859225</guid>
      <dc:creator>former_SQLA_member1694874</dc:creator>
      <dc:date>2010-10-07T12:36:15Z</dc:date>
    </item>
    <item>
      <title>Re: How do I execute a batch file using xp_cmdshell?</title>
      <link>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828383#M4859226</link>
      <description>&lt;P&gt;First off, thanks to everyone who has replied.  I like the idea of employing NET USE for this.  However, I've never used it and I'm not sure what parameters to pass in for /USER and /DELETE.  Assuming my username for the server is 'juser' would it be something like the following?  Thanks&lt;/P&gt;
&lt;LI-CODE lang="sql"&gt;NET USE T: \\\\server1\\somefolder /USER: juser pwd
&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 08 Oct 2010 16:23:43 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828383#M4859226</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-10-08T16:23:43Z</dc:date>
    </item>
    <item>
      <title>Re: How do I execute a batch file using xp_cmdshell?</title>
      <link>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828384#M4859227</link>
      <description>&lt;P&gt;Volker -
I tried this:&lt;BR /&gt;
&lt;/P&gt;
&lt;P&gt;nRetCode = call xp_cmdshell('NET USE /USER:myuserid@production.local 2&amp;gt;' || strCmdErrorLog, 'no_output' ); &lt;/P&gt;
&lt;P&gt;The log file indicates that I am not using the correct syntax for NET USE -- it just lists what all the possible switches are.  When I tried this:&lt;/P&gt;
&lt;P&gt;nRetCode = call xp_cmdshell('NET USE M: \\servershare /USER:myuserid@production.local 2&amp;gt;' || strCmdErrorLog, 'no_output' );&lt;/P&gt;
&lt;P&gt;I got the following error in the log file:  "System error 67 has occurred. The network name cannot be found."&lt;/P&gt;</description>
      <pubDate>Fri, 08 Oct 2010 17:55:35 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828384#M4859227</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-10-08T17:55:35Z</dc:date>
    </item>
    <item>
      <title>Re: How do I execute a batch file using xp_cmdshell?</title>
      <link>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828385#M4859228</link>
      <description>&lt;P&gt;@Tom: I generally use the "domainuser" syntax, i.e. "NET USE M: \\MyServerMyShare /User:MyDomainMyUser MyPwd 2&amp;gt; c:MyError.log" and "NET USE M: /DELETE". Can't commment on the syntax with "MyUser@MyDomain". Note, however, when you use these commands as SQL strings, you will have to mask all backslashes by doubling them, i.e. 'NET USE M: \\\\MyServer\\MyShare ...'.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Oct 2010 21:16:12 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-do-i-execute-a-batch-file-using-xp-cmdshell/qaa-p/13828385#M4859228</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2010-10-08T21:16:12Z</dc:date>
    </item>
  </channel>
</rss>

