supermanzdead
(Getting the hang of it)
2005-04-27 01:58 PM
Display Default Printer with WMI Question

Code:

Function ListDefaultPrinterWMI()
$txtBox.text = ""
For Each $strItem in $lstListView.SelectedItems
$txtBox.appendtext("Listing Default Printer for " + $strItem.text + @CRLF)
$objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $strItem.text + "\root\CIMV2")
If @error = 0
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Printer")
For Each $strObj in $colItems
If $strObj.Default = "True"
$txtBox.appendtext(" " + $strObj.Name + @CRLF)
EndIf
Next
Else
$txtBox.appendtext(" Error connecting to this workstation via WMI!" + @CRLF)
EndIf
Next
EndFunction



Above is the code I came up with to display the default printer, the only problem is that it wont display networked printers under the person whose logged in's profile if you know what I mean. Can any of you WMI experts help me out?


Radimus
(KiX Supporter)
2005-04-27 02:14 PM
Re: Display Default Printer with WMI Question

You are impersonating to a remote connection.
Network printers is a user specific setting, with network permissions

you will only get A) network printers that YOU connected while on THAT PC, or B) network printers that the Local System account has, i.e. none

Unless someone has another trick, WMI will not be the way.


supermanzdead
(Getting the hang of it)
2005-04-27 02:40 PM
Re: Display Default Printer with WMI Question

I am gussing there is a registry key somewhere under HKCU then...but I like WMI!! *cry*

Les
(KiX Master)
2005-04-27 02:52 PM
Re: Display Default Printer with WMI Question

Is "gussing" like cussing? Yes there is reg key under HKCU but HKCU is smoke and mirrors so it does not exist remotely.

Radimus
(KiX Supporter)
2005-04-27 02:58 PM
Re: Display Default Printer with WMI Question

hkusers\(sid of remote current user) NOT @sid, as that is you

Kdyer
(KiX Supporter)
2005-04-27 03:04 PM
Re: Display Default Printer with WMI Question

If we look at -
KiXtart FAQ & How to's

We find:
Default Printer Configuration

This is not as "elegant" as WMI, but gets the job done.

Kent


NTDOCAdministrator
(KiX Master)
2005-04-27 07:20 PM
Re: Display Default Printer with WMI Question

Here is a UDF to return all the printer connections.

EnumPrinterConnections2() - Enumerates all connected printers

And here is some code to display the current Default Printer

Code:
Break On
Dim $DefPrint
$DefPrint = Split(ReadValue('HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows','Device'),',')[0]
If $DefPrint
? $DefPrint
EndIf




These methods require you to run this and log it during the logon process. Otherwise you would need to remotely load the users profile, or use an @SID method WHILE the remote user is actively logged on.