question

Ed7 avatar image
1 Vote"
Ed7 asked LimitlessTechnology-2700 answered

Powershell Script for robocopy

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-serverwindows-server-powershellremote-desktop-servicesoffice-scripts-excel-devwindows-server-backup
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered

When you map the D:\ drive, provide the user credentials needed to authenticate to the destination server.

https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-R2-and-2012/gg651155(v=ws.11)

https://shellgeek.com/powershell-map-network-drive/

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Ed7 avatar image
0 Votes"
Ed7 answered

@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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
1 Vote"
MotoX80 answered MotoX80 edited

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

both on a different domain.

Does a trust relationship exist between the two domains? When you use the /COPYALL switch, robocopy will copy the NTFS permissions from the source to the destination. If they reference AD groups/accounts from the "old domain" and the "new domain" is unable to authenticate any "old domain" accounts, they you will have succeeded in copying the data files, but the users will not be able to access them.

0 Votes 0 ·
LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

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://docs.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--

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.