I'm using net use
command in batch file to connect a remote location.
I want to store the output of it into a variable and process it.
When the command completes successfully, my code works fine.
However, if there is some error, like wrong password, then I'm not able to store the error output in a variable. It directly prints to the console window in which script is running. Please help with the following:
- Why does this happen?
Why I'm unable to store output in variable when error occurs in the command output? - Is there a suitable way to log ERROR output of the 'net use' command?
Following is my code:
:connNEWlocation
echo Connecting required remote location...
for /f "tokens=1,2,3,4,5,6,7,8 delims=*" %%a IN ('net use \\!hostaddress!\!user! /user:!user! !user_pass!') DO (
if "%%a"=="The command completed successfully." (
echo Connected successfully^!
) else (
echo ERROR:"%%a"
echo do something based on error obtained here
)
)