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
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
No comments:
Post a Comment