xtross
asked on
WshShell.Exec never finishes. Simple code not working correctly.
I have created a script that gets a list of all the Scheduled Tasks, parses the results and restarts any failed Tasks. The problem I am having is with the Set oExec = WshShell.Exec("schtasks /query /fo LIST /v") part of the script. The oExec.Status never reaches 0. So if I time out the wait the data I parse through is incomplete. Out of six Scheduled Tasks I get to parse about 2.8. If I remove the time out of the script it stalls after doing the WshShell.Exec. I have run this in debug mode and just can't see why it is not working. Also if I redirect the output, "schtasks /query /fo LIST /v > taskfile.txt", I get all the tasks listed in the text file.
Dim WshShell
Dim oExec
Dim MaxWaitTime
Dim CurrentWaitTime
Dim strLine
Dim strLineArrayTask
Dim strLineArrayResult
Dim strItem
Dim strTaskName
Dim strTaskArray
Dim strLastResult
Dim objEmail
MaxWaitTime = 20000
CurrentWaitTime = 0
Set WshShell = CreateObject("WScript.Shel l")
'The tool is launched
Set oExec = WshShell.Exec("schtasks /query /fo LIST /v")
'We wait for the end of the process
Do While oExec.Status = 0
WScript.Sleep 100
CurrentWaitTime = CurrentWaitTime + 100
If CurrentWaitTime >= MaxWaitTime Then
oExec.Terminate
Exit Do
End if
Loop
'We scan the output for the TaksName and LastStatus
Do While oExec.StdOut.AtEndOfStream <> True
strLine = oExec.StdOut.ReadLine
Wscript.Echo strLine
If instr(strLine,"TaskName:") <> 0 Then
'Wscript.Echo "Found:TaskName: " & vbcrlf & strLine
strLineArrayTask = split(trim(strLine))
'For each strItem in strLinearrayTask
'msgbox strItem
'Next
strTaskName = strLineArrayTask(29)
'MsgBox strTaskName
Else If instr(strLine,"Last Result:") <> 0 Then
'Wscript.Echo "Found:Last Result: " & vbcrlf & strLine
strLineArrayResult = split(trim(strLine))
'For each strItem in strLinearrayTask
'msgbox strItem
'Next
strLastResult = strLineArrayResult(27)
'MsgBox strLastResult
If (strLastResult) <> 0 Then
WshShell.Exec("schtasks /run /TN " & strTaskName)
Set objEmail = CreateObject("CDO.Message" )
objEmail.Configuration.Fie lds.Item(" http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fie lds.Item(" http://schemas.microsoft.com/cdo/configuration/smtpserver") = "MailServer"
objEmail.Configuration.Fie lds.Item(" http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fie lds.Update
objEmail.From = "email@address.com"
objEmail.To = "email2@address.com"
objEmail.Subject = "The " & strTaskName & " was restarted by the autostarter."
objEmail.Textbody = "The reason it was restarted was the Last Status when checked was " & strLastResult
objEmail.Send
Set objEmail = nothing
End If
End If
End if
Loop
Dim WshShell
Dim oExec
Dim MaxWaitTime
Dim CurrentWaitTime
Dim strLine
Dim strLineArrayTask
Dim strLineArrayResult
Dim strItem
Dim strTaskName
Dim strTaskArray
Dim strLastResult
Dim objEmail
MaxWaitTime = 20000
CurrentWaitTime = 0
Set WshShell = CreateObject("WScript.Shel
'The tool is launched
Set oExec = WshShell.Exec("schtasks /query /fo LIST /v")
'We wait for the end of the process
Do While oExec.Status = 0
WScript.Sleep 100
CurrentWaitTime = CurrentWaitTime + 100
If CurrentWaitTime >= MaxWaitTime Then
oExec.Terminate
Exit Do
End if
Loop
'We scan the output for the TaksName and LastStatus
Do While oExec.StdOut.AtEndOfStream
strLine = oExec.StdOut.ReadLine
Wscript.Echo strLine
If instr(strLine,"TaskName:")
'Wscript.Echo "Found:TaskName: " & vbcrlf & strLine
strLineArrayTask = split(trim(strLine))
'For each strItem in strLinearrayTask
'msgbox strItem
'Next
strTaskName = strLineArrayTask(29)
'MsgBox strTaskName
Else If instr(strLine,"Last Result:") <> 0 Then
'Wscript.Echo "Found:Last Result: " & vbcrlf & strLine
strLineArrayResult = split(trim(strLine))
'For each strItem in strLinearrayTask
'msgbox strItem
'Next
strLastResult = strLineArrayResult(27)
'MsgBox strLastResult
If (strLastResult) <> 0 Then
WshShell.Exec("schtasks /run /TN " & strTaskName)
Set objEmail = CreateObject("CDO.Message"
objEmail.Configuration.Fie
objEmail.Configuration.Fie
objEmail.Configuration.Fie
objEmail.Configuration.Fie
objEmail.From = "email@address.com"
objEmail.To = "email2@address.com"
objEmail.Subject = "The " & strTaskName & " was restarted by the autostarter."
objEmail.Textbody = "The reason it was restarted was the Last Status when checked was " & strLastResult
objEmail.Send
Set objEmail = nothing
End If
End If
End if
Loop
ASKER
Using cmd /c schtasks /query /fo LIST /v had no affect on the script.
Would there be any chance that, for certains tasks, the command is waiting for a password to be entered, because of an "access denied" issue?
If you run the same command from a command prompt manually, under the same user account that would run the script, does it complete, and how long does it take?
Regards,
Rob.
If you run the same command from a command prompt manually, under the same user account that would run the script, does it complete, and how long does it take?
Regards,
Rob.
ASKER
The same command runs fine in a command window. There is no prompt for anything when running schtasks. I'm starting to think the file itself is corrupt. It makes no sense.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Never did solve this one. I went with creating a file as suggested.
Set oExec = WshShell.Exec("schtasks /query /fo LIST /v")
to
Set oExec = WshShell.Exec("cmd /c schtasks /query /fo LIST /v")
Regards,
Rob.