Solved

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

Posted on 2008-06-17
19
297 Views
Last Modified: 2008-07-17
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

0
Comment
Question by:davewalter1
  • 11
  • 5
  • 3
19 Comments
 
LVL 15

Expert Comment

by:sr75
ID: 21807136
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

0
 
LVL 24

Expert Comment

by:purplepomegranite
ID: 21807144
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.
0
 

Author Comment

by:davewalter1
ID: 21807640
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.
0
 

Author Comment

by:davewalter1
ID: 21807650
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.
0
 
LVL 15

Expert Comment

by:sr75
ID: 21809334
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.
0
 

Author Comment

by:davewalter1
ID: 21812242
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... :-)
0
 
LVL 15

Expert Comment

by:sr75
ID: 21813840
Yes, it requires credentials.   Change the XML.Open line to this one (substituting the username and password):

XML.Open "GET", strURL, False, userName, password
0
 

Author Comment

by:davewalter1
ID: 21816420
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
0
 

Author Comment

by:davewalter1
ID: 21907225
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.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 15

Expert Comment

by:sr75
ID: 21908004
What is the F: drive?
0
 

Author Comment

by:davewalter1
ID: 21908121
A logical drive partition on the machine running the scheduled task.
0
 
LVL 24

Expert Comment

by:purplepomegranite
ID: 21908168
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?
0
 

Author Comment

by:davewalter1
ID: 21908320
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...
0
 
LVL 15

Expert Comment

by:sr75
ID: 21912275
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.
0
 
LVL 24

Expert Comment

by:purplepomegranite
ID: 21912424
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...
0
 

Author Comment

by:davewalter1
ID: 21915578
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!
0
 

Author Comment

by:davewalter1
ID: 21956019
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.
0
 

Accepted Solution

by:
davewalter1 earned 0 total points
ID: 21963751
If anyone's interested, here's my solution: circumvent browser authentication. I created a directory, copied the dotnet file into it,  and set IIS permissions to allow anonymous access, but restricted file access on the directory to the server's IP address. So it can't be run except at the server, and there's no need to log in.

I need a script to copy the dotnet file to the restricted directory so that I don't have to worry about propagating changes to the file. I'm a beginner at scripting these things, so if someone would like to write that script, I'd appreciate it and that would complete this solution.
 
0
 

Author Comment

by:davewalter1
ID: 21964077
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"
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now