Jump to content











Photo
- - - - -

Single-line command to download and run file from Windows


  • Please log in to reply
8 replies to this topic

#1 Nuno Brito

Nuno Brito

    Platinum Member

  • .script developer
  • 10549 posts
  • Location:boot.wim
  • Interests:I'm just a quiet simple person with a very quiet simple life living one day at a time..
  •  
    European Union

Posted 06 April 2014 - 06:42 PM

Friends,

 

I was looking at the website of a newish company. They have something nice, to try their tool immediately you can run a CURL command and execute the installer right away.

 

Their site detected that I'm using Windows and invited me to try this command:

curl -O download.bowery.io/downloads/bowery_2.1.0_windows_amd64.zip && sudo unzip bowery_2.1.0_windows_amd64.zip -d /usr/local/bin

As you might notice, the command fails because CURL is not available by default on Windows. Perhaps available on Linux and OSX? :huh:

 

In either case, I liked this simple concept to get things running and went ahead to see how this could be done on Windows (I'm running Windows 7).

 

Eventually, found BitsAdmin to work ok for this goal. It has some quirks of its own but can be used. On my test, I placed a batch file that would be downloaded and executed. Here is the syntax:

bitsadmin /transfer test http://triplecheck.de/now %temp%\now.bat & %temp%\now.bat

To ensure that you're not running something dangerous, you can check the content of now.bat directly at http://triplecheck.de/now from your browser.

 

The "&" operator is probably equivalent to "&&" in Unix (I'm somewhat more used to ";") and the %temp% is a variable that points to the Windows temporary folder. It seems that bitadmin doesn't work with specifying where the file will be downloaded.

 

From the now.bat is possible to automate more elaborated steps:

- create a folder on disk

- get the big zip package with the software

- extract all files from the zip package to final location

- launch the software

 

 

Hope you find this tidybit useful. For me it was fun to see this working.

 

:cheers:



#2 Nuno Brito

Nuno Brito

    Platinum Member

  • .script developer
  • 10549 posts
  • Location:boot.wim
  • Interests:I'm just a quiet simple person with a very quiet simple life living one day at a time..
  •  
    European Union

Posted 06 April 2014 - 10:20 PM

I've kept on working on this concept. It is relatively easy to script an installer. I've done this for the TripleCheck software.

 

If you want to try it, this is the command to run from the Windows prompt:

bitsadmin /transfer t http://triplecheck.de/launch %temp%\x.bat&%temp%\x.bat

This command will download a batch script and then start the install.

 

I noted that bitsadmin is very slow to download files, so I'm using wget.exe to speed up the process. My software is based on Java so the batch script gets the needed files in case there is no Java available. To ease folder placement, I'm using c:\triplecheck to avoid the long folder name limitations.

 

There might be issues in the future with Antivirus, they might not be happy if we download from the Internet wget.exe. A possible improvement is checking if wget is present or not in the folder and just use the sluggish Microsoft downloader when necessary.

 

It was getting difficult to extract zip files under Windows using only native tools. Likely it would be possible to use .vbs file to wrap around the Win32 API that access the zip extraction but in the end I just used cabinet files (.cab) as archives. However, this does force to maintain two separate types of archives, I see room to improve the script with zip extraction.

 

The last part is creating a shortcut on the user desktop, I simply created one shortcut on my own computer and then copy it over. Didn't really looked much into how to generate the shortcut with Windows tools.

 

The end result is quite nice, from a single line of code the end-user gets the fully deployed application running.

 

In case someone is interested, I'm posting here the full script on its current state, you're welcome to adapt/reuse for your own needs:

@echo off
:: this is the launcher executed when someone types:
:: bitsadmin /transfer t http://triplecheck.de/launch %temp%\x.bat&%temp%\x.bat

:: Copyright TripleCheck (c) 2014
:: Author: Nuno Brito
:: Date: 2014-04-06
:: License terms: Careware License ( http://jaclaz.altervista.org/Projects/careware.html )

set drive=c:
set name=triplecheck
set folder=%drive%\%name%
set cabfile=triplecheck-recent.cab
set wget=%folder%\wget.exe
set shortcut=TripleCheck.lnk
set url=http://triplecheck.de/archive

:: first step, create the folder
mkdir %folder%

:: change to the related drive and folder
%drive%
cd\%name%

:: get our downloader in place
bitsadmin /transfer TripleCheck %url%/wget.exe %wget%


:: now get our triplecheck runtime
%wget% %url%/%cabfile%.zip %cabfile%.zip
ren %cabfile%.zip %cabfile%
expand %cabfile% -F:* %folder%
del %cabfile%

:: install the shortcut
%wget% -q %url%/misc/%shortcut%.zip %shortcut%.zip
ren %shortcut%.zip %shortcut%
copy %shortcut% "%UserProfile%\Desktop"\%shortcut%
del %shortcut%


:: do we have java installed?
:: test if we have java installed or not
"%JAVA_HOME%"\bin\java -version > nul 2>&1
if %ERRORLEVEL% == 0 goto end

:: no java found, let's download our own java version
wget %url%/java.zip java.cab
:: rename from .zip to .cab
ren java.zip java.cab
expand java.cab -F:* %folder%
del java.cab


:end
:: launch our software
echo opening explorer
explorer .

echo launching triplecheck
%folder%\triplecheck.exe

:: delete our temporary files
del %wget%
del %temp%\x.bat

Can someone let me know if bitsadmin is still available in Windows 8? Thanks!

 

:cheers:



#3 sbaeder

sbaeder

    Gold Member

  • .script developer
  • 1338 posts
  • Location:usa - massachusettes
  •  
    United States

Posted 07 April 2014 - 07:06 PM

I'm already running the new Update for Win 8.1, and seems to be here just fine...But it does say deprecated, and to use "powershell" to do this sort of thing going forward...

Would have done a screen capture, but can't easily insert a picture directly...here is partial output from  "bitsadmin /? >foo.txt"


BITSADMIN version 3.0 [ 7.7.9600 ]
BITS administration utility.
(C) Copyright 2000-2006 Microsoft Corp.

BITSAdmin is deprecated and is not guaranteed to be available in future versions of Windows.
Administrative tools for the BITS service are now provided by BITS PowerShell cmdlets.

USAGE: BITSADMIN [/RAWRETURN] [/WRAP | /NOWRAP] command
The following commands are available:

/HELP           Prints this help 
/?              Prints this help 


(etc...)

  • Nuno Brito likes this

#4 Nuno Brito

Nuno Brito

    Platinum Member

  • .script developer
  • 10549 posts
  • Location:boot.wim
  • Interests:I'm just a quiet simple person with a very quiet simple life living one day at a time..
  •  
    European Union

Posted 07 April 2014 - 09:44 PM

Thank you for the feedback. On Windows 7 also has the same deprecated warning, they'd do better in simply removing the warning by now.
 
I mean, the scareware sign seems to be there since 2006 (at minimum) while other features of Windows like the Start Menu, cmd.exe and others were removed or hidden without such warning.. :)
 
In the meanwhile I've written the Linux equivalent of the script, significantly simpler command line invocation:

wget -O - http://triplecheck.de/linux.txt | bash

Wget with Bash present a very powerful option here. Can be used for running a shell script directly from a web server without need for downloading any file and keeping the command line quite elegant (and simple to read/write).

 

Very nice stuff. I like it for the simplicity of presenting people with a quick way to test the software.

 

:cheers:



#5 Icecube

Icecube

    Gold Member

  • Team Reboot
  • 1062 posts
  •  
    Belgium

Posted 09 April 2014 - 07:17 PM

I've kept on working on this concept. It is relatively easy to script an installer. I've done this for the TripleCheck software.
 
If you want to try it, this is the command to run from the Windows prompt:

bitsadmin /transfer t http://triplecheck.de/launch %temp%\x.bat&%temp%\x.bat 
This command will download a batch script and then start the install.
 
I noted that bitsadmin is very slow to download files, so I'm using wget.exe to speed up the process.

BITS uses idle bandwidth to transfer data. Normally, BITS transfers data in the background, i.e., BITS will only transfer data whenever there is bandwidth which is not being used by other applications, for example, when applications use 80% of the available bandwidth, BITS will use only the remaining 20%. BITS constantly monitors network traffic for any increase or decrease in network traffic and throttles its own transfers to ensure that other foreground applications (such as a web browser) get the bandwidth they need. BITS also supports resuming transfers in case of disruptions.

http://en.wikipedia....ransfer_Service

Maybe you need to change the priority of the job to e.g. FOREGROUND:
/Transfer name [type] [/priority job_priority] [/ACLFlags flags] remote_name local_name    Transfers one or more files. By default, BITSAdmin creates a download job that runs at NORMAL priority. BITSAdmin updates the command window with progress information until the transfer is complete or until a critical error occurs. BITSAdmin completes the job if it successfully transfers all the files and cancels the job if a critical error occurs. BITSAdmin does not create the job if it is unable to add files to the job or if you specify an invalid value for type or job_priority. Note that BITSAdmin continues to run if a transient error occurs. To end BITSAdmin, press Ctrl+C.    Use the name parameter to specify the name of the job.    The type parameter is optional. Use the type parameter to specify the type of job. Specify /download for a download job or /upload for an upload job.    The /priority parameter is optional. To specify the priority of the job, set the job_priority operand to FOREGROUND, HIGH, NORMAL, or LOW.    The /ACLFlags parameter is optional. The flags indicate that you want to maintain the owner and ACL information with the file being downloaded. Specify one or more of the following flags. For example, to maintain the owner and group with the file, set flags to OG.        O — Copy owner information with file.        G — Copy group information with file.        D — Copy DACL information with file.        S — Copy SACL information with file.    To transfer more than one file, specify multiple remote_name-local_name pairs. The pairs are space-delimited.
http://msdn.microsof...3(v=vs.85).aspx

#6 Icecube

Icecube

    Gold Member

  • Team Reboot
  • 1062 posts
  •  
    Belgium

Posted 09 April 2014 - 07:30 PM

Windows Explorer can download files via http. Just enter the URL into the Address bar. Or from the command line, eg: C:\windows\explorer.exe http://somewhere.com/filename.ext. You get the classic File Download prompt. Unless the file is a type that Windows Explorer knows how to display inline, (.html, .jpg, .gif), in which case you would then need to right-click save it.

I just tested this on my vmware image of a virgin install of WinXP 2002 SP1, and it works fine.

http://superuser.com/questions/59465/is-it-possible-to-download-using-the-windows-command-line

I can't test it here, but if it still works, you could try something like this:
explorer http://triplecheck.de/launch.bat


#7 Nuno Brito

Nuno Brito

    Platinum Member

  • .script developer
  • 10549 posts
  • Location:boot.wim
  • Interests:I'm just a quiet simple person with a very quiet simple life living one day at a time..
  •  
    European Union

Posted 09 April 2014 - 10:46 PM

Some people reported back that the Windows command only works on their machine when running from a cmd.exe with Administrator permissions.

 

Strange is that my own machine doesn't bother me about this kind of permission, must be something to do with the UAC sensitivity level. Basically both wget and bitsadmin get bogged down by admin rights on some machines. Under Linux it is no issue to call wget with plain permissions.

 

I've tried your advice with explorer. On my case it opened a firefox tab (my default browser) so I guess this varies too often to be considered as a reliable manner of downloading files.

 

The next command that comes to my mind in Windows is FTP. Available and fast enough. Would likely also need admin permission. :(

 

 

How else can we parse a file from the Internet as a batch script or similar? Any ideas? :huh:



#8 mcwilli4

mcwilli4
  • Members
  • 5 posts
  • Location:Sunnyvale, CA
  • Interests:powershell, mdt, cats, linux (suse and redhat & debian), buffets
  •  
    United States

Posted 10 April 2014 - 02:46 AM

Powershell lets you do it using Invoke-WebRequest or the below way does it too .. I like that you can add the powershell modules to PE. Also, Powershell ISE is a complete scripting environment. Strange stuff if you haven't used it are NuGets .. like from nuget.org, it's like Visual Studio stuff.. easiest way to try it is by using chocolatey..From the cmd line, to install it you just paste this: 

 @powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin


  • Nuno Brito likes this

#9 Nuno Brito

Nuno Brito

    Platinum Member

  • .script developer
  • 10549 posts
  • Location:boot.wim
  • Interests:I'm just a quiet simple person with a very quiet simple life living one day at a time..
  •  
    European Union

Posted 10 April 2014 - 10:18 AM

Very good tip. Thanks! :)

 

Its seems to be available on all Windows machine from XP SP3 and above: http://en.wikipedia....ell#Version_2.0

 

:cheers:






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users