Skip to content

Instantly share code, notes, and snippets.

@nshores
Created May 21, 2018 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nshores/23c5726d8dd177e52fd11fae3773451f to your computer and use it in GitHub Desktop.
Save nshores/23c5726d8dd177e52fd11fae3773451f to your computer and use it in GitHub Desktop.
ps_robocopy.ps1
#Requires -Version 4
## Robocopy Script for multiple data sources
## Created for Grupe File Server Migration
# Run this script on the target server (where file shares are getting migrated to)
$LogFile = "$PSScriptRoot\logs\Migrate-FileShares_$SourceServer-$(Get-Date -format yyyy-MM-dd_hh-mm-sstt).txt"
$RoboLog = "$PSScriptRoot\logs\Robo-Migrate-FileShares_$SourceServer-$(Get-Date -format yyyy-MM-dd_hh-mm-sstt).txt"
# Loggging function
function Log {
<#
.Synopsis
Function to log input string to file and display it to screen
.Description
Function to log input string to file and display it to screen. Log entries in the log file are time stamped. Function allows for displaying text to screen in different colors.
.Parameter String
The string to be displayed to the screen and saved to the log file
.Parameter Color
The color in which to display the input string on the screen
Default is White
Valid options are
Black
Blue
Cyan
DarkBlue
DarkCyan
DarkGray
DarkGreen
DarkMagenta
DarkRed
DarkYellow
Gray
Green
Magenta
Red
White
Yellow
.Parameter LogFile
Path to the file where the input string should be saved.
Example: c:\log.txt
If absent, the input string will be displayed to the screen only and not saved to log file
.Example
Log -String "Hello World" -Color Yellow -LogFile c:\log.txt
This example displays the "Hello World" string to the console in yellow, and adds it as a new line to the file c:\log.txt
If c:\log.txt does not exist it will be created.
Log entries in the log file are time stamped. Sample output:
2014.08.06 06:52:17 AM: Hello World
.Example
Log "$((Get-Location).Path)" Cyan
This example displays current path in Cyan, and does not log the displayed text to log file.
.Example
"$((Get-Process | select -First 1).name) process ID is $((Get-Process | select -First 1).id)" | log -color DarkYellow
Sample output of this example:
"MDM process ID is 4492" in dark yellow
.Example
log "Found",(Get-ChildItem -Path .\ -File).Count,"files in folder",(Get-Item .\).FullName Green,Yellow,Green,Cyan .\mylog.txt
Sample output will look like:
Found 520 files in folder D:\Sandbox - and will have the listed foreground colors
.Link
https://superwidgets.wordpress.com/category/powershell/
.Notes
Function by Sam Boutros
v1.0 - 08/06/2014
v1.1 - 12/01/2014 - added multi-color display in the same line
#>
[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Low')]
Param(
[Parameter(Mandatory=$true,
ValueFromPipeLine=$true,
ValueFromPipeLineByPropertyName=$true,
Position=0)]
[String[]]$String,
[Parameter(Mandatory=$false,
Position=1)]
[ValidateSet("Black","Blue","Cyan","DarkBlue","DarkCyan","DarkGray","DarkGreen","DarkMagenta","DarkRed","DarkYellow","Gray","Green","Magenta","Red","White","Yellow")]
[String[]]$Color = "Green",
[Parameter(Mandatory=$false,
Position=2)]
[String]$LogFile,
[Parameter(Mandatory=$false,
Position=3)]
[Switch]$NoNewLine
)
if ($String.Count -gt 1) {
$i=0
foreach ($item in $String) {
if ($Color[$i]) { $col = $Color[$i] } else { $col = "White" }
Write-Host "$item " -ForegroundColor $col -NoNewline
$i++
}
if (-not ($NoNewLine)) { Write-Host " " }
} else {
if ($NoNewLine) { Write-Host $String -ForegroundColor $Color[0] -NoNewline }
else { Write-Host $String -ForegroundColor $Color[0] }
}
if ($LogFile.Length -gt 2) {
"$(Get-Date -format "yyyy.MM.dd hh:mm:ss tt"): $($String -join " ")" | Out-File -Filepath $Logfile -Append
} else {
Write-Verbose "Log: Missing -LogFile parameter. Will not save input string to log file.."
}
}
if (-not (Test-Path "$PSScriptRoot\logs")) { New-Item "$PSScriptRoot\logs" -Type Directory -Force -Confirm:$false | Out-Null }
$uri = 'https://outlook.office.com/webhook/f17755df-0568-4967-983d-d65d281417ca@94c27a8c-c13f-42c3-a519-bb453aa966a2/IncomingWebhook/c27bcf55ca784a61aa0518e778698356/4317989c-ce6a-4a49-aa83-8b8be0e04e6c'
$date = Get-Date -format yyyy-MM-dd_hh-mm-sstt
$body =@{ text = "Starting Data Migrations Scripts on WCI-FILE at $date"} | ConvertTo-Json
Invoke-RestMethod -uri $uri -Method Post -body $body -ContentType 'application/json'
# Define sources
$source = '\\10.2.1.243\pVault\Documents'
$destination = "e:\pvault\documents"
$body =@{ text ="Copying folder $source to $destination"} | ConvertTo-Json
Invoke-RestMethod -uri $uri -Method Post -body $body -ContentType 'application/json'
log 'Copying folder',$source,'to',$destination Green,Cyan,Green,Cyan $LogFile
start-job {ROBOCOPY /xj /r:0 /w:0 /it /mir /copyall /np /tee /zb /log+:$using:RoboLog $using:source $using:destination}
$source = '\\10.2.1.243\BeetleJuice\Financials'
$destination = 'e:\Financials'
$body =@{ text ="Copying folder $source to $destination"} | ConvertTo-Json
Invoke-RestMethod -uri $uri -Method Post -body $body -ContentType 'application/json'
log 'Copying folder',$source,'to',$destination Green,Cyan,Green,Cyan $LogFile
start-job {ROBOCOPY /xj /r:0 /w:0 /it /mir /copyall /np /tee /zb /log+:$using:RoboLog $using:source $using:destination}
$source = '\\10.2.1.243\Greenlight'
$destination = "e:\greenlight"
$body =@{ text ="Copying folder $source to $destination"} | ConvertTo-Json
Invoke-RestMethod -uri $uri -Method Post -body $body -ContentType 'application/json'
log 'Copying folder',$source,'to',$destination Green,Cyan,Green,Cyan $LogFile
start-job {ROBOCOPY /xj /r:0 /w:0 /it /mir /copyall /np /tee /zb /log+:$using:RoboLog $using:source $using:destination}
$source = '\\10.2.1.93\jdtemp\Wells 4-5-17'
$destination = "e:\jdtemp\wells 4-15-17"
$body =@{ text ="Copying folder $source to $destination"} | ConvertTo-Json
Invoke-RestMethod -uri $uri -Method Post -body $body -ContentType 'application/json'
log 'Copying folder',$source,'to',$destination Green,Cyan,Green,Cyan $LogFile
start-job {ROBOCOPY /xj /r:0 /w:0 /it /mir /np /tee /zb /log+:$using:RoboLog $using:source $using:destination}
## Display Job Output
# Use get-job #jobid for more detil during operations
# $jobs | wait-job
# $jobs | recieve-job - Recieve command execution result
@milnak
Copy link

milnak commented Jan 22, 2024

# $jobs | recieve-job - Recieve command execution result

it's commented out, but that should be 'receive-job -receive'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment