0Day Forums
Run cmd commands in JScript - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: JScript (https://0day.red/Forum-JScript)
+--- Thread: Run cmd commands in JScript (/Thread-Run-cmd-commands-in-JScript)



Run cmd commands in JScript - elvaelvah162 - 07-24-2023

I have a cmd command which I need to execute in JScript. So, I'm trying the next sequence of code:

var WshShell = new ActiveXObject("WScript.Shell");
sevenZip = "C:\\Program Files\\7-Zip\\7z.exe";
WshShell.Run("cmd.exe /c '"+sevenZip+" e "+File+" -p"+Pass+"'");

But when I launch the script, the cmd window opens, then closes, but the command doesn't seem to be executed. What am I doing Wrong?


RE: Run cmd commands in JScript - hemodromograph19657 - 07-24-2023

I'd recommend using `Shell.Aplication` instead. Here's the code how it should look like :

var objShell = new ActiveXObject("Shell.Application");
sevenZip = "C:\\Program Files\\7-Zip\\7z.exe";
executableCommand = "x " + zipFile + " -p" + zipPass + " -o" + Temp;
objShell.ShellExecute("7z.exe", executableCommand, sevenZip , "open", 0);




RE: Run cmd commands in JScript - Rodrigus540692 - 07-24-2023

chcp 1251,866 - change it to your regional parameters

var htmlfile=new ActiveXObject('htmlfile').parentWindow;
var alert=function(s){htmlfile.alert(s)};
var echoIt='Hi'
//...solve the problem of doubling percentages in variables during transmission by introducing the parameter /v!
var cmdCode=[
'for /F "tokens=1-6 delims=:., " %A In ("!date! !time!") Do (Echo %A.%B.%C %D:%E:%F)',
'set var='+echoIt,
'echo %var%',
'echo !var!'
];//'setlocal disableDelayedExpansion' parameter is useless becose /v
var std, arr=[];
var WshRunning=0,WshFinished=1,WshFailed=2;var i=0;tryCount=0,output=[],errors=[];
with (new ActiveXObject('WScript.Shell')) {
std=Exec("cmd /v /q /k echo off"); //Turning on the /v option last, switches the output here in its entirety, including C:\...\notepad>
std.StdIn.WriteLine("chcp 1251>nul");
for(var i=0;i<cmdCode.length;i++) {
std.StdIn.WriteLine(cmdCode[i]);
};
std.StdIn.WriteLine("chcp 866>nul");
std.StdIn.WriteLine("exit");
do{
if (std.Status==WshFailed){
errors.push('Error in string '+i+' :\n '+std.StdErr.ReadLine());
tryCount++
}
else if(std.Status==WshRunning){
output.push(std.StdOut.ReadLine());
tryCount=0;
WScript.Echo('Running ...')
}
else if(std.Status==WshFinished){
var last=std.StdOut.ReadLine();
if(last.length>0){output.push(last)};last=undefined;
tryCount=21;
WScript.Echo('Finished ...')
}i++;
}while(tryCount<21);
}
WScript.Echo('')
if (output.length>0){for(var i=0;i<output.length;i++) {WScript.Echo(output[i])}}
if (errors.length>0){alert(errors)}
var x=WScript.StdIn.ReadLine();

or

var std, arr = [];
var oWsh = new ActiveXObject("WScript.Shell");
with (new ActiveXObject('WScript.Shell')) {
std = Exec("cmd /q /k chcp 1251>nul");
with (std.StdIn){
WriteLine("dir "+ oWsh.SpecialFolders("Desktop"));
WriteLine("exit");
}
arr = std.StdOut.ReadAll().split('\n'); //чтение данных
}
for (var i = 0; i < arr.length; i++) {
WScript.echo(i+' '+arr[i]);
}
var x = WScript.StdIn.ReadLine();