2013-05-14 67 views
2

即使if匹配(並且也被處理),批處理腳本處理else語句。爲什麼是這樣?當if語句匹配時,爲什麼我的批處理腳本同時運行if和else語句?

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 
+0

這是'else'語句還是隻是一個? – ChrisF

回答

2

這可能與你想呼應做:(將顯示在明文)

可以逃脫字符^(和^)

+0

這很有道理。謝謝! – EpicCyndaquil

0

我會重寫它像這樣(請將腳本的後半部分留給你完成):

@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