Jump to content



Photo

_Printers_Add_Remove_Shared


  • Please log in to reply
15 replies to this topic

#1 stamandster

stamandster

    Wayfarer

  • Active Members
  • Pip
  • 58 posts

Posted 04 September 2008 - 03:01 PM

Hi All,

Thought I'd share a UDF I created for myself to add network printers. There wasn't one that wouldn't error out so I sat down and wrote it in an afternoon.
So basically you can add and delete one printer or multiple printers. Separate each printer string with a comma.

The first variable, $sMapPrinters, you can use either \\Server1\PrinterShare or just Server1\PrinterShare . If you add a \1 to the end of the path it will tell the UDF to make that the default printer. You do not need to do \0 on the rest of the printers. Which would make it \\Server1\PrinterShare\1 or Server1\PrinterShare\1 . If you want to add multiple printers you will need to set it up this way "\\Server1\PrinterShare1\1,\\Server1\PrinterShare2,\\Server2\PrinterShare" . When you add only one printer you can do \\Server1\Printer1\1 or \\Server1\Printer1 or Server1\Printer1\1 or Server1\Printer1 . You can now use a \-1 to remove a printer!

The second variable, $iOverrideDefaultPrinter, is optional. It can override the currently set default printer on the machine with the one you specified. The default is 0 or nothing, which will not override the default printer. Setting the variable to 1 will make the printer you chose the default printer. Even if the printer is already added.

UDF is attached.

Revisions
v1.6 - Fixed an issue with compiling, forgot to Local a variable
v1.5 - Added the ability to remove a shared printer with a -1 ; Changed UDF name to _Printers_Add_Remove_Shared
v1.4 - Changed so that you don't need a comma at the end when you are attaching only one printer
v1.3 - Added a var that sets if a printer was added and only then will it restart spooler, else it doesn't; Converted script to standard UDF
v1.2 - Fixed a return value that was screwey
v1.1 - Fixed issue with only adding one printer; after the printer line just add a comma like so Server1\Printer1\1,
v1.0 - First release with only support for shared network printers, set default printer and override the previous default printer

Attached Files


Edited by kickarse, 09 September 2008 - 03:48 PM.






#2 dmob

dmob

    Prodigy

  • Active Members
  • PipPipPip
  • 191 posts

Posted 04 September 2008 - 03:12 PM

Very useful script. Great work @kickarse; thanks for sharing.

#3 archgriffin

archgriffin

    Wayfarer

  • Active Members
  • Pip
  • 58 posts

Posted 04 September 2008 - 03:53 PM

Perfect!
Well, exactly something I am looking for and will be testing once we are done moving around our entire building of people...

But thank you! Will write back if I have any issues.
"Human kind cannot gain anything without first giving something in return, to obtain; something of equal value must be lost."The Help File is truly your friend.

#4 YogiBear

YogiBear

    deFinitely not yoUr average bear!

  • MVPs
  • 9,863 posts

Posted 04 September 2008 - 03:57 PM

Here is my take on it. I designed mine to do groups of printers at once. The only thing I couldn't solve is how to install the printer if it was hooked to a Novell server.

Plain Text         
#cs     Network Printer Utility 1.0 beta     Designed by Volly and other folks     July 2006     A big THANK YOU goes to big_daddy. His help     was invaluable in fixing some issues.     @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@     History     July 2006: Initial release.         ---------------     Known issues:     Will not work on Novell servers.     #ce #include <GuiConstantsEX.au3> #include <WindowsConstants.au3> #include <Process.au3> #include <Array.au3> $g_szVersion = "PrintInst.exe" If WinExists($g_szVersion) Then Exit Opt("TrayIconDebug", 1);debug is on Dim $hService, _         $data, _         $sPrinter, _         $sPrinterList, _         $font = "Comic Sans MS", _         $Input[12], $Combo[7], $radio[7], _         $check[11], $Button[6] Global Enum Step + 25 _         $CON_POS_1 = 25, _         $CON_POS_2, _         $CON_POS_3, _         $CON_POS_4, _         $CON_POS_5, _         $CON_POS_6, _         $CON_POS_7, _         $CON_POS_8, _         $CON_POS_9, _         $CON_POS_10, _         $CON_POS_11 GUICreate("", 600, 290, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS)) GUICtrlCreateLabel("\\", 20, 54, 10, 20) $iniread_1 = IniRead(@ScriptDir & "/npu.ini", "SERVER", "SERVER", "") $iniread_2 = IniRead(@ScriptDir & "/npu.ini", "Printers", "PLIST", "") iniwrite(@ScriptDir & "/npu.ini", "Printers", "LOCAL", "") $iniread_3 = IniRead(@ScriptDir & "/npu.ini", "Printers", "LOCAL", "") $LocalPrinters = StringSplit($iniread_3, "|") If $LocalPrinters[0] > 10 Then     ReDim $LocalPrinters[11] EndIf $Input[1] = GUICtrlCreateInput($iniread_1, 35, 50, 100, 20) For $i = 2 To 11     $Input[$i] = GUICtrlCreateInput("", 270, Eval("CON_POS_" & $i - 1), 270, 21)     If ($i - 1) <= $LocalPrinters[0]Then         GUICtrlSetData(-1, $LocalPrinters[$i - 1])     EndIf Next For $i = 1 To 6     $Combo[$i] = GUICtrlCreateCombo("", 20, Eval("CON_POS_" & $i + 2), 170, 21)     GUICtrlSetData(-1, $iniread_2)     $radio[$i] = GUICtrlCreateRadio("", 210, Eval("CON_POS_" & $i + 2), 15, 21) Next For $i = 1 To 10     $check[$i] = GUICtrlCreateCheckbox("", 550, Eval("CON_POS_" & $i), 20, 20) Next GUICtrlCreateLabel("Currently install printers", 270, 3, 200, 21) GUICtrlCreateLabel("Delete", 550, 7, 100, 21) GUICtrlCreateGroup("Default", 200, 62, 50, 160) $Button[1] = GUICtrlCreateButton("Run", 20, 225, 35, 30) $Button[2] = GUICtrlCreateButton("Exit", 65, 225, 35, 30) $Button[3] = GUICtrlCreateButton("Find", 146, 50, 43, 20) $Button[4] = GUICtrlCreateButton("Find", 498, 3, 43, 20) $Button[5] = GUICtrlCreateButton("Restart Spooler", 110, 225, 80, 30) $Label_1 = GUICtrlCreateLabel("   Network Printer Utility" & @CRLF & "for Windows Print Servers", 20, 0, 240, 50) GUICtrlSetFont(-1, 10, 400, "", $font) $Label_2 = GUICtrlCreateLabel("2006 CTS Scripting Team", 40, 270, 130, 25) $_dummy = GUICtrlCreateDummy() GUISetState() While 1     $msg = GUIGetMsg()     Switch $msg         Case $GUI_EVENT_CLOSE             ExitLoop         Case $Button[1]             _Rprinter()                     _instprint()             MsgBox(0, "", "Done!", 3)         Case $Button[2]             ExitLoop         Case $Button[3]             $data = GUICtrlRead($Input[1], 0)             _netprint1()         Case $Button[4]             _getinstalledPrinters()         Case $Button[5]             _restart()         Case Else            ;;;     EndSwitch WEnd Exit Func _netprint1()     $hService = ObjGet("winmgmts:{impersonationLevel=impersonate}!" & "\\" & $data & "\root\cimv2")     If @error Then         MsgBox(48, "ERROR", "No Printers Found. Possible issues: " & @CRLF _                  & "" & @CRLF _                  & "  1. The Windows Print Server name has been entered in incorrectly." & @CRLF _                  & "  2. You are trying to access a Novell Server. This utility does not support Novell Print Servers." & @CRLF _                  & "  3. There are no printers shared on the Windows Print Server you selected.")     Else         _np2()     EndIf EndFunc  ;==>_netprint1 Func _np2()     Local $i = 0     Local $sPrinters = ""     Local $aArray[1]         SplashTextOn("", "Gathering printers, please wait...", 150, 75)         For $y = 1 To 6         GUICtrlSetData($Combo[$y], "")     Next     IniWrite(@ScriptDir & "/npu.ini", "Printers", "PLIST", "")         $oPrinterList = $hService.ExecQuery ("Select * From Win32_Printer")         For $oPrinter In $oPrinterList         ReDim $aArray[$i + 1]         $aArray[$i] = StringUpper($oPrinter.name)         $i += 1     Next         _ArraySort($aArray)         For $i = 0 To UBound($aArray)-1         For $y = 1 To 6             GUICtrlSetData($Combo[$y], $aArray[$i])         Next     Next             $iElements = UBound($aArray) - 1     For $x = 0 To $iElements         If $x = $iElements Then             $sPrinters &= $aArray[$x]         Else             $sPrinters &= $aArray[$x] & "|"         EndIf     Next         IniWrite(@ScriptDir & "/npu.ini", "Printers", "PLIST", $sPrinters)     $dataget2 = GUICtrlRead($Input[1], 0)     IniWrite(@ScriptDir & "/npu.ini", "SERVER", "SERVER", $dataget2)     SplashTextOn("", "Select what printers you wish to install", 150, 75)     Sleep(2000)     SplashOff() EndFunc  ;==>_np2 Func _instprint()     Local $ComboGet[7], $RadioGet[7]         SplashTextOn("", "Installing printer, please wait...", 150, 75)         $InputGet = GUICtrlRead($Input[1], 0)         For $i = 1 To 6         $ComboGet[$i] = GUICtrlRead($Combo[$i], 0)         $RadioGet[$i] = GUICtrlRead($radio[$i], 0)     Next         For $i = 1 To 6         If $ComboGet[$i] <> "" Then                         RunWait(@SystemDir & "\RUNDLL32 PRINTUI.DLL,PrintUIEntry /ga /c\\" & @ComputerName & " /n\\" & $InputGet & "\" & $ComboGet[$i])           EndIf     Next     _RunDOS("NET STOP spooler")     _RunDOS("NET START spooler")     sleep(10000)     For $i = 1 To 6             If $RadioGet[$i] = 1 Then                 If $ComboGet[$i] <> "" Then                 RunWait(@SystemDir & "\RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n\\"&$iniread_1&"\"& $ComboGet[$i])                 endif             EndIf               GUICtrlSetData($Combo[$i],"")             GUICtrlSetState($radio[$i], $GUI_UNCHECKED)             sleep(50)             GUICtrlSetData($Combo[$i],$iniread_2)                 Next     SplashOff() EndFunc  ;==>_instprint func _restart()     SplashTextOn("", "Stopping Print Spooler, please wait...", 150, 75)     _RunDOS("NET STOP spooler")     SplashOff()     SplashTextOn("", "Starting Print Spooler, please wait...", 150, 75)     _RunDOS("NET START spooler")     SplashOff() EndFunc Func _getinstalledPrinters()         SplashTextOn("", "Gathering installed printers, please wait...", 200, 75)         IniWrite(@ScriptDir & "/npu.ini", "Printers", "LOCAL", "")     $hService = ObjGet("winmgmts:{impersonationLevel=impersonate}!" & "\\" & @ComputerName & "\root\cimv2")     $sPrinterList = $hService.ExecQuery ("Select * From Win32_Printer")     ConsoleWrite('$sPrinterList.count = ' & $sPrinterList.count & @LF)     For $sPrinter In $sPrinterList         ConsoleWrite('$sPrinter.name = ' & $sPrinter.name & @LF)         $rd3 = IniRead(@ScriptDir & "/npu.ini", "Printers", "LOCAL", "")         IniWrite(@ScriptDir & "/npu.ini", "Printers", "LOCAL", $rd3 & $sPrinter.name & "|")     Next     $local = StringSplit(IniRead(@ScriptDir & "/npu.ini", "Printers", "LOCAL", ""), "|")     If UBound($local) < 11 Then ReDim $local[11]     For $i = 1 To 10         GUICtrlSetData($Input[$i + 1], $local[$i])     Next         SplashOff() EndFunc  ;==>_getinstalledPrinters Func _Rprinter()     Local $InputGet[11]     For $i = 2 To 11         $InputGet[$i - 1] = GUICtrlRead($Input[$i])     Next     For $i = 1 To 10         If GUICtrlRead($check[$i]) = 1 Then             SplashTextOn("", "Removing printer, please wait...", 150, 50)             GUICtrlSetState($check[$i], $GUI_UNCHECKED)             sleep(1000)             If $InputGet[$i] <> "" Then                 $str = StringLeft($InputGet[$i], 2)                 If $str = "\\" Then                     RunWait(@SystemDir & "\RUNDLL32 PRINTUI.DLL,PrintUIEntry /gd /dn /n /c\\" & @ComputerName & " /n" & $InputGet[$i])                 ;RunWait(@SystemDir & "\RUNDLL32 PRINTUI.DLL,PrintUIEntry /gd /q /c\\" & @ComputerName & " /n" & $InputGet[$i]);mine original                 ;RunWait(@ComSpec & ' /c RUNDLL32 PRINTUI.DLL,PrintUIEntry /gd /dn /q /n "' & $ret & '"', '', @SW_HIDE);danny35                     GUICtrlSetData($InputGet[$i], "")                 Else                     RunWait(@SystemDir & '\RUNDLL32 PRINTUI.DLL,PrintUIEntry /dl /q /n "' & $InputGet[$i] & '" /c\\' & @ComputerName)                 EndIf             EndIf         EndIf     Next     EndFunc  ;==>_Rprinter ;#cs - printer install ;adds a printer ;Run(@SystemDir & "\RUNDLL32 PRINTUI.DLL,PrintUIEntry /ga /c\\" &@ComputerName& " /n\\va017f1a\19_4200_82.248_PCL") ;&&&& ;deletes a printer ;Run(@SystemDir & "\RUNDLL32 PRINTUI.DLL,PrintUIEntry /gd /c\\" [email="&@ComputerName"]&@ComputerName[/email]& " /n\\va017f1a\19_4200_82.248_PCL") ;_rundos("NET STOP spooler") ;_rundos("NET START spooler") ; #ce


#5 stamandster

stamandster

    Wayfarer

  • Active Members
  • Pip
  • 58 posts

Posted 04 September 2008 - 07:51 PM

Thanks all!

Yeah I didn't want to use the printui.dll because it sometimes flakes out when your loading a profile for the first time on a machine. For whatever reason. That's why it's only reg imports and one restart of the spooler.

Hopefully it works for everyone!

#6 stamandster

stamandster

    Wayfarer

  • Active Members
  • Pip
  • 58 posts

Posted 06 September 2008 - 03:18 PM

Just wanted to let everyone know I'm going to be making a delete printer and find connected printer udf. Plus I'll be making them and converting this one to be in accordance with the UDF guidelines.

#7 stamandster

stamandster

    Wayfarer

  • Active Members
  • Pip
  • 58 posts

Posted 08 September 2008 - 02:50 PM

Script Updated!

#8 martin

martin

    ~~\o/~~~/0\=¬''~~~

  • MVPs
  • 7,199 posts

Posted 08 September 2008 - 05:04 PM

Script Updated!

The comma is a separator for the printer list, but when there is nothing to separate it doesn't feel like it should be neeeded. Your first version didn't seem to need a comma at the end so why did you add that requirement?

Edited by martin, 08 September 2008 - 05:04 PM.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

#9 stamandster

stamandster

    Wayfarer

  • Active Members
  • Pip
  • 58 posts

Posted 08 September 2008 - 06:44 PM

The comma is a separator for the printer list, but when there is nothing to separate it doesn't feel like it should be neeeded. Your first version didn't seem to need a comma at the end so why did you add that requirement?


It was to keep the script smaller. I'd have to create another case for that specifically. I guess I could.

ed. It's updated! You don't need the extra comma when adding only one printer!

Edited by kickarse, 08 September 2008 - 06:52 PM.


#10 stamandster

stamandster

    Wayfarer

  • Active Members
  • Pip
  • 58 posts

Posted 09 September 2008 - 01:15 PM

Updated v1.5 allows you to remove a shared printer. The name of the udf has also changed to _Printers_Add_Remove_Shared

#11 stamandster

stamandster

    Wayfarer

  • Active Members
  • Pip
  • 58 posts

Posted 09 September 2008 - 03:49 PM

Updated v1.6 - Fixes an issue with a undeclared variable

#12 Pies

Pies

    Seeker

  • Active Members
  • 22 posts

Posted 11 September 2008 - 09:41 PM

hi, do you also have an automated program there that can edit the printer's name. Because everyday, I need to edit a bunch of printer's name on 5 servers. And it took me an hour to finish all of those printers. And I need to do it 3x a day. haha! We had an automated program that editted the printers before, but it's not working right now. And the programmer who made it is not working here anymore.
"Failure is the key to success. So try and try until you succeed!"

#13 stamandster

stamandster

    Wayfarer

  • Active Members
  • Pip
  • 58 posts

Posted 12 September 2008 - 01:42 AM

hi, do you also have an automated program there that can edit the printer's name. Because everyday, I need to edit a bunch of printer's name on 5 servers. And it took me an hour to finish all of those printers. And I need to do it 3x a day. haha! We had an automated program that editted the printers before, but it's not working right now. And the programmer who made it is not working here anymore.


I don't currently... sorry.

#14 meokey

meokey

    Seeker

  • Active Members
  • 15 posts

Posted 06 October 2008 - 06:33 PM

I dont know why but it seems do not work on Vista as well as it does on XP.

#15 Wooltown

Wooltown

    Alpha Geek

  • Active Members
  • PipPipPipPipPipPip
  • 478 posts

Posted 07 October 2008 - 07:11 AM

I made a version of this some years ago, uses windows printui.dll

Plain Text         
;=============================================================================== ; ; Function Name:    _PrinterAdd() ; Description:    Connects to a Network Printer. ; Parameter(s):  $sPrinterName - Computer Network name and printer share name (\\Computer\SharedPrinter). ;                  $fDefault - Set to 1 if Printer should be set to default (optional). ; Requirement(s):   ; Return Value(s):  1 - Success, 0 - Failure, If Printer already exist @error = 1. ; Author(s):        Sven Ullstad - Wooltown ; Note(s):        None. ; ;=============================================================================== Func _PrinterAdd($sPrinterName, $fDefault = 0)     If _PrinterExist($sPrinterName) Then         SetError(1)         Return 0     Else         RunWait ("rundll32 printui.dll,PrintUIEntry /in /n" & $sPrinterName & " /q")         If _PrinterExist($sPrinterName) = 0 Then             Return 0         EndIf         If $fDefault = 1 Then             _PrinterDefault($sPrinterName)         EndIf         Return 1     EndIf EndFunc ;=============================================================================== ; ; Function Name:    _PrinterDefault() ; Description:    Set a printer to default. ; Parameter(s):  $sPrinterName - The name of the printer. ; Requirement(s):   ; Return Value(s):  1 - Success, 0 - Failure. ; Author(s):        Sven Ullstad - Wooltown ; Note(s):        None. ; ;=============================================================================== Func _PrinterDefault($sPrinterName)     If _PrinterExist($sPrinterName) Then         RunWait ("rundll32 printui.dll,PrintUIEntry /y /n" & $sPrinterName)         Return 1     Else         Return 0     EndIf EndFunc ;=============================================================================== ; ; Function Name:    _PrinterDelete() ; Description:    Delete a connection to a network printer. ; Parameter(s):  $sPrinterName - Computer Network name and printer share name. ; Requirement(s):   ; Return Value(s):  1 - Success, 0 - Failure. ; Author(s):        Sven Ullstad - Wooltown ; Note(s):        None. ; ;=============================================================================== Func _PrinterDelete($sPrinterName)     If _PrinterExist($sPrinterName) Then         RunWait ("rundll32 printui.dll,PrintUIEntry /dn /n" & $sPrinterName)         If _PrinterExist($sPrinterName) Then             Return 0         Else             Return 1         EndIf     Else         Return 0     EndIf EndFunc ;=============================================================================== ; ; Function Name:    _PrinterExist() ; Description:    Check if a Printer Exist. ; Parameter(s):  $sPrinterName - The name of the printer. ; Requirement(s):   ; Return Value(s):  1 - Success, 0 - Failure. ; Author(s):        Sven Ullstad - Wooltown ; Note(s):        None. ; ;=============================================================================== Func _PrinterExist($sPrinterName)     Local $hService, $sPrinter, $sPrinterList     $hService = ObjGet ("winmgmts:{impersonationLevel=impersonate}!" & "\\" & @ComputerName & "\root\cimv2")     If Not @error = 0 Then         Return 0     EndIf     $sPrinterList = $hService.ExecQuery ("Select * From Win32_Printer")     For $sPrinter In $sPrinterList         If StringUpper($sPrinterName) = StringUpper($sPrinter.name) Then             Return 1         EndIf     Next EndFunc


#16 Wooltown

Wooltown

    Alpha Geek

  • Active Members
  • PipPipPipPipPipPip
  • 478 posts

Posted 07 October 2008 - 07:11 AM

I made a version of this some years ago, uses windows printui.dll and it works on Vista as well !

Plain Text         
;=============================================================================== ; ; Function Name:    _PrinterAdd() ; Description:    Connects to a Network Printer. ; Parameter(s):  $sPrinterName - Computer Network name and printer share name (\\Computer\SharedPrinter). ;                  $fDefault - Set to 1 if Printer should be set to default (optional). ; Requirement(s):   ; Return Value(s):  1 - Success, 0 - Failure, If Printer already exist @error = 1. ; Author(s):        Sven Ullstad - Wooltown ; Note(s):        None. ; ;=============================================================================== Func _PrinterAdd($sPrinterName, $fDefault = 0)     If _PrinterExist($sPrinterName) Then         SetError(1)         Return 0     Else         RunWait ("rundll32 printui.dll,PrintUIEntry /in /n" & $sPrinterName & " /q")         If _PrinterExist($sPrinterName) = 0 Then             Return 0         EndIf         If $fDefault = 1 Then             _PrinterDefault($sPrinterName)         EndIf         Return 1     EndIf EndFunc ;=============================================================================== ; ; Function Name:    _PrinterDefault() ; Description:    Set a printer to default. ; Parameter(s):  $sPrinterName - The name of the printer. ; Requirement(s):   ; Return Value(s):  1 - Success, 0 - Failure. ; Author(s):        Sven Ullstad - Wooltown ; Note(s):        None. ; ;=============================================================================== Func _PrinterDefault($sPrinterName)     If _PrinterExist($sPrinterName) Then         RunWait ("rundll32 printui.dll,PrintUIEntry /y /n" & $sPrinterName)         Return 1     Else         Return 0     EndIf EndFunc ;=============================================================================== ; ; Function Name:    _PrinterDelete() ; Description:    Delete a connection to a network printer. ; Parameter(s):  $sPrinterName - Computer Network name and printer share name. ; Requirement(s):   ; Return Value(s):  1 - Success, 0 - Failure. ; Author(s):        Sven Ullstad - Wooltown ; Note(s):        None. ; ;=============================================================================== Func _PrinterDelete($sPrinterName)     If _PrinterExist($sPrinterName) Then         RunWait ("rundll32 printui.dll,PrintUIEntry /dn /n" & $sPrinterName)         If _PrinterExist($sPrinterName) Then             Return 0         Else             Return 1         EndIf     Else         Return 0     EndIf EndFunc ;=============================================================================== ; ; Function Name:    _PrinterExist() ; Description:    Check if a Printer Exist. ; Parameter(s):  $sPrinterName - The name of the printer. ; Requirement(s):   ; Return Value(s):  1 - Success, 0 - Failure. ; Author(s):        Sven Ullstad - Wooltown ; Note(s):        None. ; ;=============================================================================== Func _PrinterExist($sPrinterName)     Local $hService, $sPrinter, $sPrinterList     $hService = ObjGet ("winmgmts:{impersonationLevel=impersonate}!" & "\\" & @ComputerName & "\root\cimv2")     If Not @error = 0 Then         Return 0     EndIf     $sPrinterList = $hService.ExecQuery ("Select * From Win32_Printer")     For $sPrinter In $sPrinterList         If StringUpper($sPrinterName) = StringUpper($sPrinter.name) Then             Return 1         EndIf     Next EndFunc

Edited by Wooltown, 07 October 2008 - 07:17 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users