Using the information I learned here, and here. I created the short code below.
When I run the code, and knowingly input a task that exists in the "\" folder of the task scheduler library, I receive the "Task does not exist" msgbox.
I tried including double-quotes around the variables passed into the function, with no success. Any guidance is very much appreciated.
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
UPDATE
Using the example shown in my first link, I can see an alternative way of achieving the same objective by loading each task found in the collection (by .name) into an array, and comparing the elements to the taskname I'm searching for, or simply by using an iteration loop.
UPDATE
Before learning of the error in my original code, thanks to Ansgar. I began scripting the alternative version, which I believe sharing with the community is the right thing to do...
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
Set task = rootfolder.GetTasks(taskname)
The above statement doesn't do what you think it does. The GetTasks
method/property (note the trailing "s") returns all tasks in the given folder. Its parameter is an integer flag indicating whether or not hidden tasks should be included in the returned list (1→include hidden tasks, 0→don't include hidden tasks).
What you actually want is the GetTask
property:
Set task = rootfolder.GetTask(taskname)