批处理二三例

一、SchTasks

利用SchTasks,我们可以在本机或远程服务器上安排计划任务,这在需要自动化执行多台服务器上的任务、减轻繁琐工作时相当有用,下面给出我使用的批处理代码供大家参考:

REM Global variable

set logfile=schedule.txt

set /p choose=Please choose (1: for "Close Menu for Account", 2: for "Open Menu for Account"):



if "%choose%"=="1" goto CLOSE 

if "%choose%"=="2" goto OPEN



goto EXIT



:CLOSE

echo ... Close Menu for Account ...

echo Start schedule at %DATE% %TIME% > %logfile% 



set schedule_dt=%date:~0,4%/%date:~5,2%/%date:~8,2%

echo Default schedule date is %schedule_dt%, Press Enter for accepting it!

set /p schedule_dt=[Please input your own schedule date:]



for /f "eol='" %%i in (hosts.txt) do (

  echo Host: "%%i" is executing schedule ... >> %logfile%

  schtasks /change /tn "Close_Menu_for_account" /s %%i /u username /p password /sd %schedule_dt% /st 23:00:00 >> %logfile%

)



goto ENDING



:OPEN

echo ... Open Menu for Account ...

echo Start schedule at %DATE% %TIME% > %logfile% 



for /f "eol='" %%i in (hosts.txt) do (

  echo Host: "%%i" is executing schedule ... >> %logfile%

  schtasks /run /tn "Open_Menu_for_account" /s %%i /u username /p password >> %logfile%

)



:ENDING

echo Finish schedule at %DATE% %TIME% >> %logfile%

echo Operation finish



:EXIT

pause

上述代码的作用有两个:1、指定多台服务器在23点整执行设置好的计划任务;2、立刻执行多台服务器上设置好的任务。

其中hosts.txt存储多台服务器的IP地址,具体内容如下所示:

'Web servers used
192.168.0.1
192.168.0.2
192.168.0.3

二、WebDev.WebServer

WebDev.WebServer是.NET framework 2.0内置的轻量级Web服务器,其主要是作为Visual Studio .NET调试ASP.NET程序使用。利用它我们可以对自己写的页面进行简单测试,查看效果。

@echo off

:Justify whether the webserver is running

tasklist | find /i "webdev.webserver.exe" >nul 2>nul && goto KILL || goto RUN



:KILL

taskkill /f /im "WebDev.WebServer.EXE" >nul 2>nul



:RUN

REM Suppose the VS2008 in VISTA

cd /d C:\Program Files\Common Files\microsoft shared\DevServer\9.0

start WebDev.WebServer.EXE /port:7736 /path:"D:\examples\tutorials\js" /vpath:"/"

你可能感兴趣的:(批处理)