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

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • FlashFXP注册码-FlashFXP密钥

    FlashFXP注册码-FlashFXP密钥flashfxp3.41中文版注册码:(适合最新版本)推荐(尚未被封的Realkey)FLASHFXPvACq2ssbvAAAAAC1W7cJKQTzmx77zmqJICvA7d3WnUtWNXdrp8YuERRFdIvXfOPbcpABkVix2aRTgg6afcIKFPxS72XYljdE9tgQD/2r+kmfVBngGM4Qc9p7e0PcTfFF/1tt2bqlxS8r…

    2022年7月26日
    12
  • Pytest(18)pytest接口自动化完整框架思维导图[通俗易懂]

    Pytest(18)pytest接口自动化完整框架思维导图[通俗易懂]pytest接口自动化完整框架思维导图

    2022年7月28日
    7
  • bootstrap开发工具_bootstrap经典网页布局

    bootstrap开发工具_bootstrap经典网页布局在这里推荐一款基于bootstrap的在线布局工具,以后布局可以就可以和做PPT一样了http://www.bootcss.com/p/layoutit/

    2022年8月3日
    4
  • 【2021-09-07】JS逆向之空气质量历史数据查询

    【2021-09-07】JS逆向之空气质量历史数据查询文章仅供学习使用,请勿用于非法活动文章目录前言一、页面分析二、数据获取三、总结前言目标网站:aHR0cHM6Ly93d3cuYXFpc3R1ZHkuY24vaGlzdG9yeWRhdGEvZGF5ZGF0YS5waHA/Y2l0eT0lRTYlOUQlQUQlRTUlQjclOUUmbW9udGg9MjAyMTA5反爬类型:反调试,动态js,数据加密一、页面分析打开网页后,f12调用开发者工具,弹出提示框解决办法:点这玩意设置里直接打开开发者工具,或者新开一个网页,f12再进链接然

    2022年6月19日
    30
  • 关于java中堆内存与栈内存的详细分析[通俗易懂]

    本文由java零基础入门栏目为大家推荐,文中详细分析了什么栈内存与堆内存,并为大家介绍了二者的区别,希望可以帮助到大家。堆内存用于存储Java中的对象和数组,栈内存主要是用来执行程序用的。

    2022年1月17日
    38
  • jQuery下载和安装详细教程[通俗易懂]

    jQuery下载和安装详细教程[通俗易懂]下载jQuery我们可以到jQuery的官网下载jQuery文件(PS:其实jQuery就是一个封装了很多函数的js文件,把这个js文件导入到网页中就可以了)。jQuery官网地址:https://jquery.com/打开官网,即可看到jQuery的下载按钮,点击进入下载页面。有两个版本的jQuery可以下载:Productionversion-用于实际的网站中,是已经被精简和…

    2022年5月3日
    448

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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