Link to home
Start Free TrialLog in
Avatar of bbimis
bbimis

asked on

how do you determine if a cmd shell executed successful in vb.net

if you run a command prompt to execute say psexec, how can you determine if the error code was 0 or not?
i tried
error1="cmd.exe /c ....."
if error1 <>0 then
 msgbox("there was an error")

Open in new window


it doesn't work though so again how do i determine if a cmd shell executed successfully?
Avatar of N8iveIT
N8iveIT
Flag of United States of America image

During testing ... if you either (1) put a "pause" at the end of the CMD commands or (2) put something like "echo CMDScriptRan > c:\ItRan.txt" at the end of the CMD commands.

This should tell you if it is executing the CMD script(s). I would probably do (2) then delete the file and remove the echo line from the CMD lines, once it seems to be working correctly.
Avatar of bbimis
bbimis

ASKER

well i was wanting to capture the error code if possible. if error is 0 then it was successful if not 0 then it failed
sample batch file that <--- means brief description of action being taken

Echo Off
cls
cd C:\MYBATS  <--- directory where i store my batch file"
net use z: <---"create a map to one of my server shares"
echo Copying started at %date% %time%>>RunTimes.txt <--- notice the date / time stamp being written to a text file
copy z:\*.* /y "copy all my files from mapped drive to the local drive" <--- begin copying files
echo Copying finished at %date% %time%>>RunTimes.txt <--- another date / time stamp
echo Completed Successfully at %date% %time%>>RunTimes.txt <--- final date / time stamp
echo "">>RunTimes.txt <---just writes a blank line to file to separate entries
net use z: /del <--- delete the server share

You end up with a RUNTIMES.TXT file and the following information:

Copying started at Sat 05/05/2018 21:00:01.55
Copying finished at Sat 05/05/2018 21:01:44.30
Completed Successfully at Sat 05/05/2018 21:01:44.30
""
Copying started at Sun 05/06/2018 21:00:01.87
Copying finished at Sun 05/06/2018 21:01:24.45
Completed Successfully at Sun 05/06/2018 21:01:24.45
""
Copying started at Mon 05/07/2018 21:00:01.50
Copying finished at Mon 05/07/2018 21:01:36.87
Completed Successfully at Mon 05/07/2018 21:01:36.87
""
Copying started at Tue 05/08/2018 21:00:01.32
Copying finished at Tue 05/08/2018 21:01:21.92
Completed Successfully at Tue 05/08/2018 21:01:21.92
""
Copying started at Wed 05/09/2018 21:00:01.66
Copying finished at Wed 05/09/2018 21:01:22.18
Completed Successfully at Wed 05/09/2018 21:01:22.18

and so on and so on....
I did some testing then research. Learn something new every day!

This may assist what you are wanting to confirm (see https://blogs.msdn.microsoft.com/oldnewthing/20080926-00/?p=20743 ); specifically his comments at the end "So you can perform other types of tests against the error level, for example, to perform an equality test:
IF %ERRORLEVEL% EQU 1 echo Different!" ... plus others comments on the post itself.

They should give you some ideas how to determine the errorlevel, depending on the complexity of what you are running.
Avatar of bbimis

ASKER

so how do you do it in vb.net?
ASKER CERTIFIED SOLUTION
Avatar of N8iveIT
N8iveIT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bbimis

ASKER

This gave me what i needed. Thanks for the help.