Every command or script returns with the status of execution, which is referred as return status or exit codes. A successful command returns a 0 while an unsuccessful one returns a non-zero value that usually can be interpreted as an Error Code. The last command executed in the function or the script determines the exit status. This document provides steps on how to return the error codes on .vb scripts, Powershell scripts and batch files.
Use the command EXIT /B %ERRORLEVEL% at the end of the batch file to return the error codes from the batch file
In the batch file , it is always a good practice to use environment variables instead of constant values. Since the same variable get expanded to different values on different computers.
Example:
Batch file for Copying File to a Folder
md "C:manageengine"
copy "\\sharename\foldername\samplefile.txt" "C:\manageengine"
exit /b %ERRORLEVEL%
Use the command Exit $LASTEXITCODE at the end of the powershell script to return the error codes from the powershell script.
Example:
Powershell script for copying file to a folder
$dest ="C: est"
New-Item $dest -type directory -force
$source ="c:samplefile.txt"
Copy-Item $source $dest
exit $LASTEXITCODE
Use wscript.quit Err.Number at the end of the vb script which will return the last error codes from the script
Example:
vb script for Copying File to a Folder
dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
If filesys.FileExists("c:\samplefile.txt") Then
filesys.CopyFile "c:\samplefile.txt", "C:\manageengine"
End If
wscript.quit Err.number