Jump to content

dsquery - works from command line but autoit returns blank string


Recommended Posts

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)
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Link to comment
Share on other sites

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 

Link to comment
Share on other sites

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

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

@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 (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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 

Link to comment
Share on other sites

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 (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...