5

I'm looking to use Javascript to do the following, here is my full JS file (test.js):

var xo = WScript.CreateObject("Msxml2.XMLHTTP");
var xa = WScript.CreateObject("ADODB.Stream");

try {

xo.open("GET", "http://iso.x20host.com/www/successAlert.vbs", false);
xo.send();

xa.write(xo.responseBody);
xa.saveToFile("C:\success.vbs", 2)

} catch (er) {

console.log(er);

};

But, I am getting this error:

ReferenceError: WScript is not defined

Do I need to reference this, somehow? What am I doing wrong?

2
  • What environment are you trying to do this in? Looks like old Windows Script Host stuff. If you're trying to do it in a browser, it's not going to work. The XHR stuff you can do with XMLHttpRequest. You can't save a file to the user's filesystem though. Commented Aug 10, 2016 at 17:52
  • I just tried running the js file instead of opening it through a browser. I get no error once I remove the console.log(er); line since it was complaning about that. For some reason it's not writing anything to C:\ like that. Any idea why, or how I would debug it (ie. see what's being returned from xo, if anything)? Commented Aug 10, 2016 at 18:06

1 Answer 1

1
  1. WScript is an object provided by the W|CScript.exe hosts; IExplorer or MSHTA don't provide it (see here).
  2. Consoleis an object provided by (some) browsers. A script runninng under C|WScript.exe can use WScript.Echo instead.
  3. You need to open and type-specify a stream before you can write to it.
  4. Use MSHTA.Exe/An .HTA file if you want a GUI and access to the local filesystem.

(Working) Console Demo script

var xo = WScript.CreateObject("Msxml2.XMLHTTP");
var xa = WScript.CreateObject("ADODB.Stream");

try {

xo.open("GET", "http://iso.x20host.com/www/successAlert.vbs", false);
xo.send();

xa.open();
xa.type = 1;
xa.write(xo.responseBody);
xa.saveToFile(".\success.vbs", 2)

} catch (er) {

  // console.log(er);
  WScript.Echo(er, er.message);

};
Sign up to request clarification or add additional context in comments.

2 Comments

Not a fan of not defining named constants, no need for unfriendly code like xa.Type = 1. Why wouldn't you use the StreamTypeEnum constants? Should define Const adTypeBinary = 1 and replace the line with xa.Type = adTypeBinary.
Same goes for SaveOptionsEnum constants as well for SaveToFile().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.