Powershell Script for robocopy

Ed7 96 Reputation points
2021-12-15T08:57:02.113+00:00

Hello,

I have setup a powershell script to do robocopy job.

I am new on robocopy and my plan is to transfer data from one server to another both on a different domain.
Since I have several folders to transfer, the way I have the script seems to do the work however I do not know how I can add the login details for the source/destination server. Here is where I am struggling. I have run the script on a pc and works fine but I need it for different server domains.

So far this is what I have

$sourcePath1 = "C:\path\my folder
$sourcePath2 = "C:\path\my folder
$destinationPath1 = 'D:\folder1\'
$destinationPath2 = 'D:\folder2enter code here\'
$filetransfer = 'C:\filetransferlog.txt'
robocopy $sourcePath1 $destinationPath1 /E /DCOPY:DAT /COPYALL /LOG:$filetransfer /MIR /TEE /W:3 /ZB /V /timfix /sl /r:7 /COPY:DAT #/BYTES
robocopy $sourcePath2 $destinationPath2 /E /DCOPY:DAT /COPYALL /LOG:$filetransfer /MIR /TEE /W:3 /ZB /V /timfix /sl /r:7 /COPY:DAT

Windows for business | Windows Client for IT Pros | User experience | Remote desktop services and terminal services
Microsoft 365 and Office | Development | Other
Windows for business | Windows Server | Devices and deployment | Set up, install, or upgrade
Windows for business | Windows Server | User experience | PowerShell
Windows for business | Windows Server | User experience | Other
0 comments No comments
{count} vote

4 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2021-12-15T16:33:39.687+00:00

    In the question that you posted you have:

    $destinationPath1 = 'D:\folder1\'
    

    Where does the D drive point to? Is that a drive letter that you mapped to the destination server? Are you able to view and update the files/folders on that share?

    If the user account that the robocopy/powershell processes are running as, does not have access to the \DestinationServer\ShareName then on the NET command you can provide the credentials (userid+password) so that the destination server can authenticate the user. You can also use Powershell's New-PSDrive cmdlet to map a network drive with alternate credentials.

    Another trick is to use a temporary local account, maybe named DataMigration, and put it in the Administrators group on both the source and destination servers. Use the same password on both machines. RDP to the source server with that account. You should then be able to access \DestinationServer\ShareName.

    Update: Example using New-PSDrive.

     $User = ".\admin"
     $PWord = ConvertTo-SecureString -String "admin" -AsPlainText -Force
     $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
     New-PSDrive -Name "N" -PSProvider "FileSystem"  -root "\\test10\temp" -credential $Credential
     $src = 'c:\data'
     $dst = 'n:\data'
     robocopy $src $dst /l 
     Remove-PSDrive -Name "N"
    
    1 person found this answer helpful.

  2. MotoX80 36,291 Reputation points
    2021-12-15T15:25:13.877+00:00
    0 comments No comments

  3. Ed7 96 Reputation points
    2021-12-15T16:04:04.347+00:00

    @MotoX80

    I am bit confused.

    So whithin my script how can I add the path with the new server name?

    Something like:

    robocopy \newserver\$sourcePath1 $destinationPath1

    0 comments No comments

  4. Limitless Technology 39,921 Reputation points
    2021-12-21T19:58:49.58+00:00

    Hello @Ed7

    I assume that you may need to use different credentials depending on the server in question,

    I would suggest to use the Get-Credential cmdlet to prompt credentials, for each machine you are running the script against:
    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/get-credential?view=powershell-7.2

    Hope this helps with your query,

    -----------

    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.