Solved

Return to calling VBS from HTA

Posted on 2008-10-16
18
4,975 Views
Last Modified: 2013-12-20
Well....it looks like it's my turn to ask a question - WooHoo! :o)

I am developing a method for our imaging that PXE boots to WinPE, launches a VBScript to run wpeinit, map a drive and call up an HTA where you can select an image to apply to the workstation.  All this works fine, the HTA runs, my choices are captured via radio buttons and checkboxes and Ghost32 is run with the proper commandline switches.  

The issue I'm having is when the Ghost client finishes, it closes then the HTA closes - which *should* return control to the originating VBScript to execute the next line in the script.  However....the calling script throws an error:

Start.vbs (37, 1) (null): The operation was canceled by the user.

Now, I've started the HTA using several methods - objShell.Run "x:\windows\system32\mshta.exe s:\scripts\menu.hta",3,True - seems at least wait for something to happen with the HTA (either a Self.Close, Window.Close or killing the mshta process) before continuing, but the error above happens no matter what method I use to exit the HTA.

I have other things in the calling script that execute, wait and then continue after completion just fine.  It's only this return from the HTA that has greatly reduced the existence of hair on my head.

Is there any method at all I can leverage short of including the VBScript I need to launch after the HTA closes within the HTA itself?  I don't like that last idea given how I have to maintain things.

Code gurus, I'm all ears ('cause I have no more hair)!! LOL.



0
Comment
Question by:Netman66
  • 10
  • 8
18 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 22735500
Hi netman,

Is this line 37?
objShell.Run "x:\windows\system32\mshta.exe s:\scripts\menu.hta",3,True

it seems a strange error to have from that line.....

You can shorten that to just
objShell.Run "mshta.exe s:\scripts\menu.hta",3,True

because mshta.exe is a system file.

Also, if your HTA path happens to have spaces, use
objShell.Run "mshta.exe ""s:\scripts\menu.hta""",3,True

Regards,

Rob.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22735621
Or......perhaps that line 37 is in your HTA, and the HTA is trying to close while it is still executing some code?
0
 
LVL 51

Author Comment

by:Netman66
ID: 22736303
Hi Rob,  thanks for flying Netman!

Line 37 is from the calling script and is the next line after:

objShell.Run "x:\windows\system32\mshta.exe s:\scripts\menu.hta",3,True

objShell.Run "cscript.exe s:\scripts\ai-ph1.vbs",2,True  <<= this is the line it's complaining about.

I'll have to post the code tomorrow - it's at the office, but there is something in the way the HTA or mshta.exe process ends that kills the calling script (start.vbs).  When an external process is spawned from within a VBScript, does it become a child process or a process of it's own?  I'm wondering if it runs under the PID of the cscript process and when I kill mshta it kills the whole PID.  Does that make any sense?





0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22736422
Hmmm, I can't see why that would fail.....

To answer your PID question.....I used Process Explorer to look at the tree when I ran the code segments below....

The first script will spawn separate processes for any external programs run, which do not seem to be dependant on the parent process running.  If I kill the highest process in the tree, the HTA stays running.

Anyway, I can run the below three code files without error....

So I'm not sure just yet why you'd have an issue......

Regards,

Rob.
' ==================================

' ==================================

'          START OF TEST1.VBS

' ==================================

If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then

    strPath = Wscript.ScriptFullName

    strCommand = "%comspec% /c cscript  """ & strPath & """"

    Set objShell = CreateObject("Wscript.Shell")

    objShell.Run(strCommand), 1, True

    Wscript.Quit

End If
 

Set objShell = CreateObject("wscript.shell")

WScript.Echo "Running testhta1.hta"

objShell.Run "mshta.exe ""c:\temp\temp\test script\testhta1.hta""",3,True

WScript.Echo "Running test2.vbs"

objShell.Run "cscript.exe ""c:\temp\temp\test script\test2.vbs""",2,True

MsgBox "Finished"

' ==================================

'          END OF TEST1.VBS

' ==================================

' ==================================
 
 
 

' ==================================

' ==================================

'          START OF TESTHTA1.HTA

' ==================================

  ' Code irrelevant....any HTA with

  ' an exit button that uses Window.Close

  ' will do...

' ==================================

'          END OF TESTHTA1.HTA

' ==================================

' ==================================
 
 
 

' ==================================

' ==================================

'          START OF TEST2.VBS

' ==================================

WScript.Echo WScript.ScriptName & " is now running..."

MsgBox "Press OK to terminate this script."

' ==================================

'          END OF TEST2.VBS

' ==================================

' ==================================

Open in new window

0
 
LVL 51

Author Comment

by:Netman66
ID: 22736715
Interesting....

Let me fill in a few holes in my original post - perhaps there is a difference that is relevant to you.

I startup a new workstation and hit F12 to PXE boot.
It boots up from the network into WinPE - all the modules are loaded in PE (WMI, Scripting, XML and HTA).
Startnet.cmd loads, by default, as the shell - so I'm using it.
Starnet runs one line - cscript x:\Windows\start.vbs (which is embedded into the PE wim).
Start.vbs launches WPEINIT.exe (waits), maps a network drive (waits), runs the HTA from the mapped drive (waits).
The HTA parses the mapped drive (in a certain folder) and dynamically creates buttons based on the top level folders in this location.
When a button is pressed, it parses the contents of that folder and dynamically creates radio buttons for each .gho image it finds.
When one of those radio buttons is selected a new button is created to start the imaging process using Ghost (embedded in the wim) and builds the commandline from the radio choices you make in the HTA.
Ghost launches and attaches to the Ghostcast server session name that is built above.
The image is brought down to the machine and Ghost exits.
The HTA exits.
The next line of code in the Start.vbs is supposed to execute, but the error in the original post is presented in the CMD window that was originally launched by Startnet.cmd.

So, the process is a bit weird but goes something like this:

Startnet.cmd > run Start.vbs > Run Wpeinit.exe (wait) > Map Drive > Run HTA (wait) >> HTA runs Ghost32.exe (which itself opens a CMD window to run itself - kinda different...) > Ghost32 exits > CMD window that launched Ghost exits > HTA exits > apparently the Start.vbs exits due to an error > error is presented in CMD shell started by Startnet.cmd.

I wonder if running the HTA from the network is influencing the problem?  Perhaps WinPE Scripting isn't up to the task of running it on a real OS?  Dunno....

As you can tell, I don't write Apps for a living.  I'll script for Administration, but do not normally get invloved in anything that resembles an App.  This is my first shot at creating an HTA based on one I found on the Internet and modifying it to do what I need it to.  For the most part, each component of this process works fine it's only when I attempt to run it all together sequentially that I'm running into unforseen problems.

Thanks for your time so far Rob.



0
 
LVL 51

Author Comment

by:Netman66
ID: 22736733
Sorry, type here:  Perhaps WinPE Scripting isn't up to the task of running it on a real OS?

Should have read: Perhaps WinPE Scripting isn't up to the task of running it like a real OS is?
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 22736825
Hmmmm.....OK, so first off I'm wondering if CScript.exe actually executes properly in WinPE.....

Try my files out, using this as your HTA.

See if that gets to the cscript.exe OK....

If not, then you may have to use WScript.exe (or whatever happens to run a VBS by default, maybe a different implementation on WinPE).

Regards,

Rob.
<Html>

<Head>

<Title>Test HTA</Title>

 

<HTA:Application

Caption = Yes

Border = Thick

ShowInTaskBar = No

MaximizeButton = Yes

MinimizeButton = Yes>

 

<script Language = VBScript>

	Sub Exit_HTA

		Window.Close

	End Sub

</script>

<body>

	<button name="btn_exit" id="btn_exit" accessKey="x" onclick="vbs:Exit_HTA">E<u>x</u>it</button>

</body>

</head>

</html>

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22736830
In short, though, it seems like that sequence would work in theory, but let's just narrow it down a bit....
0
 
LVL 51

Author Comment

by:Netman66
ID: 22736872
OK, I'm game.  I'll test this out tomorrow when I have a PE environment to work from.

If you send me an email at my alias here at gmail then I'll send you the scripts I'm using tomorrow - I don't want some of the info in them public (and scrubbing them would defeat the purpose if you need them to work).

I'll also report back with the results of this test.

Again, thanks Rob!



0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 65

Expert Comment

by:RobSampson
ID: 22736944
OK.  I don't have a WinPE environment that I could test in, so let's try to work without sending whole scripts....just relevant parts around problem areas are fine.

In the test code I gave above, we're pretty much emulating what you've got, just shortened it a bit, so hopefully we can get some results out of it....

Rob.
0
 
LVL 51

Author Comment

by:Netman66
ID: 22736978
It's close - the HTA calls Ghost from itself, but the principal should tell us if the HTA exit still kills the originating script.

I'll be all over this first thing in the morning.  I'm in AST so I don't know what time difference we have.

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22737084
It's currently 11:45am here....Victoria, Australia.

I may not be available tomorrow, as that will be Saturday for me.......I'll see what I can do....
0
 
LVL 51

Author Comment

by:Netman66
ID: 22737095
Ahh!  I had no idea!

I have a friend, a peer MVP in Sydney - James Kindon.

Glad to have you assisting.

We'll take it a day at a time - no worries.

0
 
LVL 51

Author Comment

by:Netman66
ID: 22739588
OMG........

You're not going to believe the ridiculously noob error I made in my script....

I tested my solution with your simple HTA - same results as I was getting previously.

Here is the line that was giving me the original error (objShell defined earlier in the script).

objShell.Run "S:\Scripts\AI-Ph1.vbs",2,True

After looking at this issue for hours, a good night sleep is all I needed to spot the problem as soon as your HTA did the same thing.

The line should have been:

objShell.Run "cscript S:\Scripts\AI-Ph1.vbs",2,True

This is a perfect example of why you should take a break after long hours of scripting.  You look at the same error over and over but it never strikes you as being incorrect.

Rob - I have to thank you for your patience and making me THINK.  I hope I can get your assistance for future problems in VBScripting.

Now to start looking at regrowing my hair!

Cheers,
NM


0
 
LVL 51

Author Comment

by:Netman66
ID: 22739607
The kicker here too is that I typed that problem line perfectly correct when I was posting it here in my first reply to you.  Unreal....

0
 
LVL 51

Author Closing Comment

by:Netman66
ID: 31506829
Rob - without a doubt, you are a first class person.  Thanks for being professional and not making me feel as dumb as I appear to be!! LOL..
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22746038
>> The kicker here too is that I typed that problem line perfectly correct when I was posting it here in my first reply to you.  Unreal....

LOL!  Yeah, I noticed that!  Ah well, at least you've found the issue!  Well done!

Yesterday I was writing a HTA to install a program and update some shortcuts in a folder.  It was throwing an error every time I ran, even though I knew the shortcuts were there.  I had this

For Each objFile In objFSO.GetFolder(strFolder)

which threw the error, and it me ages to remember that I needed to add .Files to the end!

It's those "not quite eyntax" errors, but just omissions that make the most frustrating errors.

Anyway, sure, throw me a line if you need VBS / HTA help.....

Thanks for the grade, and the comments....

Regards,

Rob.
0
 
LVL 51

Author Comment

by:Netman66
ID: 22749142
Hi Rob,

Yeah, those "almost" correct lines that don't really throw any useful error messages take ages to figure out.

You're welcome!

Hope to do business with you again - you could teach me a thing or two!

Cheers,
Paul
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now