Jump to content
Sign in to follow this  
stjw38

prnmngr.vbs to autoit

Recommended Posts

stjw38

I have a working knowledge of AutoIt for the most part, but my experience with vbscript is very limited. I'd love to have the functions in prnmngr.vbs converted to AutoIt code so that I can build a GUI around it. If someone could help me isolate and convert one of the functions so I can get a solid idea of how to do it, I'd appreciate it a lot.

And, I'm sure there are other administrators that would appreciate the converted script as well.

I'm speaking of the version of prnmngr.vbs included with Win 7.

Thanks,

Joe

Share this post


Link to post
Share on other sites
water

Welcome to AutoIt and the forum!

Doesn't look too hard to translate to AutoIt. What have you coded so far?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - 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
stjw38

My biggest issue seems to be creating the object type. Below is the code I have so far for listing the printers, but I seem to have an issue with creating the object....

Dim $PrinterList, $PrinterCount

ListPrinters(@Computername)

Func ListPrinters($strServer)

Local $Return

dim $Printers

dim $oService

dim $oPrinter

dim $iTotal

$oPrinters = ObjCreate("Win32_Printer")

$Printers = ObjCreate("Win32_Printer")

$iTotal = 0

For $oPrinter in $Printers

$iTotal = $iTotal + 1

;MsgBox(0,"",L_Empty_Text)

MsgBox(0,"",$strServer)

MsgBox(0,"",$oPrinter.DeviceID)

MsgBox(0,"",$oPrinter.ShareName)

MsgBox(0,"",$oPrinter.DriverName)

MsgBox(0,"",$oPrinter.PortName)

MsgBox(0,"",$oPrinter.Comment)

MsgBox(0,"",$oPrinter.Location)

MsgBox(0,"",$oPrinter.SepFile)

MsgBox(0,"",$oPrinter.PrintProcessor)

MsgBox(0,"",$oPrinter.PrintJobDataType)

MsgBox(0,"",$oPrinter.Parameters)

MsgBox(0,"",String($oPrinter.Attributes))

MsgBox(0,"",String($oPrinter.Priority))

MsgBox(0,"",String($oPrinter.DefaultPriority))

next

EndFunc

Results in "List Printer.au3 (22) : ==> Variable must be of type "Object".:

For $oPrinter in $Printers

For $oPrinter in $Printers^ ERROR"

Share this post


Link to post
Share on other sites
water

You need to translate function WmiConnect so that variable $OService gets set.

Then

Set Printers = oService.InstancesOf("Win32_Printer")
will work.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - 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
mikell

Perhaps scriptomatic.au3 can be useful

; Generated by AutoIt Scriptomatic

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output = $Output & "Computer: " & $strComputer & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Printer", "WQL", _
$wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
For $objItem In $colItems
$Output = $Output & "Attributes: " & $objItem.Attributes & @CRLF
$Output = $Output & "Comment: " & $objItem.Comment & @CRLF
$Output = $Output & "DefaultPriority: " & $objItem.DefaultPriority & @CRLF
$Output = $Output & "DeviceID: " & $objItem.DeviceID & @CRLF
$Output = $Output & "DriverName: " & $objItem.DriverName & @CRLF
$Output = $Output & "Location: " & $objItem.Location & @CRLF
$Output = $Output & "PortName: " & $objItem.PortName & @CRLF
$Output = $Output & "PrintJobDataType: " & $objItem.PrintJobDataType & @CRLF
$Output = $Output & "PrintProcessor: " & $objItem.PrintProcessor & @CRLF
$Output = $Output & "Priority: " & $objItem.Priority & @CRLF
$Output = $Output & "SeparatorFile: " & $objItem.SeparatorFile & @CRLF
$Output = $Output & "ServerName: " & $objItem.ServerName & @CRLF
$Output = $Output & "Shared: " & $objItem.Shared & @CRLF
$Output = $Output & "ShareName: " & $objItem.ShareName & @CRLF
if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
$Output=""
Next
Else
Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_Printer" )
Endif

Share this post


Link to post
Share on other sites
stjw38

Beautiful! I downloaded Scriptomatic to check it out and it offers a lot of good stuff. I don't suppose either of you know if I can use it to generate code to add/remove printers, do you. I now I can shell out to rundll32 printui.dll,PrintUIEntry, but would prefer to use the same method as prnmngr.vbs which uses oPrinter.Put_(kFlagCreateOnly) and oPrinter.Delete_. I don't know how those translate to AutoIt.

And... Thanks, guys, for your help. I've used the code example above to capture the information into an INI file....

Global $wbemFlagReturnImmediately = 0x10

Global $wbemFlagForwardOnly = 0x20

Global $colItems = ""

Global $strComputer = @Computername

Global $objWMIService = ObjGet("winmgmts:" & $strComputer & "rootCIMV2")

Global $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Printer", "WQL",$wbemFlagReturnImmediately + $wbemFlagForwardOnly)

ListPrinters()

Func ListPrinters()

If IsObj($colItems) then

For $objItem In $colItems

IniWrite(@ScriptDir & "Printers.ini",$objItem.DeviceID,"Device ID", $objItem.DeviceID)

IniWrite(@ScriptDir & "Printers.ini",$objItem.DeviceID,"Attributes", $objItem.Attributes)

IniWrite(@ScriptDir & "Printers.ini",$objItem.DeviceID,"Comments", $objItem.Comment)

IniWrite(@ScriptDir & "Printers.ini",$objItem.DeviceID,"Default Priority", $objItem.DefaultPriority)

IniWrite(@ScriptDir & "Printers.ini",$objItem.DeviceID,"Driver Name", $objItem.DriverName)

IniWrite(@ScriptDir & "Printers.ini",$objItem.DeviceID,"Location", $objItem.Location)

IniWrite(@ScriptDir & "Printers.ini",$objItem.DeviceID,"Port Name", $objItem.PortName)

IniWrite(@ScriptDir & "Printers.ini",$objItem.DeviceID,"Print Job Data Type", $objItem.PrintJobDataType)

IniWrite(@ScriptDir & "Printers.ini",$objItem.DeviceID,"Print Processor", $objItem.PrintProcessor)

IniWrite(@ScriptDir & "Printers.ini",$objItem.DeviceID,"Priority", $objItem.Priority)

IniWrite(@ScriptDir & "Printers.ini",$objItem.DeviceID,"Separator File", $objItem.SeparatorFile)

IniWrite(@ScriptDir & "Printers.ini",$objItem.DeviceID,"Server Name", $objItem.ServerName)

IniWrite(@ScriptDir & "Printers.ini",$objItem.DeviceID,"Shared", $objItem.Shared)

IniWrite(@ScriptDir & "Printers.ini",$objItem.DeviceID,"ShareName", $objItem.ShareName)

;if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop

;'$Output=""

Next

Else

Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_Printer" )

Endif

EndFunc

Share this post


Link to post
Share on other sites
water

As far as I know Scriptomatic is for queries only. Search the Web for Visual Basic examples to add or remove printers. Translation to AutoIt is easy then.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - 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
water

mikell,

that looks very promising :thumbsup:


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - 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
BrewManNH

Here's a snippet in VBScript that I use to delete printers, it shouldn't be hard to modify it to AutoIt

Set objNet        = CreateObject("Wscript.Network")
Set WSHPrinters    = objNet.EnumPrinterConnections'
for LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
' To remove only networked printers use this If Statement
    If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then
        objNet.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True, True
    End If
' To remove all printers incuding LOCAL printers use this statement and comment
' out the If Statement above
'    objNet.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
Next

And this is what I use to add a printer by server name, or IP address of the print server, using the name of the printer share, this is also setting the default printer for the computer.

objNet.AddWindowsPrinterConnection "\\server\PrinterShareName"
objNet.SetDefaultPrinter "\\server\PrinterShareName"

EDIT: By the way, do NOT run the first script on your print server as it will delete every printer on it.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
mikell

BrewManNH

My lazy brain still prefers a working nice-built UDF to a raw not-yet-autoit-converted VBscript :)

Share this post


Link to post
Share on other sites
stjw38

This is all beautiful. Thanks for all your help, guys. I think I've got what I needed.

Share this post


Link to post
Share on other sites
BrewManNH

BrewManNH

My lazy brain still prefers a working nice-built UDF to a raw not-yet-autoit-converted VBscript :)

It takes about 2 minutes to convert that to AutoIt syntax, lazy brain indeed. :)

Global $objNet = ObjCreate("Wscript.Network")
Global $WSHPrinters = $objNet.EnumPrinterConnections
For $LOOP_COUNTER = 0 To $WSHPrinters.Count - 1 Step 2
    If StringLeft($WSHPrinters.Item($LOOP_COUNTER + 1), 2) = "\\" Then
        $objNet.RemovePrinterConnection($WSHPrinters.Item($LOOP_COUNTER + 1), True, True)
    EndIf
Next

Second example converted from above.

$objNet.AddWindowsPrinterConnection("\\server\PrinterShareName")
$objNet.SetDefaultPrinter("\\server\PrinterShareName")


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
mikell

2 minutes indeed for someone speaking vb fluently :P

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  

×