Skip to content

Commit 23083dd

Browse files
authored
Robocopy hotfix (#4837)
1 parent f7e3842 commit 23083dd

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

Tasks/PublishBuildArtifacts/Invoke-Robocopy.ps1

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ $writer = New-Object System.IO.StreamWriter($stdout, $utf8)
3535
# All subsequent output must be written using [System.Console]::WriteLine(). In
3636
# PowerShell 4, Write-Host and Out-Default do not consider the updated stream writer.
3737

38-
# Print the ##command.
39-
[System.Console]::WriteLine("##[command]robocopy.exe /E /COPY:DA /NP /R:3 /MT:$ParallelCount `"$Source`" `"$Target`" *")
38+
# Print the ##command. The /MT parameter is only supported on 2008 R2 and higher.
39+
if ($ParallelCount -gt 1) {
40+
[System.Console]::WriteLine("##[command]robocopy.exe /E /COPY:DA /NP /R:3 /MT:$ParallelCount `"$Source`" `"$Target`" *")
41+
} else {
42+
[System.Console]::WriteLine("##[command]robocopy.exe /E /COPY:DA /NP /R:3 `"$Source`" `"$Target`" *")
43+
}
4044

4145
# The $OutputEncoding variable instructs PowerShell how to interpret the output
4246
# from the external command.
@@ -59,15 +63,30 @@ $OutputEncoding = [System.Text.Encoding]::Default
5963
#
6064
# Note, the output from robocopy needs to be iterated over. Otherwise PowerShell.exe
6165
# will launch the external command in such a way that it inherits the streams.
62-
& robocopy.exe /E /COPY:DA /NP /R:3 /MT:$ParallelCount $Source $Target * 2>&1 |
63-
ForEach-Object {
64-
if ($_ -is [System.Management.Automation.ErrorRecord]) {
65-
[System.Console]::WriteLine($_.Exception.Message)
66+
#
67+
# Note, the /MT parameter is only supported on 2008 R2 and higher.
68+
if ($ParallelCount -gt 1) {
69+
& robocopy.exe /E /COPY:DA /NP /R:3 /MT:$ParallelCount $Source $Target * 2>&1 |
70+
ForEach-Object {
71+
if ($_ -is [System.Management.Automation.ErrorRecord]) {
72+
[System.Console]::WriteLine($_.Exception.Message)
73+
}
74+
else {
75+
[System.Console]::WriteLine($_)
76+
}
6677
}
67-
else {
68-
[System.Console]::WriteLine($_)
78+
} else {
79+
& robocopy.exe /E /COPY:DA /NP /R:3 $Source $Target * 2>&1 |
80+
ForEach-Object {
81+
if ($_ -is [System.Management.Automation.ErrorRecord]) {
82+
[System.Console]::WriteLine($_.Exception.Message)
83+
}
84+
else {
85+
[System.Console]::WriteLine($_)
86+
}
6987
}
70-
}
88+
}
89+
7190
[System.Console]::WriteLine("##[debug]robocopy exit code '$LASTEXITCODE'")
7291
[System.Console]::Out.Flush()
7392
if ($LASTEXITCODE -ge 8) {

0 commit comments

Comments
 (0)