Jump to content
Sign in to follow this  
Grantson

dsquery - works from command line but autoit returns blank string

Recommended Posts

Grantson

Good Morning Guys

Im revisiting an old little project

The code segment below uses dsquery to extract the full path to a shared printer from AD and then uses a powershell command to add it.

Running the individual commands from cmd works 

When i run the script below the dsquery line returns nothing.

i would be greatful if a set of fresh/or more experienced eyes could point me in the direction of what im missing :) 

Thanks

Grant

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <IE.au3>
#include <AD.au3>
#include <Constants.au3>
#include <array.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$printers = InputBox("","Enter Printer name")
$command = 'dsquery * domainroot -filter "(&(objectClass=PrintQueue)(printername="'&$printers&'"))" -attr uncname -q'
MsgBox(0,"",$command)
$dsqOutput = RunWait(@ComSpec & '/k' & $command, @SystemDir, $STDOUT_CHILD)
MsgBox(0,"",$dsqOutput)
Sleep (2000)
$CN = StdoutRead($dsqOutput)
MsgBox(0,"",$cn)
$CN = StringTrimLeft($cn,1)
$CN = Stringtrimright($CN,4)
$command = "([wmiclass]'Win32_Printer').AddPrinterConnection('"&$CN&"')"
Run(@ComSpec & ' /k Powershell -command ' & $command)

Share this post


Link to post
Share on other sites
spudw2k

When you run dsquery manually are you doing it from an elevated CMD prompt? If so, perhaps adding #RequireAdmin to the top of the script?

edit: not sure if it matters but the help file uses Run instead of RunWait for the example usage of StdOutRead.

Edited by spudw2k

Share this post


Link to post
Share on other sites
water

I would suggest to replace dsquery with my AD UDF and Powershell by directly using WMI by AutoIt.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
spudw2k

Share this post


Link to post
Share on other sites
Grantson

Personally, if you are just adding a printer I'd use printui and the printer share name.

 

Printui requires elevation to run, or atleast it does on our network :( . I was putting this together so that it can be run as a standard user.

 

 

When you run dsquery manually are you doing it from an elevated CMD prompt? If so, perhaps adding #RequireAdmin to the top of the script?

edit: not sure if it matters but the help file uses Run instead of RunWait for the example usage of StdOutRead.

 

the dsquery line works in a non elevated cmd window and returns the full printer path 

I tried with both run & runwait, i was using runwait to try and get the cmd window to stay up to see if there was any error messages popping up 

 

 

I would suggest to replace dsquery with my AD UDF and Powershell by directly using WMI by AutoIt.

 

Hi water I had a look at the UDF for something similar a while ago, for one line the query command seemed easier at the time.

ill have a look at wmi though. I think when i started this i tried it and it didnt like the class name as i was trying to addapt the example from PS, so i just passed the wmi bit to PS

As it stands the PS bit works though its just that the dsquery return does not return anything 

Share this post


Link to post
Share on other sites
water

Using the AD UDF is as easy as using _AD_Open and _AD_GetObjectsInOU.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Grantson

Thanks water

Im looking at the examples now and trying to reform the query 

all the "" are giving me blurred vision, 

Ill let you know how i get on 

Share this post


Link to post
Share on other sites
water

As soon as I am back in my office I am happy to answer your questions.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Grantson

Ok, bizarre 

I ran another script i wrote last year that uses dsquery that was working which now appears to have stopped working :S 

Edit, thats a red herring the dsquery works, then it fails to create the txt file with the output in the temp file when running as admin

but thats for another day 

 

Water 

I came up with this when trying to rewrite the query

_Examples("(&(objectClass=PrintQueue)(printername='p104211'))", "uncname", "Printer path")

but it returns no results and Error 3

Edited by Grantson

Share this post


Link to post
Share on other sites
water

@error = 3 stands for: "No records returned from Active Directory. $sFilter didn't return a record"

You are sure there are objects of class "PrintQueue" with attribute "printername" = "p104211"?

Can you run the example script _AD_ListPrintQueues.au3?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Grantson

Thanks again Water 

Ive always been cagey about using the UDF as the docs say not to use in production without testing. 

Had a look at the examples using example 1 which lists all the print queues into an array added a search to find the requested printer and pull the server name to make up the unc path 

All i need to do now is tidy up the WMI stuff 

Share this post


Link to post
Share on other sites
water

The "not to use in production without testing" is not as relevant toda as it was when I started with the UDF. I just have read-only access to the AD so all functions that change the AD needed to be tested by the users out there.

Now - 3 1/2 years after I first published the UDF - it seems quite save.

But testing is never a bady thing!

If you search the forum for "add printer wmi" you will find a lot of useful examples.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×