1. 程式人生 > >呼叫區域網WINDOWS主機的WMIC介面批量執行VBS指令碼

呼叫區域網WINDOWS主機的WMIC介面批量執行VBS指令碼

cmdEXEC.bat的內容:

FOR /F "delims=, tokens=1,2,3" %%i in (C:\tmps\pwd.txt) do C:\tmps\bat.bat %%i %%j %%k

pwd.txt包含:IP地址,使用者名稱,密碼,以逗號分隔,每一臺主機換行。

bat.bat的內容:

@set ip=%1
@set uname=%2
@set upwd=%3
wmic /node:%ip% /user:%uname% /password:%upwd% os >nul || goto :sb
dir v:\ >nul 2>&1 && goto :vsb
wmic /node:%ip% /user:%uname% /password:%upwd% process call create "cmd /c mkdir c:\tmpss"
wmic /node:%ip% /user:%uname% /password:%upwd% share call create "","temp description","","tmps","","c:\tmpss",0
net use v: \\%ip%\tmps %upwd% /user:%uname%
copy c:\tmps\96c221be-6ab2-ef53-1589-fe16877914ca.vbs v:\
wmic /node:%ip% /user:%uname% /password:%upwd% process call create "cmd /c cscript 96c221be-6ab2-ef53-1589-fe16877914ca.vbs","c:\tmpss"
@set slptimes=1
goto :sleep

:sleep
set /a slptimes+=1
if %slptimes% gtr 60 (echo exceeded 5 min && goto :eof)
@echo ---------------- waiting %slptimes% ----------------
@ping -n 5 localhost>nul
dir v:\*96c221be-6ab2-ef53-1589-fe16877914ca*xml >nul 2>&1 || goto :sleep
dir v:\*96c221be-6ab2-ef53-1589-fe16877914ca*xml >nul 2>&1 && goto :getfile

:getfile
rem dir v:\
copy v:\*96c221be-6ab2-ef53-1589-fe16877914ca*xml c:\tmps\
goto :delshare

:delshare
net use v: /delete
wmic /node:%ip% /user:%uname% /password:%upwd% share where name="tmps" call delete
wmic /node:%ip% /user:%uname% /password:%upwd% process call create "cmd /c del /Q c:\tmpss"
goto :eof

:sb
@echo =================================== Can't connect to %ip%===================================
goto :eof

:vsb
@echo =================================== Can't use v:\ %vnode%===================================
goto :eof

:eof

注意:96c221be-6ab2-ef53-1589-fe16877914ca.vbs是要執行的指令碼

由於WINDOWS2003無法通過WIMC建立可寫的共享,可考慮換種方式如用預設共享等方式拷貝.VBS檔案,

如net use v: \\%ip%\c$\tmpss %upwd% /user:%uname%

WINDOWS2008則可以通過WMIC建立可寫共享

雙擊cmdEXEC.bat,即可一次在多臺WINDOWS主機上面執行指令碼,不用依賴WINDOWS的域環境和組策略