0

I have a user that wants to have her local desktop printer the default printer when she logs in to our terminal server. I had created a simple login script for this

Set WSHNetwork = CreateObject("WScript.Network")
WSHNetwork.SetDefaultPrinter "HP LaserJet 6P (redirected 3)"

The problem with this is that, for whatever reason, the printer name keeps changing the last digit. HP LaserJet 6P (redirected), HP LaserJet 6P (redirected 2), and HP LaserJet 6P (redirected 1) are all examples of how the printer shows up.

I don't know VBScript well enough to account for these changes and am hoping someone will help me to find which variation of the name is being used, and set that as the default printer.

I found a snippet that may help, but I'm unsure how to properly implement it.

Function printerExists(str)
    printerExists = False
    Dim objWMIService
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

    Dim colPrinters
    Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")

    Dim objPrinter
    For Each objPrinter In colPrinters
        If objPrinter.Name = str Then
            printerExists = True
            Exit For
        End If
    Next
End Function
Josh
  • 1
  • 1
  • 1
  • I'd recommend to fix the name-changing rather than trying to work around it. – Ansgar Wiechers May 28 '13 at 17:41
  • I've looked into that but apparently the client printers are dynamically added to the terminal server. So depending on how many other local printers are connected to the terminal server over RDP sessions determines what the printer is renamed to. I couldn't find any way around this other than to share her printer out and then add that to the terminal server and then set the shared as her default (which then shouldn't change). That actually may be a better solution than this logon script. Hmmm... – Josh May 28 '13 at 18:03

3 Answers3

0

If you can't fix the name changing thing, you should be able to work around it.

Start with a looping construct. In this case we'll use a for loop:

For i = 0 To 5
    'things happen
next

I chose to limit the number to 5 or lower, but that can be changed to fit your needs. Then we'll need a method to include all the possibilities for the printer name. If all te names included a number that would not take any special code, but here we'll use an if statement to say that if the number is 0, don't include the number.

If i = 0 Then
    testPrinter = "HP LaserJet 6P (redirected)"
Else
    testPrinter = "HP LaserJet 6P (redirected " & i & ")"
End If

Then, we'll make use of the printerExists function you provided to determine whether the printer exists, and to set the default printer if it does.

If printerExists(testprinter) = True Then
    WSHNetwork.SetDefaultPrinter testPrinter
    WScript.Quit
End If

To finish the script off, we put all the pieces together and add the printerExists function. The final script will end up looking something like this:

Set WSHNetwork = CreateObject("WScript.Network")

i = 1 : testPrinter = "HP LaserJet 6P (redirected)"

For i = 0 To 5
    If i = 0 Then
        testPrinter = "HP LaserJet 6P (redirected)"
    Else
        testPrinter = "HP LaserJet 6P (redirected " & i & ")"
    End If

    If printerExists(testprinter) = True Then
        WSHNetwork.SetDefaultPrinter testPrinter
        WScript.Quit
    End If
next

Function printerExists(str)
    printerExists = False
    Dim objWMIService
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

    Dim colPrinters
    Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")

    Dim objPrinter
    For Each objPrinter In colPrinters
        If objPrinter.Name = str Then
            printerExists = True
            Exit For
        End If
    Next
End Function

Keep in mind that string comparisons such as the one that is used in the function:

If objPrinter.Name = str Then

are case sensitive.

Mark
  • 181
  • 2
0

You can change that code to just look for anything that contains the first part of the name, "HP LaserJet 6P (redirected":

Dim WSHNetwork
Dim objWMIService
Dim colPrinters
Dim objPrinter

Set WSHNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")

For Each objPrinter In colPrinters
    If InStr(objPrinter.Name, "HP LaserJet 6P (redirected") Then
        WSHNetwork.SetDefaultPrinter objPrinter.Name
        Exit For
    End If
Next

Set WSHNetwork = nothing
Set objWMIService = nothing
Set colPrinters = nothing
Joe M
  • 3,060
  • 3
  • 40
  • 63
0

The printer's name is dynamic because it is redirecting through a terminal server or remote desktop connection from the local workstation's list of printers. If someone else has the same model printer as the one being tested for in the above code, the other person's printer could be set a this person's default by mistake.

An RDP connection or terminal server is always going to try to set your local default printer as your redirected default printer so to make the "HP Laserjet 6P (redirected)" your default you must make the "HP Laserjet 6P" on the local workstation your default before you connect to the server.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179