Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTA & Batch Hybrid, passing variables from BATCH section

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
    -->

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.

So in my hta section, this is my vbscript:

<script language="VBScript">

    MsgBox arg1

</script>

Which just opens an empty box.

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.

Any help would be much appreciated

like image 621
GrumpyCrouton Avatar asked Mar 29 '17 22:03

GrumpyCrouton


People also ask

Who is in charge of HTA?

Officially opened in September 2006, the Home Team Academy (HTA) is a department of the Ministry of Home Affairs (MHA). Chief Executive Anwar Abdullah heads the Academy with two Deputies in charge of Training & Development, and Administration.

What is Home Team concept?

The Home Team concept was launched in 1997. It centres around the idea that each of our 11 agencies has a specific role, but all share a common mission. We work as one with our community to keep Singapore safe and secure.

How long is HTA?

Trainees will be placed with a Host Employer for the duration of their training. Depending on their rate of progress, trainees can expect to be completed in 7-8 months.

Which Home Team department assumes the role of the leading corporate university for the Home Team?

As the Corporate University of the Home Team, the Home Team Academy (HTA) plays a crucial role in the Home Team training & learning ecosystem.


1 Answers

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>

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>

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.

like image 166
rojo Avatar answered Sep 25 '22 19:09

rojo