0

I was troubleshooting some vbscript designed to query the default printer of a remote computer when I discovered the error in the script was actually because WMI is returning conflicting or even erroneous information. Please see this screenshot: alt text

Both are against the same computer (CLIFGRIFFIN-PC). The left one is run from a Windows Server 2003 install. The right one is run on the PC itself.

The left one, in addition to having fewer results, also shows that NONE of the printers are default. Which is what is causing the scripting error that led me to investigate this.

Any ideas? It's just bizarre.

EDIT: Here is the code I'm using...

Set WshShell = CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections

Dim strComputer 
strComputer = WshShell.ExpandEnvironmentStrings("%CLIENTNAME%")

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters =  objWMIService.ExecQuery ("Select * from Win32_Printer Where Default = True")

For Each Printer in colInstalledPrinters
    For i = 0 to OPrinters.Count - 1 Step 2
        If InStr(objPrinters.Item(i+1), Printer.Name) > 0 Then
            WshNetwork.SetDefaultPrinter(objPrinters.Item(i+1))
            Exit For
        End If
    Next
Next 
clifgriffin
  • 2,008
  • 3
  • 21
  • 41
  • 1
    I believe that the results depend on the user account the script is executed by. Different accounts can have different default printer, and some printers may not be installed for all acounts. –  Nov 05 '10 at 15:52
  • The accounts are the same. That's a good idea though...something to remember for the future. Any other ideas? :) – clifgriffin Nov 05 '10 at 18:16

2 Answers2

0

It may depend on the loged in user as were said.

when you query from a remote pc you will only get locally installed printers as a result. when you query from the same pc you will get all network connected printers as well. Network connected here means they are shared on other pcs.

There is a bit of confusion when you add a network printer but it is called local in windows but it has a ip port.

Alexander Taran
  • 6,655
  • 2
  • 39
  • 60
  • The number of printers isn't as disturbing to me as which one is default. That's the one causing my WMI query to bomb. – clifgriffin Nov 15 '10 at 13:49
0

Try setting the impersonation level to impersonate i.e.

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
chrixian
  • 66
  • 2
  • I've updated my post with the code I'm attempting to use that's failing because of this. I'm actually using impersonate...probably should have given you all the information. Sorry :) – clifgriffin Nov 15 '10 at 13:48