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 Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,122 questions
Remote Desktop
Remote Desktop
A Microsoft app that connects remotely to computers and to virtual apps and desktops.
4,240 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,485 questions
Windows Server Backup
Windows Server Backup
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.Backup: A duplicate copy of a program, a disk, or data, made either for archiving purposes or for safeguarding valuable files from loss should the active copy be damaged or destroyed.
450 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,363 questions
0 comments No comments
{count} vote

4 answers

Sort by: Most helpful
  1. MotoX80 31,571 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 31,571 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,351 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