Running a batch file from winbatch - UAC issues?

Started by jm0000, April 16, 2019, 08:40:23 AM

Previous topic - Next topic

jm0000

Hi,

   I have the following batch file:

   :start
set /p name=Please enter the name:
dsquery user -name "%name%*" |dsget user  -email -display -office -title

I am quering the AD for user info. The batch file runs without any issues, but when I try to run it from winbatch it will not work.  It fails to find the dsquery file.  I tried the following:

run("cmd.exe","/c C:\showadinfo.bat")
and

shellexecute( Environment("COMSPEC"), '/k C:\showadinfo.bat',' ', @normal, 'runas')
and

cmd=Environment("COMSPEC")
Run(cmd,"/k C:\showadinfo.bat")
and

Run(Environment("COMSPEC"), '/c  C:\showadinfo.bat' )
and

objShell = ObjectCreate('wscript.shell')
objShell.Run('cmd.exe \k C:\showadinfo.bat ')

The batch file will start running but then report that it can't find dsquery.  I even tried copying dsquery to the folder in which the batch file runs.

I even tried every option in the UAC settings [No UAC info, askinvoker, highest available, requireAdministrator] during compiling.

What am I doing wrong. My gut feeling is that UAC is unable to run these commands as an administrator privileges.

How can I run this batch file from winbatch?


td

No suggestions but a few observations.  You didn't mention a version of Windows but generally, dwquery.exe is available as in both 32-bit and 64-bit versions so file redirection is not likely the problem.  I have no problems executing cmd.exe with your batch file as a parameter from a WinBatch script as long as the WinBatch script is running as an elevated admin using the "Run" function.  In other words, Dsquery executes in the cmd.exe window as expected.

Microsoft does document someplace that dsquery must be executed from an elevated admin command prompt.

Also note that Dsquery.exe requires Dsquery.dll before it can execute.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

jm0000

I'm running win10. I tried a 32bit and 64 bit version.  If I run the same batch file manually, it works great. If I run it through winbatch it will not run. I complied it with every available option.

I did change the line to

shellexecute( Environment("COMSPEC"), '/k C:\showadinfo.bat',' ', @normal, 'RUN')

I'll keep testing, but I would greatly appreciate any help.

Thank You

td

On Windows 10 "dsquery.exe" is only available as a 64-bit executable.  So you definitely need to start 64-bit cmd.exe from WinBatch by turning off file redirection or using "sysnative", or use 64-bit WinBatch when attempting to run the  "dsquery.exe" executable via "cmd.exe" on Windows 10 using WinBatch.   

That said, I have no problems running dsquery.exe from a WinBatch script on Windows 10 so I can't provide any additional assistance. 
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

Neglected to mention that it is easier to change UAC settings for testing by simply changing the file extension of your WinBatch script as opposed to recompiling with different UAC setting in the compiler.  Some UAC settings may require a code signing certificate depending on your systems settings but you don't have to worry about that when simply changing the script's file extension.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

jmburton2001

Quote from: jm0000 on April 16, 2019, 12:15:01 PM
I'm running win10. I tried a 32bit and 64 bit version.  If I run the same batch file manually, it works great. If I run it through winbatch it will not run.

I had an issue where I could run a command manually but couldn't get it to run in Winbatch. I know my issue isn't exactly the same as yours but the assistance Tony (td) provided in mine might provide some some insight into the differences between running from a command prompt and running the same commands from Winbatch??

http://forum.winbatch.com/index.php?topic=2047.msg10515#msg10515

Just my two cents...

jm0000

Thanks all. Adding oldvalue = IntControl( 92, "disable", 0, 0, 0 ) did the trick.  I'm going to experiment with the other suggestion as well.  If case your interested here is the code.  This just a phone book with special feature. This opens as CSV file [I'm using excel]. From there the user can message, skype,  and email the user. If your an admin, you cal also use dsquery to query the AD for user info. I keep AD up to date, so AD  has the latest information. I'm still writing the code, but this is a working very rough draft:



AddExtender("WWWNT34I.DLL")

;in case you want the use to choose the file
;filename = AskFilename("CSV FIle", DirScript(), "CSV Files|*.csv", "", 1 )

:menu
filename =  "C:\phonelist.csv"
names_array = ArrayFileGetCSV( filename, 0)

MyDialogFormat=`WWWDLGED,6.2`

MyDialogCaption=`My phone book`
MyDialogX=002
MyDialogY=059
MyDialogWidth=510
MyDialogHeight=118
MyDialogNumControls=007
MyDialogProcedure=`MyDialogCallbackProc`
MyDialogFont=`DEFAULT`
MyDialogTextColor=`DEFAULT`
MyDialogBackground=`DEFAULT,154|203|252`
MyDialogConfig=0

MyDialog001=`461,101,026,012,PUSHBUTTON,"PushButton_Close",DEFAULT,"Close",1,10,@csFlat|@csDefButton,DEFAULT,DEFAULT,"255|255|255"`
MyDialog002=`223,101,062,012,PUSHBUTTON,"PushButton_editspreadsheet",DEFAULT,"Edit Excell Spreadsheet",2,30,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog003=`145,101,036,012,PUSHBUTTON,"PushButton_email",DEFAULT,"Email User",3,40,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog004=`011,101,036,012,PUSHBUTTON,"PushButton_skype",DEFAULT,"Skype User",4,50,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog005=`007,001,494,094,REPORTVIEW,"ReportView_1",names_array,DEFAULT,DEFAULT,20,@csAsort|@csFirstHeader|@csFullSel|@csGrid|@csColEdit|@csSingleSel,"Microsoft Sans Serif|9728|40|34",DEFAULT,DEFAULT`
MyDialog006=`325,101,082,012,PUSHBUTTON,"PushButton_ADinfo",DEFAULT,"Use AD info - Supervisors only",5,60,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog007=`071,101,036,012,PUSHBUTTON,"PushButton_textuser",DEFAULT,"Text User",6,70,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

ButtonPushed=Dialog("MyDialog")




If Buttonpushed == 1 then exit

;;;;;;Routine to edit the spreadsheet

If Buttonpushed == 2 then
objShell = ObjectCreate('wscript.shell')
objShell.Run('excel C:\phonelist.csv ')
;run("excel","C:\phonelist.csv")
     goto menu


;;;;;;Routine to email the user
  If Buttonpushed == 3 then
; Get Selected Items
; The array variable defined in the Reportview
; will contain the selected rows once the dialog returns
selected = ""
For row = 0 To ArrInfo(names_array, 1)-1
    data = ""
    For col = 0 To  ArrInfo(names_array,6)-1
       item = names_array[row,col]
       If data == '' Then data = item
       Else data = data:'|':item
    Next
    If selected == '' Then selected = data
    Else selected = selected:@TAB:data
Next

;now extract email which is the 5 item in the list
useremail=ItemExtract(5,data, "|")
;display(2,"email name",useremail)
objShell = ObjectCreate('wscript.shell')
objShell.Run('outlook /c ipm.note /m %useremail%',0,0)

;objShell.Run('sip:%useremail%',0,0)
;now email that user

goto menu


;;;;;;Routine to skype the user
   ; Get Selected Items
; The array variable defined in the Reportview
; will contain the selected rows once the dialog returns
If Buttonpushed == 4 then
selected = ""
For row = 0 To ArrInfo(names_array, 1)-1
    data = ""
    For col = 0 To  ArrInfo(names_array,6)-1
       item = names_array[row,col]
       If data == '' Then data = item
       Else data = data:'|':item
    Next
    If selected == '' Then selected = data
    Else selected = selected:@TAB:data
Next

;now get email which is the 5 item in the list
useremail=ItemExtract(5,data, "|")
;display(2,"email name",useremail)
objShell = ObjectCreate('wscript.shell')
objShell.Run('sip:%useremail% ')
goto menu


;;;Routine to use AD info. This will only work for Network Admins
If Buttonpushed == 5 then

; Disable redirect. This allows dsquery to run
oldvalue = IntControl( 92, "disable", 0, 0, 0 )

run("cmd.exe","/c C:\showadinfo.bat")
goto menu



;;; Routine to text the user
If Buttonpushed == 6 then
selected = ""
For row = 0 To ArrInfo(names_array, 1)-1
    data = ""
    For col = 0 To  ArrInfo(names_array,6)-1
       item = names_array[row,col]
       If data == '' Then data = item
       Else data = data:'|':item
    Next
    If selected == '' Then selected = data
    Else selected = selected:@TAB:data
Next

;now get email which is the 5 item in the list
;pcname=ItemExtract(5,data, "|")
;display(2,"email name",pcname)
  Run("cmd.exe","/c C:\joecallme.bat")
goto menu


td

From the Consolidated WIL Help file, "NOTE: It is important to minimize the code run when the File System Redirection is disabled, especially when loading WIL Extenders, using COM, or doing anything that asks Windows to load any new DLLs. The problem is that while File System Redirection is disabled, your code will be looking at the wrong version of windows and will attempt to load wrong (64 instead of 32 bit) versions of modules, and usually fail, GPF, or otherwise not function properly."



"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

ChuckC

Looking over this thread of discussion, there appears to be something that never got mentioned...

There is no need at all to be shelling out to CMD.EXE to execute a .BAT file that runs DSQUERY.EXE.  WinBatch is fully capable of directly performing all of the ADSI/LDAP calls via the ADIS Extender, via COM interfaces or via the .NET CLR & the .NET Framework.

A little bit of effort spent metabolizing glucose in the brain should net some positive results...

td

The .Net CLR ADSI classes are a bit of a bear to use in WinBatch and produces less than desirable results because of issues I will not go into here.  The extender is the best choice with COM Automation being a very close second.   However, it was deemed better to give the OP an opportunity to expand some basic Windows 10 scripting knowledge without also dumping a whole lot of new stuff to learn on the OP.

"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade