Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem running a VBScript from Task Scheduler

Status
Not open for further replies.

WayneAdams

Programmer
Aug 23, 2012
1
US
Okay, I'm finding lots of similar problems, and I've tried numerous suggested fixes for this, but so far nothing has worked. I have a simple VBScript which opens an instance of Internet Explorer, navigates to a website login page, fills the UserID and Password field (with a Test account's credentials, as we only want to make sure the page is on-line), and "clicks" the Submit button. The web page ALWAYS opens a pop-up status window once it's logged on; our script sends an "Enter" keystroke to close it (there is only ever an "Okay" option), then writes a log entry for a successful login, closes the IE instance, releases the resources, and exits. If the session times out before the web page opens (a loop is used to set the timeout), or if the login fails, an error entry is written to the log and the script exits.

This script works perfectly if I run it manually, or if I put it in the Scheduler on my Windows 7 machine. Move it to the Server 2008 machine and the task is initiated, but the script does nothing. Run it manually with "only when user is logged in" selected, and it will run on the server. Run it manually with "whether user is logged in or not" selected, and it gets the same result as running in the Task Scheduler. I'm running this from a batch file that uses cscript to call the .vbs file.

Here's the batch file:
Code:
@echo off
cscript c:\VBScripts\AutoLogin.vbs

And here's the script file. The actual web site, along with the user ID and password are removed for security reasons.

Code:
' ============================================================
' Auto-login to a website using VBScript to see if the site
' can be accessed or not. If the login fails, write an error
' to the system log file.
'
' Written by Wayne Adams 8/10/2012
'
' 1) Open the home banking page.
' 2) Provide user name and password information.
' 3) Check whether the authentication was successful or not or the web page is opening up.
' 4) If login is UNSUCCESSFUL, store the results got in step 3) in a log file.
' 5) If login is SUCCESSFUL, close the dialog box that appears at login,
'    end the Session
'
' Purpose: to determine on a periodic basis that a particular link is working or not.
' You need to provide the correct designations for the elements: 
' strSiteURL (the Website URL)
' IDToken1 (the user ID), 
' IDToken2 (password) 
' the submit button (btnSubmit).  
' Open the source for the target login page and search for the correct controls.
' ============================================================

Dim oIE 'Browser object
Dim n	'Sleep counter
Dim strURL 'Web page URL
Dim IDToken1 'UserID
Dim IDToken2 'Password
Dim objPage 'Holds Page object 
Dim objTag 'Holds "A" Tag Object 
Dim objShell 'Holds a shell object
Dim intTotalLink 'Holds Total Link 
Dim intCtr 'Holds FOR Counter 
Dim strLink 'Holds Link Name 
Dim strHref 'Holds Href 
Dim intError 'Custom Error Code
Dim oShell 'Scripting Shell Object
Dim bResult 'Result of Shell Activate
Dim strCmdLine 'Concatenated line to pass through command line to error log

'WSH.Sleep 120000	' for testing scheduler execution. Comment this out to run live.

If WScript.Arguments.length =0 Then
  Set objShell = CreateObject("Shell.Application")
  'Pass a bogus argument with leading blank space, say [ uac]
  objShell.ShellExecute "wscript.exe", Chr(34) & _
  WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else
	'-------------------------------
	' Set up parameter values
	'-------------------------------
	strURL = "[URL unfurl="true"]https://www.myweb.com/"[/URL]
	IDToken1 = "UserID"
	IDToken2 = "Password"
	intError = 0
	
	' -----------------------------------
	' 1) Open web page
	' -----------------------------------
	Set oIE = CreateObject("InternetExplorer.Application")
	oIE.navigate strURL
	oIE.Visible = True
	
	' Wait for response
	n = 0
	Do while oIE.Busy And n < 100
	  ' Pause for 1 second before retrying condition
	  WSH.Sleep 1000
	  n = n + 1
	Loop
	
	' If timeout before response, 
	' log an error and quit
	If n >= 100 Then
		intError = 1
		WriteLog 
		wsh.quit
	End If
	
	' ---------------------------------------------
	'2) Provide user name and password information.
	' ---------------------------------------------
	Set UID = oIE.document.all.HBUSERNAME
	UID.value = IDToken1
	
	Set PWD = oIE.document.all.PASSWORD
	PWD.value = IDToken2
	
	' "Click" the submit button
	oIE.document.all.MCWSUBMIT.click
	
	' ---------------------------------------------
	'3) Check whether the authentication was successful 
	'   or not or the web page is opening up.
	' ---------------------------------------------
	n = 0
	Do until oIE.document.ReadyState = "complete" or n >= 100
	  ' Pause for 1 second before retrying condition
	  WSH.Sleep 1000
	  n = n + 1
	Loop
	
	' If timeout before response, 
	' log an error and quit
	If n >= 100 Then
		intError = 2
		WriteLog 
		wsh.quit
	End If
	
	' ---------------------------------------------
	' 4) Close the dialog box that appears when 
	'    the page opens.
	' ---------------------------------------------
	' Start the scripting shell
	Set oShell = CreateObject("WScript.Shell")
	
	' Access the Dialog box
	bResult = oShell.AppActivate("Message from webpage")
	
	' Pause for one second for response
	WScript.Sleep 1000
	
	' If successful, send "Enter" keystroke
	If bResult = True Then
	    oShell.SendKeys "{ENTER}"
	    ' or "%K" for Alt+k, for OK and "%N" (Alt+N) for Cancel
	Else
	' If login failed, write error
		Set oShell = Nothing
		intError = 2
		WriteLog 
		wsh.quit
	End If
	
	' Release resources
	Set oShell = Nothing
	' ---------------------------------------------
	' 5) Log off the site
	' ---------------------------------------------
	' Load all the links present on the web page
	Set objTag = oIE.document.GetElementsByTagName("A") 
	intTotalLink = objTag.Length - 1
	
	' Find the "End Session" link
	For intCtr = 0 to intTotalLink 
	     strLink = objTag(intCtr).InnerText 
	     strHref = CStr(objTag(intCtr).Href)
	     If Left(strLink,49) = "End Session" Then 
	     	' When link is found, call it
	        oIE.Navigate2(strHref)
	     End If
	Next
	
	' Write to the log
	WriteLog
	
	' Close the browser
	'oIE.Quit
End If

' Release objects
Set objTag = Nothing
Set oShell = Nothing
Set PWD = Nothing
Set UID = Nothing
Set oIE = Nothing

' Terminate the script
wsh.quit

' ================================================================
' This subroutine writes out to the log file if we were unable
' to get to the web page for any reason.
' ================================================================
Sub WriteLog

Set oShell = CreateObject("WScript.Shell")

'MsgBox("intError is " & intError)	' For testing purposes
Select Case intError
	Case 0
		strCmdLine = "eventcreate /l System /t INFORMATION /so AutoLogin /id 1 /d "& Chr(34) & "Successful Login" & Chr(34)
	Case 1
		strCmdLine = "eventcreate /l System /t ERROR /so AutoLogin /id 1 /d "& Chr(34) & "Unable to open Web Page" & Chr(34)
	Case 2
		strCmdLine = "eventcreate /l System /t ERROR /so AutoLogin /id 2 /d "& Chr(34) & "Unable to Login to Web Site" & Chr(34)
	
End Select

oShell.Run strCmdLine

' Close the browser
oIE.Quit

' Release objects
Set objTag = Nothing
Set oShell = Nothing
Set PWD = Nothing
Set UID = Nothing
Set oIE = Nothing

End Sub

Any help I can get would be appreciated. I'm pulling my hair out over this (and I don't have enough to spare as it is...[sad] )
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top