Grantson Posted May 14, 2013 Posted May 14, 2013 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)
spudw2k Posted May 14, 2013 Posted May 14, 2013 (edited) 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 May 14, 2013 by spudw2k Spoiler Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX Builder Misc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retrieve SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose Array Projects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalc Cool Stuff: AutoItObject UDF ◊ Extract Icon From Proc ◊ GuiCtrlFontRotate ◊ Hex Edit Funcs ◊ Run binary ◊ Service_UDF
water Posted May 14, 2013 Posted May 14, 2013 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 2024-07-28 - Version 1.6.3.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 (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
spudw2k Posted May 14, 2013 Posted May 14, 2013 (edited) Personally, if you are just adding a printer I'd use printui and the printer share name. Edited May 14, 2013 by spudw2k Spoiler Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX Builder Misc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retrieve SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose Array Projects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalc Cool Stuff: AutoItObject UDF ◊ Extract Icon From Proc ◊ GuiCtrlFontRotate ◊ Hex Edit Funcs ◊ Run binary ◊ Service_UDF
Grantson Posted May 14, 2013 Author Posted May 14, 2013 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
water Posted May 14, 2013 Posted May 14, 2013 Using the AD UDF is as easy as using _AD_Open and _AD_GetObjectsInOU. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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
Grantson Posted May 15, 2013 Author Posted May 15, 2013 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
water Posted May 15, 2013 Posted May 15, 2013 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 2024-07-28 - Version 1.6.3.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 (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
Grantson Posted May 15, 2013 Author Posted May 15, 2013 (edited) 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 May 15, 2013 by Grantson
water Posted May 15, 2013 Posted May 15, 2013 @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 2024-07-28 - Version 1.6.3.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 (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
Grantson Posted May 16, 2013 Author Posted May 16, 2013 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
water Posted May 16, 2013 Posted May 16, 2013 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 2024-07-28 - Version 1.6.3.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 (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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now