robocopy can be a bit fiddly in powershell. In case you are interested, this is how I do it.
Code:
$backupDrive='E'
$backupLocation=$backupDrive+':\'+$env:computername+' Backup'
$backupDirectories = @(
,('D:\Music','*.*')
,('C:\Users\XXXX\Documents\Calibre Library','*.*')
,('C:\Users\XXXX\OneDrive','*.*')
,('C:\Users\YYYY\OneDrive','*.*')
)
# Check if dock connected
if (Test-Path $backupLocation) {
For($i=0;$i -lt $backupDirectories.Count; $i++) {
$source= $backupDirectories[$i][0]
$files= $backupDirectories[$i][1]
if(Test-Path $source) {
$sourceDrive=(get-item $source).PSDrive.Name
$unqualifiedPath=split-path -noqualifier $source
$destination="$backupLocation\$sourceDrive$unqualifiedPath"
$logfile="""$backupLocation\Backup Logs\Backup $(get-date -f yyyy-MM-dd' 'HH-mm-ss-fffff) $(split-path -leaf $source).log"""
# Robocopy options
$copyOptions = @('/B','/COPYALL','/MIR')
$selectionOptions = @()
$retryOptions = @('/R:0','/W:0')
$loggingOptions = @('/NFL','/NDL',"/LOG:$logfile",'/TEE')
$jobOptions = @()
$CmdLine = @('"{0}"' -f $source) +@('"{0}"' -f $destination) +@($files) `
+$copyOptions +$selectionOptions +$retryOptions +$loggingOptions +$jobOptions
& 'robocopy.exe' $CmdLine
}
}
}
read-host