Posted on 2009-08-12 21:16
S.l.e!ep.¢% 阅读(562)
评论(1) 编辑 收藏 引用 所属分类:
DOS
@echo off & SetLocal EnableDelayedExpansion
set "self=%~0"
if /i "%~1"=="ExitCode" goto :EXIT
if /i "%~1"=="" echo 现在是测试运行
if /i "%~1"=="WIN_Exit" echo 貌似我又复活了
echo start "" "%~0" WIN_Exit>ErrExit.cmd
call :查找进程
call :FINDEXISTPID
call :守护进程
start Check.vbs
:REINPUT
echo -----------------------------------------
echo 输入:ExitCode 完全退出程序
echo -----------------------------------------
set /p "Input=请输入入口代码:"
if /i "%Input%"=="ExitCode" goto :EXITPROCESS_OK
goto :REINPUT
:查找进程
echo for each ps in getobject _>ps.vbs
echo ^("winmgmts:\\.\root\cimv2:win32_process"^).instances_ >>ps.vbs
echo if ps.name ="cmd.exe" Then wscript.echo ps.handle^&vbtab^&ps.name>>ps.vbs
echo next>>ps.vbs
goto :EOF
:FINDEXISTPID
if exist ExistPid.txt del ExistPid.txt
for /f "tokens=1,* " %%m in ('cscript //nologo ps.vbs') do echo %%m>>ExistPid.txt
for /f "tokens=1,* " %%m in ('cscript //nologo ps.vbs') do (
type ExistPid.txt | find /i "%%m">nul && set "Pid=%%m"
)
goto :EOF
:守护进程
echo set Cleaner=createobject^("wscript.shell"^)>Check.vbs
echo do>>Check.vbs
echo WScript.Sleep 100>>Check.vbs
echo if not isProcessExists^("%Pid%"^) then>>Check.vbs
echo Cleaner.run "ErrExit.cmd">>Check.vbs
echo Exit do>>Check.vbs
echo End if>>Check.vbs
echo loop>>Check.vbs
echo Function isProcessExists^(proName^)>>Check.vbs
echo Set objWMIService = GetObject^("winmgmts:" ^& "{impersonationlevel=impersonate}^!\\.\root\cimv2"^)>>Check.vbs
echo Set colProcessList = objWMIService.ExecQuery^("Select * from Win32_Process Where Handle='" ^& proName ^& "'"^)>>Check.vbs
echo isProcessExists = ^(colProcessList.Count ^<^> 0^)>>Check.vbs
echo End Function>>Check.vbs
goto :EOF
:EXITPROCESS_OK
echo for each ps in getobject _>ps.vbs
echo ^("winmgmts:\\.\root\cimv2:win32_process"^).instances_ >>ps.vbs
echo if ps.name ="WScript.exe" Then wscript.echo ps.handle^&vbtab^&ps.name>>ps.vbs
echo next>>ps.vbs
for /f "tokens=1,* " %%i in ('cscript //nologo ps.vbs') do ntsd -c q -p %%i
echo del ps.vbs>>deltemp.cmd
echo del check.vbs>>deltemp.cmd
echo del ErrExit.cmd>>deltemp.cmd
echo del ExistPid.txt>>deltemp.cmd
echo del run.vbs>>deltemp.cmd
echo del deltemp.cmd>>deltemp.cmd
echo set Cleaner=createobject^("wscript.shell"^)>>run.vbs
echo Cleaner.run "cmd /c deltemp.cmd",^0>>run.vbs
start run.vbs
:EXIT
exit