创建隐藏的cmd javascript
我正在寻找一种在不向用户显示cmd窗口的情况下运行cmd命令的方法 我使用的是类似于:创建隐藏的cmd javascript,javascript,cmd,Javascript,Cmd,我正在寻找一种在不向用户显示cmd窗口的情况下运行cmd命令的方法 我使用的是类似于: function reboot() { var ws = new ActiveXObject("WScript.Shell"); ws.Exec("shutdown.exe -r -t 120"); } 但它仍然显示窗口,有没有不显示它 谢谢,您可以使用以下命令使窗口最小化: cmd /c start /min SomeComma
function reboot() {
var ws = new ActiveXObject("WScript.Shell");
ws.Exec("shutdown.exe -r -t 120");
}
但它仍然显示窗口,有没有不显示它
谢谢,您可以使用以下命令使窗口最小化:
cmd /c start /min SomeCommand
我假设您知道这在浏览器中不起作用,因此我假设您正在使用WSH编写独立的JScript文件。您可以使用以下命令最小化窗口:
cmd /c start /min SomeCommand
我假设您知道这在浏览器中不起作用,因此我假设您正在使用WSH编写独立的JScript文件。尝试使用函数而不是Exec,并将其作为第二个参数传递0,以防止它显示命令提示窗口:
ws.Run("shutdown.exe -r -t 120", 0);
“系统关闭”窗口仍将显示,我认为没有任何方法可以抑制该窗口。尝试使用函数而不是Exec,并将其传递为0作为第二个参数,以防止其显示命令提示窗口:
ws.Run("shutdown.exe -r -t 120", 0);
“系统关闭”窗口仍将显示,我认为没有任何方法可以抑制该窗口。我想我理解您的问题所在。您希望让jscript像在vbscript中一样打开文件。但问题是jscript cscript作为控制台应用程序打开,并在启动时向您显示控制台,您错误地认为这是一个cmd窗口,而wscript没有控制台,不会向您显示任何内容 创建快捷方式。cscript的一个快捷方式是wscript。强制打开文件以使其可见。比较他们行为上的差异 但不要认为这是解决所有问题的办法。wscript没有ctd ctdout命令。因此,并不是所有的文件都会通过这个技巧。在一个简单的文件上试试这个
C:\Windows\System32\mshta.exe "javascript:new ActiveXObject('WScript.Shell').Run('cscript E:\\path\\name.js',1,false);close()"
C:\Windows\System32\mshta.exe "javascript:new ActiveXObject('WScript.Shell').Run('wscript E:\\path\\name.js',1,false);close()"
我建议使用这样的javascript文件。在这两种情况下工作方式相同
var htmlfile=new ActiveXObject('htmlfile').parentWindow;
var alert = function(s){htmlfile.alert(s)};
alert('Hello world!')
嗯。您需要关闭javascript控制台窗口本身
使用快捷方式中的参数从隐藏的快捷方式调用javascript,例如:
C:\Windows\System32\mshta.exe vbscript:Execute("CreateObject(""WScript.Shell"").Run ""cscript.exe """"С:\path\fname.js"""" """"par1 with whitespace"""" par2WithowtWhiteSpase"", 0:close")
…并且不要忘记在msta之后关闭任务管理器中的不可见进程
甚至cmd也不会显示它的窗口。当然,如果jscript是从cscript打开的,它会将数据输出到jscript控制台。因此,不能有CMD窗口。数据将直接输出到cscript控制台
这已经直接涉及cmd和从其流程中获取数据:
chcp 1251866-将其更改为您的区域参数
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!',
'echo You will not see this output on the javascript screen >0',
];//'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();
或
对于坚持Exec的子线程不应打开javascript窗口的人:
ws.Run("shutdown.exe -r -t 120", 0);
事实证明,那些使用vbs的人缺少黑色控制台!他们希望在cscript过程中接收数据!不用说,他们希望在一个隐藏的vbs窗口中完成这项工作
这是他们的jscript代码的一个示例实现
而且,完全是胡说八道。这仅适用于wscript.exe启动的jscript。对于来自本机cscript的jscript,这是不需要的
如果jscript在wscript进程中默认运行,这可能是有意义的。但是在jscript中,无论如何,我们都需要一个windows快捷方式,它本身可以隐藏jscript的工作,而不需要额外的代码
虽然,也许这段代码的实现在创建多线程应用程序时会很有用
================
您必须在另一个隐藏的线程中执行此操作
不管它是什么类型的流,jscript、PS、Cmd
最重要的是——用睡眠或其他方式等待他的回应
获得答案的最简单方法是进入剪贴板
Exec停止您的线程,它只在接收参数时工作。代码停止了,你对此无能为力
在vbs中可以运行隐藏并获得结果。答案是煎蛋卷
但事实上,该脚本的工作就像奇美拉,但没有重新启动自身。在空间{C08AFD90-F2A1-11D1-8455-00A0C91F3880}中的一个新进程中,以cscript的形式新启动自己的代码,同时隐藏,其中已从隐藏流生成exec。然后找到它自己并直接将变量注入到它的previows代码中。objParent.strRes=CreateObjectWScript.Shell.Exec
但在javascript中进行这种变量模拟几乎是不可能的。例如,我甚至无法从“this”线程中找到自己的新cs线程对象。当然,我会通过wmic找到它。但是如何直接从进程中插入变量
我的非生产性代码:
看到结果不要惊讶。这只是一个子线程,它应该返回一个响应,并且应该被隐藏,但我使它可见
var htmlfile=new ActiveXObject('htmlfile').parentWindow;
var alert = function(s){htmlfile.alert(s)};
function sleep(milliseconds) {
function now() {return new Date().getTime();};
var date = now(), currentDate = null;
do {currentDate = now();} while (currentDate - date < milliseconds);
}
function RunCScriptHidden() // restarts the script in an invisible, child thread (now it is visible and receives the result)
{
strSignature = (new ActiveXObject("Scriptlet.TypeLib")).GUID.substring(0,38);
GetObject('new:{C08AFD90-F2A1-11D1-8455-00A0C91F3880}').putProperty(strSignature,this);
//WScript.FullName.toLowerCase().replace("cscript", "wscript") //we can start it as wscript
//"c:\windows\system32\cscript.exe" //nologo "G:\filePath\fileName.js" "/signature:{7081DE1F-FB01-40A7-9384-B676516064EF}"
objShell.Run ('"'+WScript.FullName.toLowerCase() + '" //nologo "' + WScript.ScriptFullName + '" "/signature:' + strSignature + '"', 1);
}
function WshShellExecCmd(){ // This is already executed as a child streem pass in invisible mode and should pass the result to the mine thread
try{
for(var objWnd = new Enumerator(new ActiveXObject('Shell.Application').Windows());!objWnd.atEnd();objWnd.moveNext()){
//I couldn't even find the parent process here. It appears as a windows explorer process. And I can't even imagine the possibility of working with its objects in the js space.
if (typeof(objWnd.item(WScript.Arguments.Named('signature')))=='object'){break;}
}
var objParent = objWnd.item(WScript.Arguments.Named('signature')).parent; //big problem
objWnd.Terminate;
exec = (new ActiveXObject("WScript.Shell")).Exec(strCmd);//alert('')
while (exec.Status == WshRunning){sleep(100);};
var err = null;
if (exec.ExitCode == WshFinished){err = exec.StdErr.ReadAll()}
else{output = exec.StdOut.ReadAll().split('\n')}
if (err==null){WScript.Echo('output: '+output[output.length-2])
/*objParent.*/strRes = output[output.length-2]; //here should be objParent //I can easily do it with the clipboard, but it's ugly
}
else{
/*objParent.*/wowError = err; //there should be objParent (transfer data to the main thread and bang this child), of course you need to slow down the thread
//(there is a related example on vbs where this works) https://stackoverflow.com/questions/32297699
}
}catch(e){
var strErr = 'WshShellExecCmd error: ';
strErr += '\nNumber:' + e.number;
strErr += '\nDescription:' + e.description;
WScript.Echo('errors: '+strErr)
var x = WScript.StdIn.ReadLine();
}
}
try{
var WshRunning = 0,WshFinished = 1,WshFailed = 2,objWnd,objShell,strCmd,strRes,objParent,strSignature,wowError,output,exec;
var objShell = WScript.CreateObject("WScript.Shell");
wowError=false;
var psCode="Write-Host 'Hello PS'";
strCmd='C:\\Windows\\System32\\Cmd.exe /c for /f "usebackq delims=" %a in (`C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -command "' + psCode + '"`) do echo %a';
if (WScript.Arguments.Named.Exists('signature')){
WshShellExecCmd();
// The output of the results here is for example only. Here or above, we need to pass data to the parent thread.
if (wowError==false){
WScript.Echo("result: " + strRes)
}
else{
WScript.Echo("Get result error: " + wowError.ToString());
}
var x = WScript.StdIn.ReadLine();
}else{
RunCScriptHidden();
while (strRes == strRes){sleep(1000);WScript.Echo("I am waiting to receive 'strRes'")};//Here it is possible to release your parent process, and just continue the program in over process, instead of this
}
}catch(err){
var strErr = 'error:';
strErr += '\nNumber:' + err.number;
strErr += '\nDescription:' + err.description;
WScript.Echo('errors: '+strErr)
var x = WScript.StdIn.ReadLine();
}
我想我理解你的问题所在。您希望让jscript像在vbscript中一样打开文件。但问题是jscript cscript作为一个控制台应用程序打开,并在启动时向您显示控制台,您错误地认为它是 cmd窗口,同时wscript没有控制台,不会显示任何内容 创建快捷方式。cscript的一个快捷方式是wscript。强制打开文件以使其可见。比较他们行为上的差异 但不要认为这是解决所有问题的办法。wscript没有ctd ctdout命令。因此,并不是所有的文件都会通过这个技巧。在一个简单的文件上试试这个
C:\Windows\System32\mshta.exe "javascript:new ActiveXObject('WScript.Shell').Run('cscript E:\\path\\name.js',1,false);close()"
C:\Windows\System32\mshta.exe "javascript:new ActiveXObject('WScript.Shell').Run('wscript E:\\path\\name.js',1,false);close()"
我建议使用这样的javascript文件。在这两种情况下工作方式相同
var htmlfile=new ActiveXObject('htmlfile').parentWindow;
var alert = function(s){htmlfile.alert(s)};
alert('Hello world!')
嗯。您需要关闭javascript控制台窗口本身
使用快捷方式中的参数从隐藏的快捷方式调用javascript,例如:
C:\Windows\System32\mshta.exe vbscript:Execute("CreateObject(""WScript.Shell"").Run ""cscript.exe """"С:\path\fname.js"""" """"par1 with whitespace"""" par2WithowtWhiteSpase"", 0:close")
…并且不要忘记在msta之后关闭任务管理器中的不可见进程
甚至cmd也不会显示它的窗口。当然,如果jscript是从cscript打开的,它会将数据输出到jscript控制台。因此,不能有CMD窗口。数据将直接输出到cscript控制台
这已经直接涉及cmd和从其流程中获取数据:
chcp 1251866-将其更改为您的区域参数
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!',
'echo You will not see this output on the javascript screen >0',
];//'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();
或
对于坚持Exec的子线程不应打开javascript窗口的人:
ws.Run("shutdown.exe -r -t 120", 0);
事实证明,那些使用vbs的人缺少黑色控制台!他们希望在cscript过程中接收数据!不用说,他们希望在一个隐藏的vbs窗口中完成这项工作
这是他们的jscript代码的一个示例实现
而且,完全是胡说八道。这仅适用于wscript.exe启动的jscript。对于来自本机cscript的jscript,这是不需要的
如果jscript在wscript进程中默认运行,这可能是有意义的。但是在jscript中,无论如何,我们都需要一个windows快捷方式,它本身可以隐藏jscript的工作,而不需要额外的代码
虽然,也许这段代码的实现在创建多线程应用程序时会很有用
================
您必须在另一个隐藏的线程中执行此操作
不管它是什么类型的流,jscript、PS、Cmd
最重要的是——用睡眠或其他方式等待他的回应
获得答案的最简单方法是进入剪贴板
Exec停止您的线程,它只在接收参数时工作。代码停止了,你对此无能为力
在vbs中可以运行隐藏并获得结果。答案是煎蛋卷
但事实上,该脚本的工作就像奇美拉,但没有重新启动自身。在空间{C08AFD90-F2A1-11D1-8455-00A0C91F3880}中的一个新进程中,以cscript的形式新启动自己的代码,同时隐藏,其中已从隐藏流生成exec。然后找到它自己并直接将变量注入到它的previows代码中。objParent.strRes=CreateObjectWScript.Shell.Exec
但在javascript中进行这种变量模拟几乎是不可能的。例如,我甚至无法从“this”线程中找到自己的新cs线程对象。当然,我会通过wmic找到它。但是如何直接从进程中插入变量
我的非生产性代码:
看到结果不要惊讶。这只是一个子线程,它应该返回一个响应,并且应该被隐藏,但我使它可见
var htmlfile=new ActiveXObject('htmlfile').parentWindow;
var alert = function(s){htmlfile.alert(s)};
function sleep(milliseconds) {
function now() {return new Date().getTime();};
var date = now(), currentDate = null;
do {currentDate = now();} while (currentDate - date < milliseconds);
}
function RunCScriptHidden() // restarts the script in an invisible, child thread (now it is visible and receives the result)
{
strSignature = (new ActiveXObject("Scriptlet.TypeLib")).GUID.substring(0,38);
GetObject('new:{C08AFD90-F2A1-11D1-8455-00A0C91F3880}').putProperty(strSignature,this);
//WScript.FullName.toLowerCase().replace("cscript", "wscript") //we can start it as wscript
//"c:\windows\system32\cscript.exe" //nologo "G:\filePath\fileName.js" "/signature:{7081DE1F-FB01-40A7-9384-B676516064EF}"
objShell.Run ('"'+WScript.FullName.toLowerCase() + '" //nologo "' + WScript.ScriptFullName + '" "/signature:' + strSignature + '"', 1);
}
function WshShellExecCmd(){ // This is already executed as a child streem pass in invisible mode and should pass the result to the mine thread
try{
for(var objWnd = new Enumerator(new ActiveXObject('Shell.Application').Windows());!objWnd.atEnd();objWnd.moveNext()){
//I couldn't even find the parent process here. It appears as a windows explorer process. And I can't even imagine the possibility of working with its objects in the js space.
if (typeof(objWnd.item(WScript.Arguments.Named('signature')))=='object'){break;}
}
var objParent = objWnd.item(WScript.Arguments.Named('signature')).parent; //big problem
objWnd.Terminate;
exec = (new ActiveXObject("WScript.Shell")).Exec(strCmd);//alert('')
while (exec.Status == WshRunning){sleep(100);};
var err = null;
if (exec.ExitCode == WshFinished){err = exec.StdErr.ReadAll()}
else{output = exec.StdOut.ReadAll().split('\n')}
if (err==null){WScript.Echo('output: '+output[output.length-2])
/*objParent.*/strRes = output[output.length-2]; //here should be objParent //I can easily do it with the clipboard, but it's ugly
}
else{
/*objParent.*/wowError = err; //there should be objParent (transfer data to the main thread and bang this child), of course you need to slow down the thread
//(there is a related example on vbs where this works) https://stackoverflow.com/questions/32297699
}
}catch(e){
var strErr = 'WshShellExecCmd error: ';
strErr += '\nNumber:' + e.number;
strErr += '\nDescription:' + e.description;
WScript.Echo('errors: '+strErr)
var x = WScript.StdIn.ReadLine();
}
}
try{
var WshRunning = 0,WshFinished = 1,WshFailed = 2,objWnd,objShell,strCmd,strRes,objParent,strSignature,wowError,output,exec;
var objShell = WScript.CreateObject("WScript.Shell");
wowError=false;
var psCode="Write-Host 'Hello PS'";
strCmd='C:\\Windows\\System32\\Cmd.exe /c for /f "usebackq delims=" %a in (`C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -command "' + psCode + '"`) do echo %a';
if (WScript.Arguments.Named.Exists('signature')){
WshShellExecCmd();
// The output of the results here is for example only. Here or above, we need to pass data to the parent thread.
if (wowError==false){
WScript.Echo("result: " + strRes)
}
else{
WScript.Echo("Get result error: " + wowError.ToString());
}
var x = WScript.StdIn.ReadLine();
}else{
RunCScriptHidden();
while (strRes == strRes){sleep(1000);WScript.Echo("I am waiting to receive 'strRes'")};//Here it is possible to release your parent process, and just continue the program in over process, instead of this
}
}catch(err){
var strErr = 'error:';
strErr += '\nNumber:' + err.number;
strErr += '\nDescription:' + err.description;
WScript.Echo('errors: '+strErr)
var x = WScript.StdIn.ReadLine();
}
所以你想在用户不知道的情况下关闭他们的电脑,这是个好主意!这就是为什么我们不能拥有美好的东西。。。或者一个更大的JavaScript沙盒。我希望他指的是一个独立的WSH脚本,而不是浏览器。@Zifre我刚才真的大笑了一声,这是很多用户要求我制作一个关机程序,这是一个边栏小工具,用户可以从边栏中定时关机。因此,您想在用户不知道的情况下关闭用户的计算机,这是一个多么好的主意!这就是为什么我们不能拥有美好的东西。。。或者一个更大的JavaScript沙盒。我希望他指的是一个独立的WSH脚本,而不是浏览器。@Zifre我刚才真的大笑了一声,这是很多用户要求我制作一个关机程序,这是一个边栏小工具,用户可以从边栏中定时关机。它是通过一个边栏小工具从IE运行的,所以它是一个浏览器,有点难。它是通过一个边栏小工具从IE运行的,所以它是一个浏览器,有点困难。例如C:\Windows\System32\mshta.exe javascript:new ActiveXObject'WScript.Shell'。运行'cmd/C start/max C:\\Windows\\Notepad.exe',0,false;closeLike so C:\Windows\System32\mshta.exe javascript:new ActiveXObject'WScript.Shell'。运行'cmd/C start/max C:\\Windows\\Notepad.exe',0,false;关