Submitted By: Daniel Massengill

Downloads a program from a We site and then executes that program with or without user intervention. I usually use Windows Script Encoder before I send this to any of my customers. Usage: \\servername\sharename\downrunsilent.vbe http://www.boss-solutions.com/downloads/7z313.exe C:\temp "-y /q /r:n" or \\servername\sharename\downrunsilent.vbe http://www.boss-solutions.com/downloads/webinventory.exe C:\temp.

Visual Basic
Edit|Remove
Dim args, num
Dim var1, var2
Dim objFSO, objFolder
Dim objStream
Dim WshShell
Dim strFolder, strURL, strFile, strFull
Dim strUnat, strCmdLine, strDmdLine

'Sets and checks arguments
set args = WScript.Arguments
num = args.Count
If num = 2 Then
    'URL for the file
    strURL = args.Item(0)
    'Folder for the file to be downloaded to 
    strFolder = args.Item(1)
ElseIf num = 3 Then
    strURL = args.Item(0)
    strFolder = args.item(1)
    strUnat = args.item(2)
Else 
   Wscript.Echo "Usage: \\servername\sharename\downrunsilent.vbe http://www.website.com/executable.exe C:\temp"
    Wscript.Echo "For unattended options add them after download folder with a space and quotes."
    WScript.Quit
End If

'Converts URL to just filename
var1 = Split(StrReverse(strURL), "/")
var2 = StrReverse(var1(0))

'Sets path for file download
strFile = var2
strFull = strFolder & "\" & strFile
'wscript.echo "File download location is: " & strFull


'Verify local folder for file and creates it if doesn't exist
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strFolder) Then
    Set objFolder = objFSO.GetFolder(strFolder)
Else
    'Wcript.Echo "Download folder does not exist."
    'Wscript.Echo "Creating download folder."
    Set objFolder = objFSO.CreateFolder(strFolder)
End If

' Sets up an ADO Stream to download file
Set objStream = CreateObject("ADODB.Stream")
objStream.Type = 1 ' adTypeBinary

' The following starts the download
objStream.Open ("URL=" & strURL)
'wscript.echo "File downloaded"

' This saves the file to the workstation in the folder specified
objStream.SaveToFile strFolder & "\" & strFile, 2 ' adSaveCreateOverWrite
objStream.Close
Set objStream = Nothing

' Runs the downloaded file with or without unattended/silent options
If strUnat = "" Then
    'wscript.echo "Executing downloaded file"
    Set WshShell = CreateObject("WScript.Shell")
    WshShell.Run (chr(34) & strFull & chr(34))
    Set WsShell = Nothing
Else
    'WScript.echo "Executing downloaded file"
    Set WshShell = CreateObject("WScript.Shell")
    strCmdLine = strFull & " "
    strDmdLine = strCmdLine & strUnat
   WshShell.Run (strDmdLine)
   Set WsShell = Nothing
End If