SQL xp_cmdshell copy files between servers

32,592

Solution 1

I Found that the following worked for me;

  1. In the command prompt, type services.msc, this would open the list of all services on the server.

  2. In the list of services, look for SQL Server Agent, Right Click -> Properties. Go to Logon Tab

Change the logon to a user with access on both servers. then re-write your script to use Server Agent CmdExec job steps(Thank you Pete Carter)

Solution 2

I would strongly advise...Do not use xp_cmdshell. It opens up large security wholes in your surface area and makes you vulnerable to attack. xp_cmdshell should be disabled!

Instead, if you want to automate this with server agent you have 2 options. My preference would be to write a simple SSIS package with a file system task and schedule this package with server agent. SSIS is underutilized for this kind of task but is actually pretty good at it.

Alternatively re-write your script to use Server Agent CmdExec job steps. This does not require xp_cmdshell to be enabled and reduces the attack surface.

Share:
32,592
JPVoogt
Author by

JPVoogt

South African Data Tinkerer and @Microsoft Fanboy | Johannesburg Data Platform Leader | Formula 1 enthusiast and Father | (He/Him)

Updated on February 08, 2020

Comments

  • JPVoogt
    JPVoogt over 4 years

    I am trying to move all .zip in a specific folder to another folder. the source folder is located on another server, currently i am using

    EXECUTE xp_cmdshell 'copy \\server1\e$\ETL\*.zip \\server2\e$\ETL\'
    GO
    

    Which is working if I am logged into both server, but the goal is to automate this process VIA sql server job agent. I have tried

    EXECUTE sp_xp_cmdshell_proxy_account 'domain\useracc','pass'
    GO
    EXECUTE xp_cmdshell 'copy \\server1\e$\ETL\*.zip \\server2\e$\ETL\'
    GO
    

    but I am receiving the following error;

    An error occurred during the execution of sp_xp_cmdshell_proxy_account. Possible reasons: the provided account was invalid or the '##xp_cmdshell_proxy_account##' credential could not be created. Error code: '0'.

    And also not sure if this is my solution. Please help with how I can achieve this. The file names on server1 change name and quantity everyday.