Friday, June 26, 2020

RoboCopy Folder and its content to another folder using PowerShell

RoboCopy Folder and its content to another folder using PowerShell.

In this example we will use RoboCopy with pre-defined options to move content of folder A Form Source A Server into Folder A on Destination Server. Here is goes;

You will need to adjust some of the variables to make sure it fits into your needs

  

 

<#     
 
.NOTES
#=============================================
# Script      : RoboCopy-Files-V1.ps1
# Created     : ISE 3.0 
# Author(s)   : Casey.Dedeal 
# Date        : 06/24/2020 12:58:36 
# Org         : ETC Solutions
# File Name   : RoboCopy-Files-V1.ps1
# Comments    : Copy Files from source to destination
# Assumptions :
#==============================================
 
SYNOPSIS           : RoboCopy-Files-V1.ps1
DESCRIPTION        : Copy folders/content source to destination
Acknowledgements   : Open license
Limitations        : None
Known issues       : None
Credits            : None
 
.EXAMPLE
  .\RoboCopy-Files-V1.ps1
 
  MAP:
  -----------
  #(1)_.Adding Vars
  #(2)_.Define source and destination vars
  #(3)_.RoboCpy Options
  #(4)_.Function to check/create destination folder
 
  Note : Take out /TEE option if you want silent mode
  $Options     = ("/B","/MIR", "/XJ", "/FFT", "/R:0", "/V", "/LOG:$LogFile")
 
#>
 
 
 
#(1)_.Define source and destination vars
$timestamp = (Get-Date -format 'dd-MMM-yyyy-HH-mm-ss-')
$fileName  = 'RoboCopy.LOG'
$logname   = $now+$Filename
 
#(2)_RoboCpy Options
$destinDC    = 'SERVER002'
$sourceDC    = 'SERVER001'
$dataSource  = "\\$sourceDC\d$\Scripts\"
$dataDestin  = "\\$destinDC\d$\Scripts\"
$LogFile     = ($dataDestin+$logname )
$Options     = ("/MIR", "/XJ","/FFT", "/TEE", "/R:0", "/V","/LOG:$LogFile")
 
 
#(3)_.Function to check/create destination folder
Function Function-Check-Destination-Folder{
 
  [CmdletBinding()]
  param(
    [parameter(
     Mandatory         = $true,
     ValueFromPipeline = $true)]
      [string]$DestinationPath)
 
 Try{
 
 if (!(Test-Path -Path $DestinationPath))
 {
  New-Item -Type Directory -Path $DestinationPath -ErrorAction Stop | Out-Null
 }
 
}catch{
  
    $errormessage = $($PSItem.ToString())
    Write-Warning 'Error has occoured'
    Write-host 'Problem FOUND:' $errormessage -ForegroundColor Red -BackgroundColor Black
   }
}
 
#(4)_.Function-Check-Source Folder , Stop if not found
Function Function-Check-Source-Folder{
 
  [CmdletBinding()]
  param(
    [parameter(
     Mandatory         = $true,
     ValueFromPipeline = $true)]
      [string]$SourcePath)
 
 Try{
 
 if (!(Test-Path -Path $SourcePath))
 {
     Write-Host 'CANNOT locate (SOURCE) directory' -ForegroundColor White -BackgroundColor Red
     Write-Host 'Script will stop' -ForegroundColor Yellow
     break;
 
 }
 
}catch{
  
    $errormessage = $($PSItem.ToString())
    Write-Warning 'Error has occoured'
    Write-host 'Problem FOUND:' $errormessage -ForegroundColor Red -BackgroundColor Black
   }
}
 
#(5)_.Check source path,Action:<STOP>, if it does not exist
Function-Check-Source-Folder -SourcePath $dataSource
 
 
#(6)_.Create Destination,action:<CREATE>, if it does not exist
Function-Check-Destination-Folder -DestinationPath $dataDestin
 
#(7)_Start Robocopy with pre-defined options
 
write-host '++++++++++++++++++++++++++++++++++++++++++++++++++++'
Write-host 'Starting RoboCopy now' -ForegroundColor DarkYellow
robocopy $dataSource $dataDestin $Options
write-host '++++++++++++++++++++++++++++++++++++++++++++++++++++'
Write-host 'Completed' -ForegroundColor Green
   
 

 

Azure Solutions Architect
AWS Certified Cloud Practitioner
Azure Certified Security Engineer Associate
https://simplepowershell.blogspot.com
https://cloudsec365.blogspot.com
https://msazure365.blogspot.com
https://twitter.com/Message_Talk


1 comment: