Powershell to copy files over the network

Attempting to copy some files from a local server to a network resource, as shown in the images below:

Folder 1

enter image description here

Folder 2

enter image description here

I need to copy the entire contents of the ROOT folder to the SOURCE folder in DESTINATION. But every time he copies the content, I would like him to create a new folder with the same destination folder name, but with some sequential numbering. The number of folders and files in the SOURCE / ROOT folder always remains unchanged (2 folders and 2 files). I just need all the source materials to end up in a new folder at the destination every time I run the script. Here is an example script that I tested, but it just doesn't do what I need:

$date = Get-Date -format MMMM-dd-yyyy
$date2 = Get-Date -format yyyyMMdd

$Source = "C:\SOURCE\ROOT"

$Destination1 = "\\netshare\DESTINATION\DATA_May-26-2014"
$Destination2 = "\\netshare\DESTINATION\DATA_May-26-2014-1st"
$Destination3 = "\\netshare\DESTINATION\DATA_May-26-2014-2nd"
$Destination4 = "\\netshare\DESTINATION\DATA_May-26-2014-3rd"
$Destination5 = "\\netshare\DESTINATION\DATA_May-26-2014-4th"
$Destination6 = "\\netshare\DESTINATION\DATA_May-26-2014-5th"

#Check destination path
if (Test-Path $Destination1)
 {
  #then copy
  robocopy $Source $Destination2 /MIR /Z /E /fft /MAXAGE:$date2
 }
if (Test-Path $Destination2)
 {
  #then copy
  robocopy $Source $Destination3 /MIR /Z /E /fft /MAXAGE:$date2
 }
if (Test-Path $Destination3)
 {
  #then copy
  robocopy $Source $Destination4 /MIR /Z /E /fft /MAXAGE:$date2
 }
if (Test-Path $Destination4)
 {
  #then copy
  robocopy $Source $Destination5 /MIR /Z /E /fft /MAXAGE:$date2
 }
if (Test-Path $Destination5)
 {
  #then copy
  robocopy $Source $Destination6 /MIR /Z /E /fft /MAXAGE:$date2
 }
else
 {
  robocopy $Source $Destination1 /MIR /Z /E /fft /MAXAGE:$date2
 }

robocopy , ( ), robocopy, powershell script robocopy.

, script?

+4
1

. Robocopy , , PowerShell. , TimeStamp .

$TimeStamp = get-date -f yyyyMMddhhmm
$Destination = "\\netshare\DESTINATION\DATA_" + $TimeStamp
New-Item -ItemType directory -Path $Destination -Force
Copy-Item -Path $Source\*.* -Destination $Destination -Force

, RoboCopy, , - (, TeamCity).

(ROBOCOPY $Source $Destination /MIR /W:30 /R:10) ^& IF %%ERRORLEVEL%% LEQ 1 exit 0

. RoboCopy, ErrorLevel 1 , 0 .

+8

Source: https://habr.com/ru/post/1542153/


All Articles