Passing CMD Results to Variable in a Batch File

Posted by TripleNad on Stack Overflow See other posts from Stack Overflow or by TripleNad
Published on 2012-10-02T15:33:38Z Indexed on 2012/10/02 15:37 UTC
Read the original article Hit count: 180

Filed under:
|
|

I am trying to install an application and a group of services using PSTools, but I want to take into account that the computer I am trying to connect to may be turned off or on a different network, which is not reachable from the internal network.

Basically if the machine is not able to be accessed through the admin share, this is the message that I am getting:

Couldn't access MachineName:

The network path was not found.

Make sure that the default admin$ share is enabled on MachineName.

This is the syntax I am using to try to capture the "Error Message" and then report back that if installation was successful or not (depending on if it can contact the machine)

@echo off

set /p name=What is the machine name?: 

psexec.exe \\\%name% -u *useraccount* -p *password* \\\ServerName\installation.bat

FOR /F "tokens=*" %%A IN ('COMMAND ^| FIND "Couldn't access"') DO SET Error=%%A

If "%Error%"=="Couldn't access"

ECHO Installation Failed.

Else

ECHO Installtion complete.  

Pause

exit

Currently it hangs right at the point it's defining the Error Variable. Can't quite figure out what I am going wrong here.

© Stack Overflow or respective owner

Related posts about variables

Related posts about scripting