Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, May 11, 2017 7:16 AM
I am using PowerShell with Robocopy using following command. It seems to be working without any issue here.
I am basically copying data from FIle Server map drive's folder to user local machine.
Is there any way to check if network is disconnected or offline then script should terminate automatically ?
I also want to know once I script start it should start from those file where it was terminated?
foreach ( $folder123 in $SourceFolder)
{
robocopy $($folder123.FullName) $pathDir /mov $emptyFolder /z /w:1 /r:0 /tee /np /XO /xd $folderIgnoreList /xf "desktop.ini" /log+:$copyLog /v
)
Albert
All replies (12)
Thursday, May 11, 2017 7:24 AM
To check Mapped drive status use below under foreach may be using a check.
Get-PSDrive -PSProvider FileSystem
And Robocopy has built in feature to skip the already copied files and start copying new items or item that has changed.
Thanks
Roushan
Thursday, May 11, 2017 7:32 AM
Roushan
What will happen if you are copying so many files lets say 2000-300 files and some of them are large files and during copy process if network got disconnected or offline, how do we handle this situation?
Albert
Thursday, May 11, 2017 8:11 AM
Probably those will fail and start once copy process restarts. Have you came across any issues?
Regards
Roushan
Thursday, May 11, 2017 8:14 AM
Albert,
that's not a scripting issue anymore then. You can set robocopy to try it again and again and you can set the number of retries ( option /r ) and you can set the waiting time between these retries ( option /w )
And BTW: You could check the availablity of the source or target for robocopy also with Test-Path.
Grüße - Best regards
PS:> (79,108,97,102|%{[char]$_})-join''
Thursday, May 11, 2017 8:25 AM
I am already checking offline mode and network connection thru code before for-each loop.
Some users are facing issue during copy process network disconnected and robocopy keep continuing until it is not completed. This is scenario where I want to log error and terminate the script.
Thursday, May 11, 2017 8:36 AM
So you could evaluate the return code of robocopy and act accordingly or you could parse the log file.
BTW: you don't need a mapped network drive to use robocopy you can use UNC-path to specify source or target.
Grüße - Best regards
PS:> (79,108,97,102|%{[char]$_})-join''
Thursday, May 11, 2017 8:52 AM
Return code only returns once robocopy process is completed that we can do it using try catch. We can not get the code during robocopy process and if there is any connectivity issue.
Secondly I am using UNC path only not using mapping network drive name.
Thursday, May 11, 2017 8:59 AM
Return code only returns once robocopy process is completed that we can do it using try catch. We can not get the code during robocopy process and if there is any connectivity issue.
That's right. But robocopy means "robust file copy". Usually it does not "fail" when it is not able to copy one or more files. And it will not throw a powershell error. So you are limited to the exit code robocopy returns when it finishes.
Secondly I am using UNC path only not using mapping network drive name.
Great ... thumbs up! ;-)
Grüße - Best regards
PS:> (79,108,97,102|%{[char]$_})-join''
Thursday, May 11, 2017 9:14 AM
As you suggested, I am checking the return code using following code
# Check exit code
If (($LASTEXITCODE -eq 0))
{
$RoboCopyMessage = "EXITCODE: 0, Succeeded"
}
elseif (($LASTEXITCODE -gt 0) -and ($LASTEXITCODE -lt 16))
{
$RoboCopyMessage = "EXITCODE: 1-15, Warning"
}
elseif ($LASTEXITCODE -eq 16)
{
$RoboCopyMessage = "EXITCODE: 16, Error"
}
else
{
$RoboCopyMessage = "Robocopy did not run"
}
Thursday, May 11, 2017 9:35 AM
In addition to my earlier post I tried try-catch, Now I can see the error if robocopy fails now depending on error code I can add my custom error message in custom admin log and terminate the code.
Let me know if try-catch approach is correct or not.
Thursday, May 11, 2017 10:11 AM | 1 vote
I cannot confirm at the moment but I guess try catch will not work properly with "external" (non Powershell) commands. That's what I meant with "limited to exit codes of robocopy". But if it works for you ... even better. ;-)
Anyway you could shorten your error message creation a little with a switch statement:
switch ($LASTEXITCODE) {
0 { $RoboCopyMessage = 'EXITCODE: 0, Succeeded'}
{$_ -gt 0 -and $_ -le 8} { $RoboCopyMessage = 'EXITCODE: 1-15, Warning' }
{$_ -gt 8} { $RoboCopyMessage = 'EXITCODE: > 8, Error' }
default { $RoboCopyMessage = 'Unknown Error' }
}
BTW: Microsoft considers all exit codes higher than 8 as an error ... see this: Return Codes of Robocopy
Grüße - Best regards
PS:> (79,108,97,102|%{[char]$_})-join''
Thursday, May 11, 2017 10:35 AM
RoboCopy will retry the file until the network returns if you option it to do so. It can be set to retry forever or for a number of retries at a specific interval.
If yuo use /MIR then you can retry the command and it will pick up the missing files.
This is not a RoboCopy forum.
\(ツ)_/