VBScript - 使用错误处理来确定任务是否存在

时间:2016-09-07 21:58:05

标签: vbscript error-handling scheduled-tasks

使用我学到的信息herehere。 我在下面创建了短代码。

当我运行代码并故意输入任务调度程序库的“\”文件夹中存在的任务时,我收到“任务不存在”msgbox。

我尝试在传递给函数的变量周围加上双引号,但没有成功。非常感谢任何指导。

Option Explicit
Dim strtaskn, strtaskf, sso, result

Call start
Sub start
  strtaskn = ""
  strtaskf = ""
  Do While Len(strtaskn) = 0
    strtaskn = inputbox("Please enter task name to search for:", "Task Exist?")
    If isempty(strtaskn) Then
      WScript.Quit()
    End If
  Loop

  strtaskf = InputBox("Please enter task folder to search in:" & vbCrLf & vbCrLf & "Leave blank for default folder", "Task Exist?")
  If Len(strtaskf) = 0 Then strtaskf = "\"
  If IsEmpty(strtaskf) Then
    WScript.Quit()
  End If

  'WScript.Echo strtaskn
  'WScript.Echo strtaskf

  Call taskexist(strtaskn, strtaskf)
End Sub

Function taskexist(taskname, taskfolder)
  Dim rootfolder, task

  Set sso = CreateObject("Schedule.Service")
  Call sso.Connect()

  On Error Resume Next
  Set rootfolder = sso.GetFolder(taskfolder)
  If Err.Number <> 0 Then
    result = MsgBox("Task folder does not exist " & Err.Description & vbCrLf & vbCrLf & "Do you wish to try another search?", vbYesNo+vbCritical, "Task Exist?")
    Select Case result
      Case vbYes
        Err.Clear
        Call start
      Case vbNo
        WScript.Quit()
    End Select
    Err.Clear
  End If

  Set task = rootfolder.GetTasks(taskname)
  If Err.Number <> 0 Then
    result = MsgBox("Task does not exist " & Err.Description & vbCrLf & vbCrLf & "Do you wish to try another search?", vbYesNo+vbCritical, "Task Exist?")
    Select Case result
      Case vbYes
        Err.Clear
        Call start
      Case vbNo
        WScript.quit()
    End Select
    Err.Clear
  End If
  On Error Goto 0

  result = MsgBox("Task exists" & vbCrLf & vbCrLf & "Do you wish to try another search?", vbYesNo+vbInformation, "Task Exist?")
End Function

更新

使用我的第一个link中显示的示例,我可以通过将集合中发现的每个任务(通过.name)加载到数组中,并将元素与元素进行比较来查看实现相同目标的另一种方法。 taskname我正在搜索,或者只是使用迭代循环。

更新

在了解原始代码中的错误之前,感谢Ansgar。我开始编写替代版本的脚本,我认为与社区分享是正确的做法......

option explicit
dim strtaskn, strtaskf, sso, rootfolder, taskcollect, numtasks, intcomp, taskreg, result

call start
sub start
strtaskn=""
strtaskf=""
do while len(strtaskn)=0
strtaskn = inputbox("Please enter task name to search for:","Task Exist?")
if isempty(strtaskn) then
wscript.quit()
end if
loop

strtaskf = inputbox("Please enter task folder to search in:" &vbcrlf&vbcrlf& "Leave blank for default folder","Task Exist?")
if len(strtaskf)=0 then strtaskf = "\"
if isempty(strtaskf) then
wscript.quit()
end if

wscript.echo strtaskn
wscript.echo strtaskf

set sso = createobject("schedule.service")
call sso.connect()

on error resume next
set rootfolder = sso.getfolder(strtaskf)
if err.number <> 0 then
result = msgbox("Task folder does not exist " &err.description &vbcrlf&vbcrlf& "Do you wish to try another search?", vbyesno+vbcritical, "Task Exist?")
select case result
case vbyes
err.clear
call start
case vbno
wscript.quit()
end select
err.clear
end if
on error goto 0

set taskcollect = rootfolder.gettasks(0)

numtasks = taskcollect.count

if numtasks = 0 then 
wscript.echo "No tasks are present in " &strtaskf& " folder"
else
for each taskreg in taskcollect
intcomp = strcomp(strtaskn, taskreg.name, vbtextcompare)
    if intcomp = 0 then
    result = msgbox("Task exists" &vbcrlf&vbcrlf& "Do you wish to try another search?", vbyesno+vbinformation, "Task Exist?")
    select case result
    case vbyes
    call start
    case vbno
    wscript.quit()
    end select  
    end if
next
    result = msgbox("Task does not exist " &vbcrlf&vbcrlf& "Do you wish to try another search?", vbyesno+vbcritical, "Task Exist?")
    select case result
    case vbyes
    call start
    case vbno
    wscript.quit()
    end select
end if
end sub

2 个答案:

答案 0 :(得分:1)

Set task = rootfolder.GetTasks(taskname)

上述陈述并不符合您的想法。 GetTasks方法/属性(注意尾随“s”)返回给定文件夹中的所有任务。它的参数是一个整数标志,表示隐藏的任务是否应该包含在返回的列表中(1→包括隐藏的任务,0→不包括隐藏的任务)。

您真正想要的是GetTask属性:

Set task = rootfolder.GetTask(taskname)

答案 1 :(得分:0)

Set service = CreateObject("Schedule.Service")
service.Connect()
Set rootFolder = service.GetFolder("\")
DetermineName = "\Test Task"
task = rootfolder.GetTask("Test Task")
   if task = DetermineName then
     WScript.Echo "Task Exist"
  else
     WScript.Echo "Not Exist"
  end if