mshta usage

mshta usagemshtaisshortforMicroSoftHtmlApplication.Itcouldrunhtmlfileorhtmlstringasaparameter.whatisinterestingisyoucanuseitinawindowbatchcommandorbatchfileandyoucanusesome

大家好,又见面了,我是你们的朋友全栈君。

  • mshta is short for MicroSoft Html Application. It could run html file or html string as a parameter.
  • what is interesting is you can use it in a batch command or batch file and you can use some functions like in the browser such as show a dialog and so on.
  • with mshta you can run javascript code and vbscipt code in windows command line or batch file.

Example 1, show a message box in many different ways.

mshta javascript:window.execScript("msgBox('hello world!'):window.close","vbs") 
mshta vbscript:window.execScript("alert('hello world!');close()","javascript") 
mshta vbscript:window.execScript("msgBox('hello world!'):window.close","vbs")
mshta javascript:alert("hello world!");close()
mshta vbscript:msgbox("hello world!",64,"Title")(window.close)
mshta vbscript:CreateObject("Wscript.Shell").popup("hello world!",7,"Title",64)(window.close) 

Example 2, execute more than one commands in one line.(In fact this already shows in Example 1, you must find it, it is the close command.)

mshta vbscript:execute("msgbox ""BOX one"":msgbox ""BOX two"":window.close") 
mshta vbscript:(msgbox("BOX one"))(msgbox("BOX two")(window.close))
mshta javascript:alert("BOX one",);alert("BOX two");close()
mshta javascript:execScript("msgBox('BOX one'):msgBox('BOX two'):window.close","vbs")

Example 3, use ActiveXObject in script to use more sophisticated functions of the OS.

mshta vbscript:createobject("sapi.spvoice").speak("Hello, I am tom, let's do something fun.")(window.close) mshta "javascript:close((V=(v=new ActiveXObject('SAPI.SpVoice')).GetVoices()).count&&v.Speak('Hello! I am '+V(0).GetAttribute('Gender')))"

Example 4, javascript is much more easy to use, because you do not have to add so many double qoutes like in vbscript.

mshta "javascript:var objFSO=new ActiveXObject('Scripting.FileSystemObject'); var objFile = objFSO.CreateTextFile('test.txt',true); objFile.Write('Hello World.');objFile.Close();close();" mshta "javascript:var sh=new ActiveXObject( 'WScript.Shell' ); sh.Popup( 'hello world!', 10, 'Title!', 64 );close()"

Example 5, several ways to calculate the free memory of your computer.

mshta "javascript:close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(GetObject('winmgmts:').ExecQuery('Select * from Win32_PerfFormattedData_PerfOS_Memory').ItemIndex(0).AvailableBytes));"|more
for  /f "usebackq" %a in (`mshta ^"javascript^:close^(new ActiveXObject^(^'Scripting.FileSystemObject^'^).GetStandardStream^(1^).Write^(GetObject^(^'winmgmts:^'^).ExecQuery^(^'Select * from Win32_PerfFormattedData_PerfOS_Memory^'^).ItemIndex^(0^).AvailableBytes^)^);^"^|more`) do set free_mem=%a
mshta "javascript:close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(GetObject('winmgmts:').ExecQuery('Select * from Win32_PerfFormattedData_PerfOS_Memory').ItemIndex(0).AvailableMBytes));"|for /f %%a in ('more') do set free_mem=%%a

Example 6, another way to calculate free memory.

@echo off
setlocal
:: Define simple macros to support JavaScript within batch
set "beginJS=mshta "javascript:close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(" set "endJS=));""
:: Direct instantiation requires that output is piped
%beginJS% GetObject('winmgmts:').ExecQuery('Select * from Win32_PerfFormattedData_PerfOS_Memory').ItemIndex(0).AvailableBytes %endJS% | findstr "^"
:: FOR /F does not need pipe
for /f %%N in ( '%beginJS% GetObject('winmgmts:').ExecQuery('Select * from Win32_PerfFormattedData_PerfOS_Memory').ItemIndex(0).AvailableBytes %endJS%' ) do set free_mem=%%N echo free_mem=%free_mem%

Example 7, show color pallets dialog.

mshta "about:<script>function b(){ 
    new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(d.ChooseColorDlg().toString(16));close();}</script><body onload='b()'><object id='d' classid='clsid:3050f819-98b5-11cf-bb82-00aa00bdce0b'></object></body>"|more 

Example 8, show screen resolution.

mshta "javascript:res=screen.width+'x'+screen.height;alert(res);close();" 1 | more

Example 9, use clipboard data.

for /f "usebackq tokens=1,* delims=[]" %i in (`mshta "javascript:close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(clipboardData.getData('Text')));"^|find /v /n ""`) do @set "c[%i]=%j" 

Example 10, use javascript setTimeout to delay some time interval to execute a command.

mshta javascript:setTimeout('close()',10000)

Example 11, show File Open Select Dialog, you can redirect your output to the command window or to a file or to a variable.

mshta "about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);</script>"|more

mshta "about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();</script>">test.txt

mshta "about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();</script>">temp && set /p a=<temp for /f "delims=" %%i in ('mshta "about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();</script>"') do set a=%%i

Example 12, show Open For Folder Dialog to select a directory, and you can assign it to a variable.

for /f "delims=" %%i in ('mshta "javascript:var folder=new ActiveXObject("Shell.Application").BrowseForFolder(0,'选择要处理的文件夹', 0, '').self.path;new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(folder);close();"') do set input=%%i

Example 13, execute batch command with parameters which has spaces.

::a.bat
@echo off
set text=Hello World
mshta vbscript:createobject("WScript.Shell").Run("b.bat "+"""%text%""",0)(window.close)
::b.bat
@echo off
mshta "javascript:var sh=new ActiveXObject( 'WScript.Shell' ); sh.Popup( '%~1', 10, 'Title!', 64 ); close()"

Example 14, ultimate function, run a html file.

<!-- :: Batch section @echo off setlocal echo Select an option: for /F "delims=" %%a in ('mshta.exe "%~F0"') do set "HTAreply=%%a" echo End of HTA window, reply: "%HTAreply%" goto :EOF -->

<HTML>
<HEAD>
<HTA:APPLICATION SCROLL="no" SYSMENU="no" >

<TITLE>HTA Radio Buttons</TITLE>
<SCRIPT language="JavaScript"> window.resizeTo(440,170); var reply = "No button selected"; function closeHTA(){ 
     var fso = new ActiveXObject("Scripting.FileSystemObject"); fso.GetStandardStream(1).WriteLine(reply); window.close(); } </SCRIPT>
</HEAD>
<BODY>
<p>Which prize do you prefer?</p>
<label><input type="radio" name="prize" onclick="reply=this.value" value="House">House</label>
<label><input type="radio" name="prize" onclick="reply=this.value" value="Money">$1 million</label>
<label><input type="radio" name="prize" onclick="reply=this.value" value="None">No prize thanks, I'm already happy <b>:)</b></label>
<br><br>
<button onclick="closeHTA();">Submit</button>
</BODY>
</HTML>

reference1
reference2
reference3

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/159687.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 离散 单射 满射 双射

    离散 单射 满射 双射单射双射满射阐述一下什么是单射,双射,满射1.单射:对于每一个不同的x都有不同的y,即x1!=x2–>y1!+y2条件:|X|<=|Y|2.满射:对于每一个y都有x与之对应条件:|Y|<=|X|3.双射:既是单射又是满射条件:|X|=|Y|代码实现通过map函数建立映射1.单射:map<int,int>BuildInjection(vector<int>src,vector<int>dst){map&l

  • Java 面试之 IO 流及Socket

    Java 面试之 IO 流及SocketJava 面试之 IO 流及Socket

  • ThinkPHP函数详解:F方法

    ThinkPHP函数详解:F方法

  • FastQC_fastech

    FastQC_fastech1、下载fastqc2、解压3、给予执行权限,否则执行的时候会显示没有权限cdFastQCchmod755fastqc4、加入到PATHexportPATH=/home/h/Fas

  • VirtualBox管理工具Vboxmanage「建议收藏」

    VirtualBox管理工具Vboxmanage「建议收藏」注意事项: 使用vboxmanage管理virtualBox虚拟机,必须使用命令行开启虚拟机。  1、如果没有使用virtualbox管理界面创建好虚拟机的话,需要使用命令行或管理界面将虚拟机创建完成(创建步骤就不啰嗦了)  2、将创建好的虚拟机注册到管理工具里。      VBoxManageregistervm &lt;filename&gt;.vbox  …

  • deviceiocontrol true怎么解决_分段函数

    deviceiocontrol true怎么解决_分段函数前言:最近需要对Windows中的设备进行编程操作,其中涉及到非常重要的函数DeviceIoControl,在使用的时候也比较的复杂,国内这一块中文资料比较少,在学习之余顺便将其翻译出来,以供参考,如有错误,欢迎指正。lcb0281@163点comMSDN原文地址:DeviceIoControlfunctionDeviceIoControl将控制代码直接发送到指定的设备驱动…

    2022年10月22日

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号