5

The batch script processes the else statement even though the if matches (and is processed as well). Why is this?

set getprocesslistlocal=wmic process get name,processid
echo Type the name of the remote machine to view processes of (or type local for local machine), and press Enter.
set /P remotemachine=
if %remotemachine%==local (
%getprocesslistlocal%
) else (
echo Type the user name to access %remotemachine% with, then press Enter.
set /P remoteuser=
echo Type the password for %remoteuser% on %remotemachine%, then press Enter. (Will be displayed in plaintext)
set /P remotepassword=
set getprocesslistremote=wmic /node %remotemachine% /user:%remoteuser% /password:%remotepass% process get name,processid
%getprocesslistremote%
)
echo End of list.
pause
echo Type the process id and hit Enter.
set /P killid=
if %remotemachine%==local (
wmic process where processid="%killid%" call terminate
) else (
wmic /node %remotemachine% /user:%remoteuser% /password:%remotepass% process where processid="%killid%" call terminate
)
echo Process id %killid% terminated. Press Enter to exit.
pause
1
  • Is it both else statements or just one? Commented May 14, 2013 at 22:45

2 Answers 2

6

It may have to do with you trying to echo: (Will be displayed in plaintext)

You can escape the characters with ^( and ^)

Sign up to request clarification or add additional context in comments.

1 Comment

That makes perfect sense. Thank you!
0

I would rewrite it like so ( leave the second half of the script for you to finish):

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set /P machine=Type the name of the remote machine to view processes of [local]: 
if "%machine%"=="" (
  set machine=local
  set "getprocesslist=wmic.exe"
) else (
  set "getprocesslist=wmic.exe /node %machine%"
)


echo %getprocesslist%

set /P user=Type the user name to access %machine% with: 
if not "%user%"=="" SET "getprocesslist=%getprocesslist% /user:%user%
set /P password=Type the password for %remoteuser% on %machine% [displayed in plaintext]: 
if not "%password%"=="" SET "getprocesslist=%getprocesslist% /password:%pass%"

SET "getprocesslist=%getprocesslist% process get name,processid"

ECHO cmd.exe /c %getprocesslist%
cmd.exe /c %getprocesslist%

pause

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.