question

JonathanWoods-5093 avatar image
0 Votes"
JonathanWoods-5093 asked IanXue-MSFT commented

Robocopy to mapped drives Powershell

Hello all. I'm trying to create a script that will run on an AWS instance with local drives that will copy each drive to a corresponding AWS FSX drive that is also mapped to the local machine. If I create the script in PS ISE and run, things seem to process as expected, but when I create a Scheduled job it never runs.

Any idea's?

$directories = @(
@{"Source"="H:\";"Destination"="W:\D$";"Log"="C:\devops\logs\robocopy\$((Get-Date -Format MMddyy_hhmmss))_coursesdev.log"}
@{"Source"="I:\";"Destination"="X:\D$";"Log"="C:\devops\logs\robocopy\$((Get-Date -Format MMddyy_hhmmss))_courseslive.log"}
@{"Source"="J:\";"Destination"="Y:\D$";"Log"="C:\devops\logs\robocopy\$((Get-Date -Format MMddyy_hhmmss))_companyfiles.log"}
@{"Source"="K:\";"Destination"="Z:\D$";"Log"="C:\devops\logs\robocopy\$((Get-Date -Format MMddyy_hhmmss))_othercontent.log"}

 )

foreach($directory in $directories){
$scriptBlock = {
Param($SourceDir, $DestDir, $LogLocation)
robocopy $SourceDir $DestDir /xd RECYCLE.BIN "System Volume Information" /mir /W:1 /R:1 /MT:24 /log:$LogLocation
}
Start-Job -ScriptBlock $scriptBlock -ArgumentList $directory.Source, $directory.Destination, $directory.Log
}

windows-server-powershell
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,
Is there any update? Have you got a chance to verify the below reply?
Please feel free to let us know if more assistance is needed.
If the reply is helpful, please “Accept Answer” to help other community members find it more easily.

0 Votes 0 ·

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

Hi,

How did you create the scheduled task? Per my test, the powershell process is terminated when the script finishes running and this makes the background be stopped.
You could add the "-noexit" argument to powershell in the scheduled task, or append "Wait-Job" to the "Start-Job" line.

 Start-Job -ScriptBlock $scriptBlock -ArgumentList $directory.Source, $directory.Destination, $directory.Log | Wait-Job

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.