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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running a VBScript ON THE SERVER via ASP 1

Status
Not open for further replies.

benlinkknilneb

Programmer
May 16, 2002
590
US
Hey all,

I am attempting to run a VBScript on my webserver when the user clicks a button. For some reason I was unable to add the script directly into my asp code... it's a mail send routine for Lotus Notes and it doesn't behave well with ASP. My workaround was to make a standalone script on the server (which runs fine when I manually execute it) and then make my ASP page call that script. It has to run on the server because it accesses a special account within Lotus Notes that is set up for this server... ie I can't use the individual users' Notes accounts.

Here's my ASP code:
Code:
Dim objWShell
Set objWShell = CreateObject("WScript.Shell")
objWShell.CurrentDirectory = "{directory}"
objWShell.Run("SendMail.vbs '" & strSubject & "' '" & strBody & "' '" & strToAddr & "'")

As I said, the script that it is supposed to call works fine... but this code does NOT open the vbs file. Any suggestions?

Ben
The ships hung in the sky in much the same way that bricks don't. - Douglas Adams
 
You may not have rights to run the shell or the OCX for the shell may not be registered on the server. It should be registered by default but is sometimes unregistered for security reasons.

Try out this code. It executes a shell, sends a ping request and echos the results back to the browser. If this works then you have shell access. If it does not work then you do not have rights to execute the shell and will have to talk with the server admin about it.

Code:
<% Response.Buffer = true %> 
<% 
    url = "[URL unfurl="true"]www.espn.com"[/URL] 
 
    Set objWShell = CreateObject("WScript.Shell") 
    Set objCmd = objWShell.Exec("ping " & url) 
    strPResult = objCmd.StdOut.Readall() 
    set objCmd = nothing: Set objWShell = nothing 
 
    strStatus = "offline" 
    if InStr(strPResult,"TTL=")>0 then strStatus = "online" 
 
    response.write url & " is " & strStatus 
    response.write ".<br>" & replace(strPResult,vbCrLf,"<br>") 
%>

It's hard to think outside the box when I'm trapped in a cubicle.
 
Hey niteowl,

Thanks for the quick response. I ran the code you provided and it seems to have executed perfectly.

Is there anything syntactically wrong with my original code above? All I'm attempting to do is pass in recipient, subject and body strings.

Ben
The ships hung in the sky in much the same way that bricks don't. - Douglas Adams
 
I do not see anything obviously wrong but it could be in the bits I cannot see.

What is the format of the directory path to the vbs file?
The path will have to be fully justified, you cannot use a relative path to the current folder because (I believe) the shell object is executing with server-side privledges outside of the IIS server context. So you would have to give the full path like: C:\MyFolder\somefile.vbs

Does your VBS file accept parameters without a delimiter or is it using a space as a delimiter?

You can try making a simple BAT file rather than the VBS file and just have it execute a command like "dir > test.txt" and execute that to see if it is running.

When the bat or vbs file runs it will not return anything to the screen, you know that right? Anything executing server-side will not show up on your screen. In the PING code above it reads the output of the screen and returns it in a variable but you would never see the actual window display.


It's hard to think outside the box when I'm trapped in a cubicle.
 
When you say that it accesses a special account, is it using the username of the logged in user to authenticate the Lotus account or are you passing that information seperately? Sorry, don't know much about Lotus.

Basically when you run this yourself it runs fine, when you automate from the website it doesn't work. So the differences are:
Manual:
Runs as the person you are logged in as
Runs with access to the desktop and logged in user's windows

Website:
Runs as the generic web user (probably)
Runs without access to a dsktop/windowing environment

Based on that my guess would be that either it's a permissions issue, an issue with the difference between what user launches the application, or the application wants to open some sort of display and realizes the user it is logged in under doesn't have an active desktop/display.

-T

barcode_1.gif
 
I missed that bit about the Lotus Notes account or even Lotus Notes for that matter.

What is the VBS file supposed to do? As I mentioned above and Tarwn talks about as well is that no window is going to execute on the server. This does not mean that the application cannot be run this way but it could be problematic if the program is not capable of automation.
You could also end up with the application left running on the server if it does not close itself out after it completes.

I tested this code successfully.
Code:
<%

Dim objWShell
Set objWShell = CreateObject("WScript.Shell")
objWShell.CurrentDirectory = "C:\"
objWShell.Run("SendMail.bat")

%>
The only difference is that I run a bat file without parameters just to make certain it is executing.
The ASP page finishes it's job immediately but the bat file still runs in the background until it has completed. You will see no indication that it is running server-side except perhaps through Task Manager where you can see the active process.

If you do something similar with an app like MS Word or Excel you have to take care to close out the application at the end of your asp page or it gets left hanging on the server eating up resources and they are difficult to shut down.



It's hard to think outside the box when I'm trapped in a cubicle.
 
Ok, looks like I have a couple of questions to work with here.

1. My SendMail.vbs opens a notes session, and uses the parameters to send an email. It's essentially a notification for a helpdesk ticket entry... nothing fancy, just a warning that a ticket status has changed.

2. I wrote a test.vbs script which did nothing but call the SendMail.vbs and pass it parameters. This ran perfectly.

3. I added the test.vbs code to my asp page (see my original post) and when I run the asp page I get no errors, but my email never arrives either.

4. I added an erroneous line of code to SendMail.vbs, hoping that I could force an error to appear, but nothing ever showed up. So that's why I believe that the script is never called.

5. #2 above proved to me that the issue was not with the Notes setup, because test.vbs actually sent me an email.

6. I am using the full path name to the script file... it resides at C:\test\scripts. My asp page resides at C:\test\user.

Tarwn's thought above about difference between which user runs it makes me think... can I add a "run as" command to the asp call, run it as the server's logged-in user, and be ok? If so, how do I do "run as" from the command line?

Ben
The ships hung in the sky in much the same way that bricks don't. - Douglas Adams
 
You can bang your head on permissions problems all day and then find out it is something simpler. I would eliminate the obvious first by making sure the ASP CAN execute a file in that folder.

The problem could be the code executing the file. It could be permissions related but a problem with execution, not account issues within Lotus Notes. You could spend all day trying different things working on this from the wrong end and all your attempts could fail for the simple reason that the vbs file does not execute for a different reason.

Step 1. Verify that you can execute a very basic script from your ASP page. This should be code that does nothing that would require additional privledges. Calling a .bat file that has the command dir > test.txt is a good example. You cannot see the bat file run but you can check test.txt for verification.

Step 2. Once you know you can run a bat file in that folder try running the .vbs file. It would be a good idea to setup your vbs file to output an error.txt file when it encounters an error because you WILL NOT see the application run.

When the shell runs I believe it runs under the IWAM_machinename accounts permissions. Make certain that the account has permissions to the file and folder you are trying to run the file in.

It's hard to think outside the box when I'm trapped in a cubicle.
 
Ok, Thanks for your continuing help...

Step 1 is where the failure occurs. I am unable to execute the bat file. I gave the IWAM account full control over the working directory, and it did not execute the bat file at all.

Suggestions?

Ben
The ships hung in the sky in much the same way that bricks don't. - Douglas Adams
 
Check the permissions on the folder.
What rights does the Everyone account have?
How about Anonymous?
Are the rights set to flow down from the higher level folder? If so it could be overwriting any local privledges you set there.

In the IIS server settings you can flag that folder to allow access to a specified account. I am not familiar with the screens for that and cannot look it up at the moment but you may find something.

Is the folder the script is in on the same server or is it elsewhere on the network?
What version of IIS are you running?

It's hard to think outside the box when I'm trapped in a cubicle.
 
Folder Permissions:

"Anonymous" and "Everyone" are not on the list. I have the IUSR_PCNAME account, the IWAM_PCNAME account, the server's current login, and SYSTEM on the list. All have Full Control. Settings are not inherited from above.

For ease of testing, I moved the script to the same folder as the ASP page resides in... on the same server, of course.

IIS = 6.0. Incidentally, I updated the Microsoft Scripting Engine before posting to Tek-Tips... one website I saw suggested that as a possible fix... but no luck there.

I set the property for the folder in IIS to be Read/Write, and Scripts only (no executables). Then I tried it Read Only, with Scripts AND Executables. Nothing changes either way.

Ben
The ships hung in the sky in much the same way that bricks don't. - Douglas Adams
 
The folder would need execute permissions for a vbs file.
Try adding the Everyone group to the folder to execute the vbs script from.
My test server is IIS 5, the folder did not have IUSR or IWAM accounts but it had the Everyone group set will full premissions and the test folder was outside of the Web server environment off the root and it worked without a problem. IIS 6 has extra security from what I have read.

Your test should have worked within the ASP script folder, make sure execute permissions are on for that folder in IIS.


It's hard to think outside the box when I'm trapped in a cubicle.
 
niteowl... thanks so much for your continuing help.

I tried making the folder within and outside of the web environment, with Everyone and Full Control. Neither way worked.

I think it just hates me. :)

Ben
The ships hung in the sky in much the same way that bricks don't. - Douglas Adams
 
Under IIS administration find the folder you are executing the ASP file in, right click and go into it's properties, then to the virtual directory tab and see which checkboxes are checked and which options are selected under Execute Permissions and Application Protection.
My own are set to Read, Write, Scripts and Executables, Medium (Pooled) respectively.
Then under the Directory Security tab and Anonymous access and authentication click the Edit button and see if Anonymous Access is checked then click the edit button and see which account is setup for access.

I am not sure if any of these are causing your problems but figure if mine is working with these settings...



It's hard to think outside the box when I'm trapped in a cubicle.
 
I attempted your last suggestion about checking the IIS settings. The script failed to run. From the links you provided, I noticed a line that said my VBscript should run directly from the ASP page. I set it up this way and I get an unidentified trappable error. Then, if I run it again without restarting the machine, I get an out-of-memory error on the line where I create the lotus notes session object.

=CreateObject("Notes.NotesSession")

This was the reason I attempted to run the script separately in the first place... any ideas why that wouldn't work either?

Ben
The ships hung in the sky in much the same way that bricks don't. - Douglas Adams
 
The vbs code may have to be reworked a bit to work in an ASP page as opposed to running local on the machine.
It sounds as if the code IS creating an object but then not accessing it properly or closing it out so the next time you run the code the object is already there and causing different problems.

It may be that you do not have the correct server extensions installed to run the external files or that the methods of authentication have to be addressed for this application type. Much of what was in the first link is beyond what I have worked with and I am using IIS 5 for which the same issues do not apply so all I can do is guess.

You need to determine which ID is in use by the system to execute the application, all files used by that application and set permissions for that ID on all files/folders involved.
You may need to ensure that the IUSR account has access to the cmd.exe file or that the Everyone or Anonmyous groups have read/execute permissions. I would not have thought so since the first tests with the Ping code worked I figured you were getting a shell fine.
vbs files use either the wscript or cscript.exe files so access may be needed on those files.

I really do not know what else to try. It has to do with the way IIS6 locks things down and I cannot replicate that here for testing but I would really like to know the solution.


It's hard to think outside the box when I'm trapped in a cubicle.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top