Robocopy returns error 16 in a batch file if run through PowerShell

0

I am working on a PowerShell script file parent.ps1 that calls 4 other batch files batchFile1 through batchFile4 in order. The next batchFile only runs if the previous was successful.
The 3rd and 4th batch file use robocopy that copies some files from a local drive to a network drive.

Both batch files as well as the PowerShell script have same permissions, however when I run the PowerShell script, either directly or through task scheduler, it fails on 3rd batch file when it reaches the robocopy with error level 16.

ERROR 67 (0x00000043) Getting File System Type of Destination
\\SERVER\DATA\SHARED\DESTINATION\
The network name cannot be found.

On the other hand, if I run batchFile3 and batchFile4 directly or through the task scheduler, robocopy works fine without any issues and copies over the files as intended.

This is my powershell code that is calling the batch file:

#call the third batch file
Write-Log INFO "Calling batch file C:\JOBS\batFile3.bat" $logfile
C:\JOBS\batchFile3.bat
Write-Log INFO "Execution of C:\JOBS\batchFile3.bat complete" $logfile
Write-Log INFO "Last Exit Code is $LASTEXITCODE" $logfile

#abort Powershell if third batch fails and send email to support group
if ($LASTEXITCODE -ne 0) {
    $EmailBody = "Execution of batchFile3.bat failed with error message $LASTEXITCODE. Examine the logs and fix any issues before the next run.`n
Logs are located at C:/JOBS/logs/`n`nAt your service,`nAdmin"

    Write-Log WARN "Sending Alert Email of failure" $logfile
    Send-MailMessage -From $FromEmail -To $Recipient -Subject $Subject -Body $EmailBody

    Write-Log FATAL "Exiting Powershell script because C:\JOBS\batchFile3.bat failed" $logfile
    exit
}

This is the code that is performing robocopy in batchFile3 (and batchFile4):

robocopy %BCP_Queryout_Folder% \\SERVER\DATA\SHARED\DESTINATION\ fileToCopy-%file_date%.txt /is /it /copyall
echo .
echo Errorlevel_Value: %ERRORLEVEL% (Error level of 1 means robocopy was successful)
set "FileCopy_errorlevel=%ERRORLEVEL%"
if %ERRORLEVEL% EQU 1 (set "FileCopy_errorlevel=0")
if %ERRORLEVEL% EQU 0 (set "FileCopy_errorlevel=1")
echo FileXcopy_errorlevel: %FileCopy_errorlevel%
echo.
echo Process time: %date% - %time%
EXIT /B %FileCopy_errorlevel%

What could be causing robocopy to fail when the batch files are called through PowerShell script?

powershell
batch-file
batch-processing
robocopy
asked on Stack Overflow Apr 17, 2019 by mandyallstars • edited Apr 18, 2019 by mandyallstars

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0