[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Running a command in a hta

Posted on 2009-04-16
7
Medium Priority
?
2,581 Views
Last Modified: 2012-05-06
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
Comment
Question by:Simon336697
  • 4
  • 3
7 Comments
 
LVL 7

Expert Comment

by:sj_hicks
ID: 24163589
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
 
LVL 1

Author Comment

by:Simon336697
ID: 24163683
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
 
LVL 7

Accepted Solution

by:
sj_hicks earned 2000 total points
ID: 24163875
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Simon336697
ID: 24164149
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
 
LVL 7

Expert Comment

by:sj_hicks
ID: 24164347
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
 
LVL 1

Author Comment

by:Simon336697
ID: 24164640
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
 
LVL 7

Expert Comment

by:sj_hicks
ID: 24164822
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

830 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