Go Back

dsquery/dsget

I'm trying to shell/spawn out to get username email addresses from the active directory on my dev system, upto date W10 running 10.3.3.g.
if I run up a CMD from the desktop and type in
dsquery user -samid "chrisa" | dsget user -email > c:\fleetfactors\data\tmp\chrisa.log
it works and I get the file with my email address in it.
However if I spawn or shell the same from within a program it fails with
'dsquery' is not recognized as an internal or external command
I've checked the paths are similar and even spawned a CMD and replaced the path with the path from the desktop CMD. It Failed.
Also if I start a CMD from within VS (latest) it fails with the same error.
Anyone any idea what might be wrong?

Desktop CMD Path:
c:\fop-2.3\fop
C:\Program Files (x86)\Common Files\Oracle\Java\javapath
C:\Program Files (x86)\IDM Computer Solutions\UltraCompare\
C:\Program Files (x86)\IDM Computer Solutions\UltraEdit\
C:\Program Files (x86)\ZipGenius 6\
C:\Program Files\dotnet\
C:\Program Files\PuTTY\
C:\qpdf-7.1.1\bin
c:\tools\
C:\Users\chrisa\AppData\Local\Microsoft\WindowsApps
C:\Users\chrisa\AppData\Local\Programs\Microsoft VS Code\bin
C:\WINDOWS
C:\WINDOWS\system32
C:\WINDOWS\System32\OpenSSH\
C:\WINDOWS\System32\Wbem
C:\WINDOWS\System32\WindowsPowerShell\v1.0\

Using Synergy path:
c:\fop-2.3\fop
C:\Program Files (x86)\Common Files\Oracle\Java\javapath
C:\Program Files (x86)\IDM Computer Solutions\UltraCompare\
C:\Program Files (x86)\IDM Computer Solutions\UltraEdit\
C:\Program Files (x86)\Synergex\SynergyDE\
C:\Program Files (x86)\Synergex\SynergyDE\bin
C:\PROGRAM FILES (X86)\SYNERGEX\SYNERGYDE\connect\
C:\Program Files (x86)\ZipGenius 6\
C:\Program Files\dotnet\
C:\Program Files\PuTTY\
C:\Program Files\Synergex\SynergyDE\
C:\Program Files\Synergex\SynergyDE\bin
C:\qpdf-7.1.1\bin
c:\tools\
C:\Users\chrisa\AppData\Local\Microsoft\WindowsApps
C:\Users\chrisa\AppData\Local\Programs\Microsoft VS Code\bin
C:\WINDOWS
C:\WINDOWS\system32
C:\WINDOWS\System32\OpenSSH\
C:\WINDOWS\System32\Wbem
C:\WINDOWS\System32\WindowsPowerShell\v1.0\


 

6 Answers
0   | Posted by Chris Aiken to Synergy DBL on 5/5/2020, 11:19 AM
Steve Ives
A cursory google revealed this:

"Dsquery is a command-line tool that is built into Windows Server 2008. It is available if you have the Active Directory Domain Services (AD DS) server role installed. To use dsquery, you must run the dsquery command from an elevated command prompt."

Which brings a couple of questions to mind:

Where is it actually located on the system? You cloud try "WHERE DSQUERY" in a shell where it works to answer that question.

Presumably, when you are shelling out from DBR you are not creating an "elevated" process, which seems to be a basic requirement for using the utility. Not sure if that's even possible actually. Anyone else know the answer to that?
 

5/5/2020, 10:15 PM   0  
Chris Aiken
I had the info on DSQUERY but wondered why would I get less permissions spawning the command or from a command prompt from inside VS, than from just running up a command prompt (not run as administrator) from the desktop all on my own Pc.
Where DSQUERY returned c:\windows\system32
 

5/6/2020, 7:57 AM   0  
Chris Aiken
Does anyone have an alternative for getting email addresses from active directory other than DSQUERY ?

5/7/2020, 3:52 PM   0  
Geoff MacCue
Have you tried dropping into a shell command from your synergy program and running the DSQUERY from there
XCALL SHELL (,'CMD')

Have you tried using the "CMD /C DSQUERY  …" within you synergy shell code
XCALL SHELL (,'CMD /C dsquery user -samid "chrisa" | dsget user -email > c:\fleetfactors\data\tmp\chrisa.log')
You may need to put the full path name for dsquery and dsget (or add their path to the PATH logical)

Have you tried using the PIPE | in an open
OPEN (CHN=%SYN_FREECHN,I,'|CMD /C dsquery user -samid "chrisa" | dsget user -email')
You may need to put the full path name for dsquery and dsget (or add their path to PATH)

The command "WHERE DSQUERY" should display the full path name of the application, run this from where you can successfully execute you dsquery command. similarly for DSGET

I found both DSQUERY and DSGET using dir /s dsquery.exe from C:\
To work in your application you will need to include these paths in the PATH env variable.


I have worked out how to enable permissions within SYNERGY Code (as well as for batch files) using a single line of code.
I won't publish it here, but you can have a batch file to which you pass parameters (username etc) and it will return to your synergy program the output
If you want the "enable Permissions" code let me know, no guarantees it will work everywhere but I have tried on three different systems and versions of Windows. I was able to launch REGEDIT from synergy DBR.

eg xcall shell (,'batch.bat chrisa filename)
and it will put the resulting data in filename

I wrap it up in an xcall to keep it simple
    xcall open_command (channel,command,r_error)
    while (r_error == 0) begin
         reads (channel,data,eof)        ; read output from command
         do something with the data
    end
eof,
      continue
-------------------------------------------------------

Samples using other methods
record
     chn ,d4
proc
     open (15,o,'tt:')
     xcall shell (,'cmd')                 ; drop out to command prompt, see if you can run DSQUERY
     stop
----------------------------------------------------------
record
     data ,a500
     chn ,d4
proc
     open (15,o,'tt:')
     open (chn=%syn_freechn,I,'|cmd /c dir')                  ; get dir information
loop,
     reads (chn,data,eof)
     writes (15,%atrim(data))
     goto loop
eof,
     stop
-----------------------------------------------------------
record
     data ,a500
     chn ,d4
proc
     open (15,o,'tt:')
     xcall shell (,'cmd /c dir > dir.lst')                ; get dir information into a work file and read it
     open (chn=%syn_freechn,i,'dir.lst')
loop,
     reads (chn,data,eof)
     writes (15,%atrim(data))
     goto loop
eof,
     stop









 

5/8/2020, 6:42 AM   0  
Steve Ives
I was just discussing this with some colleagues and we had another thought on how you might approach this.

Synergy includes two runtimes called DBRPRIV.EXE and DBSPRIV.EXE. For details see Running applications that require elevated privileges.

Now to be clear, I have not tested this, but you might want to try it out. How about you write a synergy program that spawns the DSQUERY command that you need to run, grabs the necessary data and displays it to STDOUT (TT:). Then use an Open Pipe command in your main app to run the new program with the DBSPRIV.EXE runtime (e.g. "|DBSPRIV.EXE DSQUERYHOST.DBR") and read the data output by the utility.

If this does work, and if UAC is enabled, you're user is presumably going to see a UAC authorization dialog, but short of disabling UAC I don't think there is anything you can do about that.

Anyway, like I said, just an unproven thought, but hope it helps.

5/12/2020, 10:11 PM   0  
Chris Aiken
Thanks for all the suggestions.
I couldn't get anything to work nicely.
We already have a user file which contains their specific software options.
I'm going to add the email address to it and populate it from DSQUERY's for existing users.
We've changed very few email addresses over the years. Usually for a surname change. So maintaining them should not be a problem
Cheers
Chris

5/21/2020, 2:37 PM   0  
Please log in to comment or answer this question.