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


Monday, June 22, 2020

Robocopy files from source to destination;




Robocopy files from source to destination;
In times, you might need to move folders/files from location A to location B. Following server will be perfect fit for the mission. When running the script you will need to adjust two variables


#(1)_. Add vars, surce and destination
$dataSouce   = '\\SourceServer\d$\Scripts\'
$dataDestin  = '\\DestinServer\d$\' 


The Function I have created called “Function-RoboCopy.ps1” will use following variables.

Function-RoboCopy -SourcePath $dataSouce -DestinationPath $dataDestin -LogPath $LogFile 



Rest of the script is also pretty easy to read. I will be adding few other functions to make sure source exist, , script will stop is it does not. Destination folder will also be created, assuming path is accessible.
Here it comes;

#(1)_. Add vars, surce and destination
$dataSouce   = '\\SourceServer\d$\Scripts\'
$dataDestin  = '\\DestinServer\d$\'

#(2)_.Construct Log file
$timestamp = (Get-Date -format 'dd-MMM-yyyy-HH-mm-ss-')
$fileName  = 'RoboCopy.LOG'
$logname   = $timestamp+$Filename 
$LogFile   = ($dataDestin +$logname )


#(3)_.Function-Check-Source if does not exist, stop
   Function Function-Check-Source{
 
  [CmdletBinding()]
  param(
    [parameter(
     Mandatory         = $true,
     ValueFromPipeline = $true)]
      [string]$SourcePath)

Try{

  If (!(Test-Path $dataSource)){
     Write-Host 'CANNOT locate file' -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
   }
}


#(4)_.Function to check/create destination folder
   Function Function-create-ReportFolder{
 
  [CmdletBinding()]
  param(
    [parameter(
     Mandatory         = $true,
     ValueFromPipeline = $true)]
      [string]$ReportPath)

Try{

if (!(Test-Path -Path $ReportPath))
{
  New-Item -Type Directory -Path $ReportPath -ErrorAction Stop | Out-Null
}

}catch{
  
    $errormessage = $($PSItem.ToString())
    Write-Warning 'Error has occoured'
    Write-host 'Problem FOUND:' $errormessage -ForegroundColor Red -BackgroundColor Black
   }
}




#(5)_.Function-Copy-Scripts source/Destination
   function Function-RoboCopy{

  [CmdletBinding()]
  param(
  [Parameter(Mandatory=$True)]
  [String]$SourcePath,
  [String]$DestinationPath,
  [String]$LogPath  
  ) 
 
   $Options   = ("/B","/MIR", "/XJ", "/FFT", "/R:0", "/LOG:$LogFile")


   Try{

        Write-host 'Starting RoboCopy now' -fore Green
        robocopy $SourcePath $DestinationPath $Options


      }Catch{

          $errorcode = $($PSItem.ToString())
          Write-Warning 'Error has occoured'
          Write-host "Problem FOUND: $errorcode" -f red -b White

      }
  }









#(6)_.Run function check source
   Function-Check-Source -SourcePath $dataSour1e

#(7)_.Run Function-create-ReportFolder
    Function-create-ReportFolder -ReportPath $Destin

#(8)_.Function-RoboCopy
   Function-RoboCopy -SourcePath $dataSouce -DestinationPath $dataDestin -LogPath $LogFile  




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