Link to home
Start Free TrialLog in
Avatar of davewalter1
davewalter1

asked on

Scheduled task runs IE via VB script, and won't work unattended

I'm using a short vbs file which starts IE, navigates to a URL, and then closes IE. My problem seems to be that the URL is SSL encrypted, so it needs credentials. I've saved a username and password and the script below seems to work fine unless it's running unattended. It's running on Server 2003.

The script is below.:

Is this the best way to approach this? I've never tried using VB this way (I'm not a VB guy) but it's the only thing I could think of to get past the login dialog.

Thanks for your help.
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "iexplore https://myurl.aspx?SendMail=true", 9
WScript.Sleep 10000 ' Give ie some time to load
 
'Accept the credentials and close the browser
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "%FX"

Open in new window

Avatar of sr75
sr75
Flag of United States of America image

What are you trying to do?  I suggest you use the MSXML2.XMLHTTP.3.0 object instead as it will not OPEN IE (but it uses it none the less).  It is what I use for some of my scripts that are scheduled tasks.  I have attached an example of how to use it as well as provided a link that explains how to use the object.

http://en.wikipedia.org/wiki/XMLHttpRequest

Set XML = CreateObject("MSXML2.XMLHTTP.3.0")
strURL = "http://www.experts-exchange.com"
XML.Open "GET", strURL, False
XML.setRequestHeader "User-Agent","MyBrowser" 
XML.send   
 
If err.number <> 0 then 
        strHTML = "Url not found" 
else 
        strHTML = XML.responseText 
End If
 
wscript.echo strHTML

Open in new window

Unattended meaning running in the system account or similar?  Does the IE window actually get displayed on screen?  If it doesn't, the SendKeys function won't work... that only works for visible windows.
Avatar of davewalter1
davewalter1

ASKER

Sorry if I was vague. By unattended, I mean nobody's logged in to the server at the time -- it's running at 3:00 AM. If I'm logged in (it's running under my account -- I'm an administrator) it displays the IE window and runs as expected.

Here's what I'm trying to do: I have an aspx page that does some stuff. I want to send an email with output from that page. I have an email function that looks for a URL parameter and sends email only if it sees that parameter. This way, I can maintain one page and provide a link which doesn't send mail every time the page is accessed, and sends email when I run the page as a scheduled task with the URL parameter specified.

The task spawns IE and navigates to the URL. IE then asks for authentication, so I previously provided credentials and saved a password. Then the SendKeys hit enter to accept the credentials and close the browser.
Thanks for that, sr75. I'll see what I can do with that when I get in to the office. This is the first time I've used  a script for this -- I don't normally use VB, so I'm a complete beginner here.
No problem.  Using the sendkeys method requires a user to be logged in and that just isn't going to work for what you want to do.   I use the method to get the source of the page and then I convert it to get the MD5 hash.  If it changes from a known good hash, I get emailed.   It helps me monitor my websites for any changes (i.e. getting defaced).   It should work for you, just use an "If InStr()" to search for that URL in the XML.ResponseText.  If you didn't see it in the link, it does support authentication.
Sorry, man. I really need hand-holding on this one. I substituted my URL for strURL, and the Script Host returns the error "Access is denied." Is that because the page needs authentication, and I'm not supplying credentials? I'm rereading what you've said above, and I'm trying to understand... :-)
Yes, it requires credentials.   Change the XML.Open line to this one (substituting the username and password):

XML.Open "GET", strURL, False, userName, password
Same error. Here's what Script Host says when I try to run the script:

Script: F:\path to the script
Line: 5
Char: 1
Error Access is denied.
Code: 80070005
Source: msxml3.dll
This still doesn't work. Please let me know if there's any interest in solving this problem, or I'll request that the question be deleted.
What is the F: drive?
A logical drive partition on the machine running the scheduled task.
lol, there was no other answer to that really...

The Access is Denied error is being generated by line 5 of the script, which if the script is as you posted, sr75, is the XML.send line.

I would think this is the web-server rejecting the log-in?
That's what I would think, too. But if I forget about the script and step through the process and manually enter the credentials, they work fine. Besides, I wouldn't want the script to contain the credentials, anyway...
If the webpage requires credentials, then the script is required to enter the credentials.

The webpage is an HTTPS?  do you get prompted for a certificate as the user running the script?  If so then you need to import the certificate while logged in as the user who is running the script.  That may be why you are getting denied.  I had to do that for the websites that I used my script on.

I haven't tested it, but you may be able to select "remember me" and NOT have to place the credentials into the script.
One thing I was thinking... presumably the credentials that are being sent in this case are username/password as would apply to domain logons.  If the website uses a login based on forms and cookies, it would not work.  In this case you'd have to start from the login page and send the login as POST data?  It could get a bit complex after this though, I have a feeling...
Thanks, gents...

I don't get prompted for a certificate. And I get the error even with a username and password hard coded in the script.  And the logon is a domain logon, so nothing funky going on there. If it weren't for the fact that I have to be logged in to run it, the script I had initially would be perfect!
I don't suppose there is a way to use the AT Service Account to accomplish this? I don't know what this account actually does... In any case, I have very little experience scripting like this.
ASKER CERTIFIED SOLUTION
Avatar of davewalter1
davewalter1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Okay, here's the final bit, a batch file to copy the files:

@echo off
xcopy /d /y "path to source file" "path to destination directory"