Search code examples
batch-filejscriptwsh

Return cscript output to batch


My batch script is performing an HTTP request by using CScript with Javascript syntax like in my example.

Using this approach (also seen here) and some help on escaping i tried the following:

@if (@This==@IsBatch) @then
@echo off
rem **** batch zone *********************************************************
    setlocal enableextensions EnableDelayedExpansion

    set OWNPATH="%~dpnx0"

    if not "%~11"=="" (
        FOR /F "usebackq tokens=*" %%r in (`cscript //E:JScript %OWNPATH%`) DO SET RESULT=%%r
        ECHO %RESULT%
    )

    exit /b

@end
// **** JScript zone *****************************************************
// Instantiate the needed component to make url queries
var http = WScript.CreateObject('Msxml2.XMLHTTP.6.0');

// perform request
var requestURL = "http://myserver/api";

// Make the request 
http.open("GET", requestUrl, false);
http.send();

WScript.Echo(http.ResponseText);

// All done. Exit
WScript.Quit(0);

Unfortunately i get a message that "ECHO is off." and not the string in %RESULT%.

The script is run on a Windows 2008 R2 server.


Solution

  • Thanks to @Stephan it works when using

    echo !RESULT!
    

    due to the delayed expansion