让我们先给vbs做一个ASP页面作为接口吧。我将其命名为TaskSelector,就是通过他来选择执行什么任务的意思。
执行任务前,我们需要做安全检查,这里我做了两个安全检查,一个是这个接口只有本机可以调用,另一个是因为采取用了参数直接对应到文件名,所以要防止使用..这样的父目录。其他的,在我的获取参数的函数sReq中,也进行了各项安全检查,这个就不做介绍了。
来看一下这个TaskSelector.asp
<!--#include file="../_.asp"-->
<% '本页面作为vbs调用的接口,命名为任务选择器
'为了安全起见,如果非本地调用,则停止。该页面只允许通过 http://localhost/... 的形式调用。
If Request.ServerVariables("HTTP_HOST")<>"localhost" Then
Response.End()
End If
'要运行的任务ID
taskId = sReq("taskId")
If taskId="" or InStr(taskId, "..")>0 Then
Response.End()
End If
'运行前先记入数据库,进程开始咯
sql = "Insert into logAutoTasks (taskId, logTime, Operate) Values ('"&taskId&"', getDate(), '任务开始');"
db.Execute(sql)
Server.Execute "AUTOTASK_"&taskId&".asp"
'运行结束也记入数据库
sql = "Insert into logAutoTasks (taskId, logTime, Operate) Values ('"&taskId&"', getDate(), '任务结束');"
db.Execute(sql)
%>
也就是说,其他的任务文件,都使用 AUTOTASK_taskId.asp 作为文件名来保存,从这个TaskSelector调用。
我现在来新建一个 AUTOTASK_helloworld.asp 文件
<%
'Hello World
Response.Write "Hello"
%>
然后修正刚刚的wsf,将其接口对准这个helloworld:
task.Run "http://localhost/autorun/TaskSelector.asp?taskId=helloworld", 5000
好了,再执行刚刚的wsf文件,然后我们来检查数据库里的任务运行日志。
嗯,好多。如果想只看当前helloworld任务的状态,可以用这样的SQL:
select top 1 * from logAutoTasks where taskId='helloworld' order by logTime desc
嗯,好吧,我承认上一篇设计上有个小失误,taskId也应该加上索引,否则这个查询也可能会慢。