Sql-server – EXEC master.dbo.xp_cmdshell ‘The user name or password is incorrect’

sql servert-sqlxp-cmdshell

I am trying to move a file from a network drive to another server and when I execute this statement. I get an output of

The user name or password is incorrect

DECLARE @SourceFile AS VARCHAR(500);  
DECLARE @DestinationFile AS VARCHAR(500);  
DECLARE @Cmd AS VARCHAR(500);

SET @SourceFile='\\SHDVNFDBV1\shared-backup\netFORUM_Archive.bak';
SET @DestinationFile ='J:\netFORUM_Archive_TEST.bak';


SET @Cmd = 'COPY ' + @SourceFile + ' ' + @DestinationFile; 

EXEC master.dbo.xp_cmdshell @Cmd

Best Answer

You should check if you have permissions or not

exec xp_cmdshell 'net use \\SHDVNFDBV1\shared-backup\  /USER:domain\username Passw0rd1'

Do not copy to root directory as @AaronBertrand mentioned.

As a side note, why are you using SQL Server to do filesystem tasks? This should be done by PowerShell e.g. Copy-Item c:\scripts\test.txt c:\test