Select Default Printer Using Microsoft's prnadmin.dll

Added by Citrix CDN , last edited by Vishal Ganeriwala on Nov 06, 2007  (view change)
Tags: 

Description

This script example enumerates the current default printer and then provides a numbered list of printers from which the user can select a default.

Script Version

1.0

How to Use

This script requires that prnadmin.dll from the Windows 2003 Resource Kit be registered on the machine that runs the script. For more examples and further information, refer to the prnadmin.doc that is included with the Windows 2003 Resource Kit.

Windows Server 2003 Resource Kit Tools

If the script is to be launched from a published batch file, it is important to clear the "Start this application without waiting for printers to autocreated" option in the Citrix Management Console when the batch file is published. Failing to clear this option allows the script to enumerate printers while the printers are still autocreating. This causes the script to lock up until the printers are fully built. Clearing the option will show users a status message from the ICA client, but will not run the script until the printers are built and can then be queried.

Paste the script into Notepad and save as fileName.vbs. Double-click the file. You can also run the file from the command prompt with the following command:

Cscript filename.vbs

Code Snippet

Option Explicit
Dim Text, Title, i, j, tmp, printer, PrinterText, invalid, count
Dim WshNetwork, oDevices, oMaster, objshell
' vbcrlf.....Create Line-numbered list
' vbOKonly...OK button only
' This script requires W2k3 and prnadmin.dll from the resource kit to be registered.
' To register dll ....regsvr32 %path%\prnadmin.dll
Set objShell = CreateObject("Wscript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oDevices = WshNetwork.EnumPrinterConnections
set oMaster = CreateObject("PrintMaster.PrintMaster.1")
' invalid...Test variable for input conditions
invalid=0
Title="Citrix 2005"
WScript.Echo " Your Current Default Printer:" & oMaster.DefaultPrinter
Text = "Enter the # of the printer to set as the default." & vbcrlf

count = oDevices.Count
For i = 0 To count - 1 Step 2
         Text = Text & (i/2) & vbTab
         Text = Text & oDevices(i) & oDevices(i+1) & vbCrLf
Next

tmp = InputBox(Text, "Select default printer", 0)

If tmp = "" Then
         WScript.Echo "No input, Your default printer remains: " & oMaster.DefaultPrinter
         invalid=1
End IF

tmp = CInt(tmp)

If (tmp < 0) Or (tmp > (count/2 - 1)) then
        WScript.Echo "Invalid value,Nothing changed. Default is still: " &oMaster.DefaultPrinter
        invalid=1
end if
If (invalid=0) then
       printer = oDevices(tmp*2 + 1)
       WshNetwork.SetDefaultPrinter printer
       MsgBox "Your default printer has been successfully set to " &printer, _
       vbOKOnly, Title
end if
objShell.Run ("notepad.exe"),1,True

Disclaimer

These software applications are provided to you as is with no representations, warranties or conditions of any kind. You may use and distribute it at your own risk. CITRIX DISCLAIMS ALL WARRANTIES WHATSOEVER, EXPRESS, IMPLIED, WRITTEN, ORAL OR STATUTORY, INCLUDING WITHOUT LIMITATION WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NONINFRINGEMENT. Without limiting the generality of the foregoing, you acknowledge and agree that (a) the software application may exhibit errors, design flaws or other problems, possibly resulting in loss of data or damage to property; (b) it may not be possible to make the software application fully functional; and (c) Citrix may, without notice or liability to you, cease to make available the current version and/or any future versions of the software application. In no event should the code be used to support of ultra-hazardous activities, including but not limited to life support or blasting activities. NEITHER CITRIX NOR ITS AFFILIATES OR AGENTS WILL BE LIABLE, UNDER BREACH OF CONTRACT OR ANY OTHER THEORY OF LIABILITY, FOR ANY DAMAGES WHATSOEVER ARISING FROM USE OF THE SOFTWARE APPLICATION, INCLUDING WITHOUT LIMITATION DIRECT, SPECIAL, INCIDENTAL, PUNITIVE, CONSEQUENTIAL OR OTHER DAMAGES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. You agree to indemnify and defend Citrix against any and all claims arising from your use, modification or distribution of the code.