• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3001
  • Last Modified:

Running a command in a hta

Hi guys,

Id like to know what you guys suggest for the following.

Let's say i run the following from a command line:

dsquery user -name simonha*

In the above, the parameter is simonha*

What Id like to do is convert this into a hta so, I have the following when I run the hta:

---------------------------------------------------------------------------------- script.hta

Please enter in a username you wish to return the dn path for:

__________________________

<Run>

-------------
In the above, I would enter in the username, then click Run.

The output would be shown in the hta itself.

My question is, is that I want to run the dsquery command when I click the Run button.

If I had the below template for a hta in the code snippet, guys how would i add the command:

dsquery user -name simonha*

to it?

Any help greatly appreciated.
<html><head><title>HTA Test</title><HTA:APPLICATION      ID="objTest"      APPLICATIONNAME="HTA Test"     SCROLL="yes"     SINGLEINSTANCE="yes"></head>
<SCRIPT LANGUAGE="VBScript">
' PUT YOUR SUBROUTINES HERE
</SCRIPT>
<body>
<!-- PUT THE HTML TAGS HERE -->
</body>

Open in new window

0
Simon336697
Asked:
Simon336697
  • 4
  • 3
1 Solution
 
sj_hicksCommented:
Hi Simon, me again.  See sample below.  If you want to get a bit more fancy, you can use sh.Exec and capture the stdout stream and display it in the the span.  The WSH help file (script56.chm) or MSDN will give you that command syntax.
<html>
<head>
 
<!-- hta section below -->
 
<HTA:APPLICATION 
border="thin" 
borderStyle="normal" 
caption="yes" 
maximizeButton="yes" 
minimizeButton="yes" 
showInTaskbar="no" 
innerBorder="yes" 
navigable="yes" 
scroll="auto" 
scrollFlat="yes"
/>
 
<SCRIPT LANGUAGE="VBScript">
 
 
Sub btnRun_Click
	set sh = CreateObject("Wscript.shell")
	sh.Run "cmd /k dsquery user -name " & txtUsername.value & "*", 1, false
End Sub
 
 
</script>
 
<body>
	<h1>DSQuery</h1>
	<p>Please enter in a username you wish to return the dn path for: :</p><input type="text" id="txtUsername" name="txtUsername" size='50'>
	<p><input type="button" id="btnRun" name="btnRun" value="Run" onclick="btnRun_Click"></button>
	<span id = "DataArea"></span>
</body>
</html> 

Open in new window

0
 
Simon336697Author Commented:
Hi sj, youre a gun mate.
That works great.
sj,
This returns the output in a command window.
Is there a way to:
1) Not display the command window at all.
2) Have the output written to the hta window?
Your a champion.
0
 
sj_hicksCommented:
There is.  Use shell Exec instead of Run.  Sample shown in below link does the same thing, but with ping.  Should be easy to adapt.
http://www.microsoft.com/technet/scriptcenter/resources/qanda/aug06/hey0817.mspx
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Simon336697Author Commented:
Hi sj,
Ive given it a go.
It kind of works, but would love some help.

When I enter a username, and then click on Run, it opens up a command window, displays nothing, so I then close the command window manually. When I return to see what is in the hta window, the results are there as id like them, but im not understanding why im getting a blank dos window, and have to close it then see the results in the hta. Im pretty sure my code has some defects.
Here it is below.
<html>
<head>
 
<!-- hta section below -->
 
<HTA:APPLICATION 
border="thin" 
borderStyle="normal" 
caption="yes" 
maximizeButton="yes" 
minimizeButton="yes" 
showInTaskbar="no" 
innerBorder="yes" 
navigable="yes" 
scroll="auto" 
scrollFlat="yes"
/>
 
<SCRIPT LANGUAGE="VBScript">
 
 
Sub btnRun_Click
	'Create an instance of the Wscript.shell object:
	set objShell = CreateObject("Wscript.shell")
 
	'Now create an exec object:
	set objWshScriptExec = objShell.Exec("cmd /k dsquery user -name " & txtUsername.value & "*")
 
	'Now get to the output of running the dsquery by defining a variable to hold the StdOut property of the exec object:
	set objStdOut = objWshScriptExec.StdOut 
 
strhtml = ""
Do Until objStdOut.AtEndOfStream 
strLine = objStdOut.ReadLine 
strhtml = strhtml & strLine & "<br>"
Loop 
strhtml = strhtml
DataArea.InnerHTML = strhtml 
 
End Sub
 
 
</script>
 
<body>
	<h1>DSQuery</h1>
	<p>Please enter in a username you wish to return the dn path for: :</p><input type="text" id="txtUsername" name="txtUsername" size='50'>
	<p><input type="button" id="btnRun" name="btnRun" value="Run" onclick="btnRun_Click"></button>
	<span id = "DataArea"></span>
</body>
</html> 

Open in new window

0
 
sj_hicksCommented:
Hmm, don't think it's possible to hide the cmd window, because the app you're running is a command line app.  You can make apps run in a hidden window using sh.Run, but then you can't get the stdout to display the results in the HTA.  Probably possible to retrieve the data from AD directly from the script using ADSI, so you wouldn't need to launch the exe, but will be a bit more complex.
0
 
Simon336697Author Commented:
Hi sj,

With non-hta vbscript, is this true below?

Do Until objExec.Status
    Wscript.Sleep 250
Loop
Wscript.Echo objExec.StdOut.ReadAll()

Ref for above is:
http://www.microsoft.com/technet/scriptcenter/resources/qanda/may06/hey0519.mspx

Im just wondering if this waits until the command line tool is complete, and then returns results, then is there something comparable for htas? That is, something that would do the same as:

    Wscript.Sleep 250

Without having to use wscript, which you cant use in a hta?
0
 
sj_hicksCommented:
Hmm, maybe I was mistaken about hiding the command window - that article seems to indicate otherwise.  When you shell.exec, don't use "cmd..." just use "dsquery...".  That might hide the window.
Regarding wscript.sleep and wscript.echo, you can't use the WScript object in HTA, so they won't work.  As for alternatives for wscript.sleep - try googling it.  I've seen solution before using ping and by running a wait script.  Don't have any on had tho.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now