Skip to content

Instantly share code, notes, and snippets.

@automationhaus
Last active November 22, 2016 21:50
Show Gist options
  • Save automationhaus/a1a288946c587986b395a1a77bffd62d to your computer and use it in GitHub Desktop.
Save automationhaus/a1a288946c587986b395a1a77bffd62d to your computer and use it in GitHub Desktop.
Function utilizing Robocopy to mirror data from one location to another
function Start-DataMigration
{
<#
.SYNOPSIS
Simple data migration function using Robocopy
.DESCRIPTION
Using Robocopy and designed for scheduled incremental data migration to mirron the source with the destination
.EXAMPLE
Example of how to use this cmdlet
PS>Start-DataMigration -Name "UserMigration" -Source "\\SERVER1\Share" -Destination "E:\Share" -ExcludeFiles "*.vmdk","*.mp3","*.iso"
.NOTES
Britt Thompson
[email protected]
.LINK
https://gist.github.com/amesritter/a1a288946c587986b395a1a77bffd62d
#>
[CmdletBinding(SupportsShouldProcess)]
param
(
[Parameter(Mandatory=$true)]
[string]$Name,
[Parameter(Mandatory=$true)]
[string]$Source,
[Parameter(Mandatory=$true)]
[string]$Destination,
[string]$ScriptPath = $PSScriptRoot,
[string]$DateFormat = $(Get-Date -Format yyyy-MM-dd_HHmm),
[string]$LogPath = "$ScriptPath\Logs",
[string[]]$ExcludeFiles = @("*.log","*.tmp","~*"),
[string]$LogFile = "$LogPath\$($Name)_$DateFormat.txt"
)
Write-Verbose "Source: $Source" -ForegroundColor Green
Write-Verbose "Destination: $Destination" -ForegroundColor Green
Write-Verbose "Name: $Name"
Write-Verbose "Script Path: $ScriptPath"
Write-Verbose "Log File: $LogFile"
Write-Verbose "Exclude: $($ExcludeFiles -join ", ")"
if ($PSCmdlet.ShouldProcess($Name))
{
if(!(Test-Path $LogPath)){ $Log = New-Item -Path $ScriptPath -ItemType Directory -Name $(Split-Path $LogPath -Leaf) }
Start-Process robocopy -ArgumentList "$Source $Destination /mir /purge /z /copyall /r:0 /w:0 /xf $ExcludeFiles /log+:$LogFile /np" -Wait -NoNewWindow
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment