Try this batch+inline powershell
trick. I have used this kind of inlining in my random batch scripts. If you want to see utf8 characters you may need to change a default console font, I have not done that.
Use myscript.bat
and should see myscript_output.log
and myscript_outputPS.log
output files. This does not have any failsafe to handle missing json fields.
Passing powershell variable back to a batch context use Write-Host
to output STDOUT. After that use FOR..DO @SET
to read output to an environment variable. This does not handle well multiline replys but is fine for a single string values.
You may use for-do trick for catching any .exe console binary output, such as delphi/rust/anything little helper .exe console binary files.
@REM https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=ja&dt=t&q=%E2%80%9Chello%E2%80%9D
@set scriptfolder=%~dp0%
@set script=%~n0
@REM activate utf8 encoding in batch script
@chcp 65001
set retval=
Call :CallUrlPS
@echo retval=%retval%
@echo %retval% > %scriptfolder%%script%_output.log
@GOTO :END
@REM ==================================
:CallUrlPS
@SET retval=
@set cmd=^
$data = @('',''); ^
$objRet=Invoke-WebRequest 'https://translate.googleapis.com/translate_a/single?client=gtx^&sl=en^&tl=ja^&dt=t^&q=%%E2%%80%%9Chello%%E2%%80%%9D'; ^
$data[0]=$objRet.Content; ^
$data[1]=$objRet.StatusCode; ^
$data[0]=$data[0].Trim(); ^
Set-Content -Path '%scriptfolder%%script%_outputPS.log' -Encoding utf8 -Value $data[0]; ^
$objJson = ConvertFrom-Json $data[0]; ^
Write-Host $objJson[0][0][0]; ^
;
@SET setarg=powershell -NoLogo -Noninteractive -InputFormat none -Command "%cmd%"
@FOR /F "tokens=*" %%a IN ('%setarg%') DO @SET retval=%%a
@GOTO :EOF
:END
pause