HTA&批处理混合,从BATCH部分传递变量 [英] HTA & Batch Hybrid, passing variables from BATCH section

查看:90
本文介绍了HTA&批处理混合,从BATCH部分传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个批处理+ hta混合脚本,该脚本允许我将脚本的批处理部分中的变量传递给hta部分,以便可以生成诸如计算机型号之类的东西.

I'm attempting to write a batch + hta hybrid script that will allow me to pass variables from the batch section of the script to the hta section, so that way I can generate things like the computers model number etc.

这是我到目前为止所拥有的-批处理:

This is what I have so far - Batch:

<!-- :: Batch section
    @echo off
    Pushd "%~dp0"
    setlocal

    FOR /F "tokens=2 delims='='" %%A in ('wmic ComputerSystem Get Model /value') do SET model=%%A

    for /F "delims=" %%a in ('mshta.exe "%~F0" "%model%"') do set "HTAreply=%%a"
    echo End of HTA window, reply: "%HTAreply%"
    goto :EOF
    -->

如您所见,我尝试使用%model%作为参数,并且尝试在我的VBScript部分中使用arg1尝试使用该变量-但它不起作用.

As you can see I attempted to use %model% as a parameter, and I tried to use arg1 in my VBScript section to try to use that variable - but it did not work.

因此在我的hta部分中,这是我的vbscript:

So in my hta section, this is my vbscript:

<script language="VBScript">

    MsgBox arg1

</script>

这只是打开一个空框.

我已经在网上搜索了一段时间,试图找到一种方法来做,但我无法解决.我之前解决此问题的方法基本上是创建一个批处理脚本,该脚本创建一个新文件,即hta&批处理混合,但为简单起见,我想避免这样做.

I've been searching for a while online trying to figure out a way to do this and I cannot figure it out. The way I got around this before was basically creating a batch script that creates a new file which is the hta & batch hybrid, but I want to avoid doing that for simplicity.

任何帮助将不胜感激

推荐答案

您可以使用Wscript.Shell COM对象的Environment对象在HTA运行时中访问环境变量.您可以使用Scripting.FileSystemObjectGetStandardStream方法通过stdout将数据从HTA传递回批处理线程.这是两者的演示:

You can access environment variables in the HTA runtime by using the Wscript.Shell COM object's Environment object. You can pass data back from HTA to the Batch thread over stdout by using Scripting.FileSystemObject's GetStandardStream method. Here's a demonstration of both:

<!-- :: Batch section
@echo off & setlocal
Pushd "%~dp0"

FOR /F "tokens=2 delims==" %%A in ('wmic ComputerSystem Get Model /value') do SET model=%%A

for /F "delims=" %%a in ('mshta.exe "%~f0"') do set "HTAreply=%%a"
echo End of HTA window, reply: "%HTAreply%"
goto :EOF
-->
<script language="VBScript">

    Set Env = CreateObject("Wscript.Shell").Environment("Process")
    Set StdOut = CreateObject("Scripting.FileSystemObject").GetStandardStream(1)

    MsgBox Env("model")
    StdOut.Write("response")

    Set Env = Nothing
    Set StdOut = Nothing

    close()

</script>

对于它的价值,还可以通过混入假装的.wsf文件扩展名,使用cscript以混合格式访问VBScript.除了消除了HTA窗口出现和消失的短暂闪烁之外,优点是您可以直接传递脚本参数,而不必访问Environment("Process")范围.

For what it's worth, you can also access VBScript in a hybrid format using cscript by kludging a pretend .wsf file extension. The advantage, besides getting rid of the brief flicker of an HTA window appearing and disappearing, is that you can pass script arguments directly without having to access the Environment("Process") scope.

<!-- : batch portion
@echo off & setlocal

FOR /F "tokens=2 delims==" %%A in ('wmic ComputerSystem Get Model /value') do SET model=%%A

for /F "delims=" %%a in ('cscript /nologo "%~f0?.wsf" "%model%"') do set "VBreply=%%a"
echo End of VB script, reply: "%VBreply%"

goto :EOF

: VBScript -->
<job>
    <script language="VBScript">
        model = WScript.Arguments(0)

        MsgBox model
        Wscript.Echo "response"
    </script>
</job>

混合批处理+ JScript 更加容易.通过.wsf方法,也可以同时将VBScript和JScript代码作为多个作业.

And hybrid Batch + JScript is even easier. It's also possible to have both VBScript and JScript code as multiple jobs with the .wsf method.

这篇关于HTA&amp;批处理混合,从BATCH部分传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆