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

Including an ASP code into a .vbs file

Status
Not open for further replies.

mellenburg

Programmer
Aug 27, 2001
77
0
0
US
I'm trying to create a *.vbs program that can be run by the windows scheduler. The program I'm going to convert is written in classic ASP. It calls 5 very large classic ASP programs. If I convert the first program to .vbs, do I have to also convert the 5 large ASP programs? How would I include or run the ASP programs from a *.vbs program.

Matt
 
Do you need the output of those other ASP files or do you just need them to execute?
 
I just need them to execute, but I have to pass parameters that would be obtained using the main program. I found some information about executing an ASP program from a *.vbs file, but I haven't found any examples yet.
 
If I can execute an ASP file from a .vbs program, then I'll just execute the page I'm trying to convert.
 
WSH is one script host and ASP is another. Script really only has meaning within the context and object model of its host, though subroutines and other small "islands" of script code can be portable.

But WSH knows nothing of the ASP meta-markup that wraps snippets of script in ASP pages. It also doesn't provide the ASP objects (Session, Request, Response, etc.).

Of course nothing prevents a WSH script from hitting ASP pages on your web server to perform services. You just need to choose the right HTTP request component to do that.

If you want this all to run on the client as a WSH script though you'll have to do some conversion.
 
I was thinking along the lines of using the wShell's Exec method to lauch IExplore.exe with a command line paramenter specifying the page...



 
Just use a scriptable HTTP request component. Ever so much less hassle than automating an instance of IE. Lighter weight too.
 
Dilettante, can you provide an example of this? I'm currently trying to figure out how to schedule execution of an asp page. I was going to convert it to .vbs, but your solution of calling hte existing page sounds easier. Is this using an instance of ServerXMLHTTP in a .vbs?

Thanks
 
Yes, you could do precisely that. Almost any simple ActiveX component for making HTTP requests would do though.

Here is a generalized script I use for "pinging" web servers from scheduled BAT/CMD files. It would probably do all you need:

httping2.wsf
Code:
<job>
  <!--
  HTTPING2.WSF - "Ping" a web server.

  Version 2.0

  This WSH script is used to make HTTP requests of web servers and
  display the results returned.  It is most useful in determining
  whether a web server or application is responding from within
  Windows batch files by testing ERRORLEVEL for failure Return
  Codes (mostly due to timeouts) and HTTP Status Codes (200 is OK,
  the standard good result).

  The script is a wrapper for the "w3 Sockets" sock.tcp class, a
  free ActiveX component that can be obtained from Dimac Development
  at:
                           [URL unfurl="true"]www.dimac.com[/URL]

  There is no charge, but registration is required.

  The w3 Sockets component must be properly installed and registered
  on any machine you want to run this script on.

  Most of the effort here is spent on supporting command line
  parameters and "help."  Inspecting this script will show that the
  logic needed to use w3 Sockets for this purpose is relatively
  trivial.  If you are writing WSH scripts instead of using batch
  files you would probably want to use w3 Sockets directly.

  -->
  <runtime>
    <description>
This script makes an HTTP GET request of a web server and displays
the results returned.

Return Codes:

   1    : Bad command line options
  10    : Open failed
  20    : Send failed
  30    : Receive failed
  40    : Result display failed
>199    : HTTP Response Status Code, 200 = OK
    </description>
    <unnamed name="host"
             helpstring="Web server to access"
             many="false"
             required="true"/>
    <unnamed name="uri"
             helpstring="Resource (page) requested without leading &quot;/&quot; (defaults to root)"
             many="false"
             required="false"/>
    <named   name="port"
             helpstring="HTTP port (default = 80)"
             type="string"
             required="false"/>
    <named   name="timeout"
             helpstring="Time to wait for response (in ms, default = 1000)"
             type="string"
             required="false"/>
    <example>
Examples:

    cscript //nologo httping.wsf [URL unfurl="true"]www.google.com[/URL] > response.txt

      Suppresses WSH logo, GETs default root page of [URL unfurl="true"]www.google.com[/URL] and
      redirects the response to the file response.txt for later viewing.

    cscript httping.wsf web.foo.com cgi-bin/moeba.cgi?act=status

      GETs resource /cgi-bin/moeba.cgi?act=status of web.foo.com and
      displays the response text on the console.

    cscript httping.wsf web.bar.com /port:8080 /timeout:5000

      GETs default root page of [URL unfurl="true"]www.bar.com[/URL] on port 8080, with a timeout
      of 5 seconds (5000 ms).
    </example>
  </runtime>
  <object id="W3Sock" progid="socket.tcp"/>
  <script language="VBScript">
    Option Explicit
    Dim Host, URI, Port, TimeOut, Response

    Sub GetOptions()
      With WScript.Arguments
        With .Unnamed
          If .Count < 1 Then
            WScript.Arguments.ShowUsage
            WScript.Quit 1
          Else
            Host = .Item(0)
            If .Count < 2 Then
              URI = ""
            Else
              URI = .Item(1)
            End If
          End If
        End With
        With .Named
          If .Exists("port") Then
            Port = .Item("port")
          Else
            Port = "80"
          End If
          If .Exists("timeout") Then
            TimeOut = CLng(.Item("timeout"))
          Else
            TimeOut = 1000
          End If
        End With
      End With
    End Sub

    Sub Check(ByVal Num, ByVal Desc, ByVal RetCode)
      If Num <> 0 Then
        WScript.Echo _
            "Error " _
          & CStr(RetCode) _
          & " &H" _
          & Hex(Num) _
          & " " _
          & Desc
        W3Sock.Close
        WScript.Quit RetCode
      End If
    End Sub

    Sub ReturnHTTPStatus()
      Dim Status

      Status = InStr(1, Response, " ") + 1
      WScript.Quit CLng(Mid(Response, Status, 3))
    End Sub

    GetOptions

    With W3Sock
      .Host = Host & ":" & Port
      .TimeOut = TimeOut

      On Error Resume Next
      .Open
      Check Err.Number, Err.Description, 10
      .SendLine "GET /" & URI & " HTTP/1.0" & vbNewLine
      Check Err.Number, Err.Description, 20
      .WaitForDisconnect
      Check Err.Number, Err.Description, 30
      Response = .Buffer
      Check Err.Number, Err.Description, 30
      WScript.Echo Response
      Check Err.Number, Err.Description, 40
      .Close
      ReturnHTTPStatus
    End With
  </script>
</job>
Entering [tt]cscript httping.wsf /?[/tt] gets command help embedded in this script.

It would take very little to rewrite the script to use an XMLHTTPRequest component instead of w3 Sockets.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top