Page 1 of 1

Help with an HTA/DSQUERY

Posted: Thu May 21, 2009 3:13 am
by mattw
Hi all,

I'm trying to run a DSGet inside an HTA using a value piped from a DSQuery, and I keep getting an error that the pipe is an invalid character when I try to open the HTA. Here's the code that I'm working with. I'm sure that my issue is one of format, but I've tried adjusting " marks all around with no success. Could someone help me find my error? Thanks!

<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 object: set objShell = CreateObject("") 'Now create an exec object: set objWshScriptExec = objShell.Exec("dsquery user -name " & txtUsername.value & "*" | dsget user -memberof -expand) '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 = strhtmlDataArea.InnerHTML = strhtml End Sub </script> <body> <h1>DSQuery</h1> <p>Please enter in a username you wish to list the group memberships 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>
mattw2009-05-21 10:14:13

Help with an HTA/DSQUERY

Posted: Thu May 21, 2009 3:54 am
by jvierra
The pipe has to be inside of the quoted string command. All elements of an Exec run command have to be in one string.

Help with an HTA/DSQUERY

Posted: Thu May 21, 2009 5:04 am
by jvierra
You are not enclosing teh whole string inside your quotes.
This is what you are doing in string segments:

"dsquery user -name
No end quote "dsquery user -name "
& "txtUsername.value "
Should not be in quotes & txtUsername.value &
No & an no quotes & "|" &
dsget user -memberof -expand"
No beginning quote. "dsget user -memberof -expand"

sCmd = "dsquery user -name "
sCMd = sCmd & txtUsername.value
sCmd = sCmd & " | dsget user -memberof -expand"
MsgBox sCmd

This way you don't need to think about the quotes.

sCmd = "dsquery user -name" & txtUsername.value & " | dsget user -memberof -expand"
MsgBox sCmd

This is a string:

sCmd = "dsquery user -name "
This is a string plus variable:
sCmd = sCmd & txtUsername.value
Now add last string:
sCmd = sCmd & " | dsget user -memberof -expand"


sCmd = "dsquery user -name" & txtUsername.value & " | dsget user -memberof -expand"

Now does it make more sense?jvierra2009-05-21 12:12:27

Help with an HTA/DSQUERY

Posted: Fri May 22, 2009 3:01 am
by jvierra
Diiferent problem but yo still haven't done it the way I showed you. There is a string violation - too many quotes or not enough depending on what you are trying to do.

You must get the script working as a VBS first then move it into an HTA.

Try starting here by saving the followiong to a VBS file and testing it until it works correctly.

Code: Select all

MsgBox GetData( "user1" )
Function GetData( username )

     set objShell = CreateObject("")
     set proc= objShell.Exec("dsquery user -name " & username. & "* | dsget user -memberof -expand") 
     While proc.Status  0 
          WScript.Sleep 100
     GetData = proc.StdOut.ReadAll()

End Function

Please test the format and contents of the dsquery comman at a prompt to be sure it works as you are expecting. The syntax looks wrong to me.
jvierra2009-05-22 10:21:31

Help with an HTA/DSQUERY

Posted: Fri May 22, 2009 3:55 am
by jvierra
Here is the same code as an HTA

Code: Select all

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">  
<title>My HTML Application</title>  
<script language="vbscript">  
    Sub loaddiv()  
        result = RunCmdGetOutput( "cmd.exe /c dir c:" )  
        MsgBox result  
        datarea.innerText = result  
    End Sub  
    Function RunCmdGetOutput( cmd )  
         set objShell = CreateObject("")  
         set proc= objShell.Exec("cmd.exe /c dir c:")   
         While not proc.StdOut.AtEndOfStream  
          RunCmdGetOutput = proc.StdOut.ReadAll()  
    End Function
    <input type="button" value="clickme" onclick="loaddiv"/><br /><br />  
    <div id="datarea" style="background-color: buttonface; color: maroon; font-weight: bolder;"></div>  
jvierra2009-05-22 10:56:39

Help with an HTA/DSQUERY

Posted: Fri May 22, 2009 4:32 am
by mattw
Thanks for the information. I've been able to get the dsget and dsquery to pipe with a DN in DOS, but can't seem to get them to work together in vbscript. I've used the ADSI scripts before, but had really liked the slimmer code possibilities. Guess we can't have it all. :)

Thanks again, for your help.

Help with an HTA/DSQUERY

Posted: Fri May 22, 2009 5:12 am
by jvierra
YOu majo issue was teh strings. After testing that I saw that - of course - it's a unicode application.

There is a way to get around this but I don't know it off the top of my head. Ypu could pipe to a file and load teh file.

Help with an HTA/DSQUERY

Posted: Tue May 26, 2009 6:55 am
by mattw
Well, while messing around with pulling AD info into the HTA I tried the following:

Sub btnRun_Click
sDomain = objNetwork.UserDomainsUser = InputBox.valueSet objNetwork = CreateObject("WScript.Network")set objUser = GetObject("WinNT://" & sDomain & "/" & sUser)
for each oGroup in objUser.Groups sGroups = sGroups & oGroup.Name & vbcrlfNextDataArea.InnerHTML = sGroupsEnd Sub </SCRIPT> <body> <input type="text" name="InputBox" size=40 value="Enter User Name"><p> <input type="button" value="Run" name="btnRun" onClick="btnRun_Click"> <input id=runbutton type="button" value="clear" onClick="clear"> <p><Span ID = "DataArea"></Span></body></html>

But I keep getting an Object Required error on objNetwork when I click on Run. I understand that some WScript objects don't work in HTA because it's not running WScript or CScript, but I thought that WScript.Network was an exception if you used the CreateObject method?

Help with an HTA/DSQUERY

Posted: Tue May 26, 2009 10:54 pm
by mattw
This code works in VBS:
dim objNetworkdim sDomain, sGroup, sUserdim objGroup, objUserdim bMember
sUser = InputBox("Enter logon ID")Set objNetwork = CreateObject("WScript.Network")sDomain = objNetwork.UserDomain' MsgBox "sDomain: " & sDomain' sUser = objNetwork.UserName' MsgBox "sUser: " & sUser
set objUser = GetObject("WinNT://" & sDomain & "/" & sUser)
for each oGroup in objUser.Groups sGroups = sGroups & oGroup.Name & vbcrlfNext
msgbox sGroups

So testing that code by transplanting into the HTA didn't really help me much with the error that I'm getting.