Thank you for reading. I am trying to create a batch robocopy Powershell script so I don’t have to manually robocopy multiple folders, just pass in the parameters, however I’m getting positional based errors and am having trouble understanding what I did wrong.
The Powershell script:
# Use arguments and pass into command
param (
[Parameter(Mandatory=$true)]
[string[]]$sources,
[Parameter(Mandatory=$true)]
[string[]]$destinations,
# Define robocopy options, if nothing's passed in as the parameter this is the default
[Parameter(Mandatory=$false)]
[string[]]$robocopyOptions = @('/MIR', '/COPYALL', '/DCOPY:DAT', '/R:3', '/W:5', '/LOG+:robocopy.log')
)
if ($sources.Count -eq 0) {
Write-Host "Error: There are no source paths."
exit
}
if ($destinations.Count -eq 0) {
Write-Host "Error: There are no destination paths."
exit
}
# Verify that the number of source and destination paths match
if ($sources.Count -ne $destinations.Count) {
Write-Host "Error: Number of source paths does not match the number of destination paths."
exit
}
# Loop through each source-destination pair and run robocopy
for ($i = 0; $i -lt $sources.Count; $i++) {
$source = $sources[$i]
$destination = $destinations[$i]
# Check if source and destination paths exist
if (-not (Test-Path $source)) {
Write-Host "Source path '$source' does not exist. Skipping this iteration."
continue
}
if (-not (Test-Path $destination)) {
Write-Host "Destination path '$destination' does not exist. Skipping this iteration."
continue
}
Write-Host "Copying from $source to $destination"
# Run robocopy
robocopy $source $destination $robocopyOptions
Write-Host $source $destination $robocopyOptions
}
Write-Host "All copy operations completed."
First command I used to try to see if I can have multiple sources. I run this in cmd
as administrator.
powershell.exe -File "D:\OTHER\TỰ ĐỘNG HÓA\BATCH ROBOCOPY.ps1" -sources "D:\OTHER\TỰ ĐỘNG HÓA\BATCH ROBOCOPY A", "D:\OTHER\TỰ ĐỘNG HÓA\BATCH ROBOCOPY C" -destinations "D:\OTHER\TỰ ĐỘNG HÓA\BATCH ROBOCOPY B", "D:\OTHER\TỰ ĐỘNG HÓA\BATCH ROBOCOPY D"
D:\OTHER\TỰ ĐỘNG HÓA\BATCH ROBOCOPY.ps1 : A positional parameter cannot be found that accepts argument
'D:\OTHER\TỰ ĐỘNG HÓA\BATCH ROBOCOPY C'.
+ CategoryInfo : InvalidArgument: (:) [BATCH ROBOCOPY.ps1], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : PositionalParameterNotFound,BATCH ROBOCOPY.ps1
I thought it might have had something to do with me not writing the arguments correctly because I had spaces between my elements in the array?
powershell.exe -File "D:\OTHER\TỰ ĐỘNG HÓA\BATCH ROBOCOPY.ps1" -sources "D:\OTHER\TỰ ĐỘNG HÓA\BATCH ROBOCOPY A","D:\OTHER\TỰ ĐỘNG HÓA\BATCH ROBOCOPY C" -destinations "D:\OTHER\TỰ ĐỘNG HÓA\BATCH ROBOCOPY B","D:\OTHER\TỰ ĐỘNG HÓA\BATCH ROBOCOPY D"
Source path 'D:\OTHER\TỰ ĐỘNG HÓA\BATCH ROBOCOPY A,D:\OTHER\TỰ ĐỘNG HÓA\BATCH ROBOCOPY C' does not exist. Skipping this iteration.
All copy operations completed.
A different command I tried without multiple sources and destinations and trying to pass in parameters for robocopyOptions
, got the same positional error but for a different place.
powershell.exe -File "D:\OTHER\TỰ ĐỘNG HÓA\BATCH ROBOCOPY.ps1" -sources "D:\OTHER\TỰ ĐỘNG HÓA\BATCH ROBOCOPY A" -destinations "D:\OTHER\TỰ ĐỘNG HÓA\BATCH ROBOCOPY B" -robocopyOptions "/SEC", "/MOVE", "/E"
D:\OTHER\TỰ ĐỘNG HÓA\BATCH ROBOCOPY.ps1 : A positional parameter cannot be found that accepts argument '/MOVE,'.
+ CategoryInfo : InvalidArgument: (:) [BATCH ROBOCOPY.ps1], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : PositionalParameterNotFound,BATCH ROBOCOPY.ps1
I looked at Powershell: A positional parameter cannot be found that accepts argument “xxx” but still don’t really understand the issue because I did put the parameter name?
I was also looking at Powershell with Robocopy and Arguments Passing - Stack Overflow too, and from my understanding, to pass in robocopy switches, the best way to do it is to make an array of separate values, and that’s why I made robocopyOptions
an array.
Passing in only one source and one destination without passing in arguments for robocopyOptions
works. So am I doing something wrong with my arrays? I don’t get what I could be doing wrong there because I thought you’re supposed to just separate the arguments by commas.