Page 1 of 1 1
Topic Options
#40925 - 2003-06-02 05:09 PM Enumerating shared printers
sliver Offline
Getting the hang of it

Registered: 2002-09-05
Posts: 94
[Moderator (Sealeopard): Moved thread from 'Scripts' to 'Starters' forum due to lack of script in body]

I am trying to write a script to enumerate shared printers. I thought maybe VBS\WSH had a command to do this but I could'nt find it.
As a result I turned to kix to try to parse some registry strings to allow me to just get the printer name itself and then look in the registry under a different key to check the "Shared" registry key to see if it has a value (kind of a crappy way to do it but I can't seem to figure anything else out). I searched the site and checked out UDF's but I didn't see anything. I guess my question is this:

1.)Does anyone know any other way to enumerate shared printers or a script that would do this on a workstation.

2.)How do I parse a string using substr that looks like \\printserver\printer. I only want the value of the printer but the printserver and printer commands will be different (we have over 100 different print servers)size string on every workstation. Also need to include local shared printer too.

Thanks in advance.

[ 02. June 2003, 17:43: Message edited by: sealeopard ]

Top
#40926 - 2003-06-02 05:36 PM Re: Enumerating shared printers
smacleanwalker Offline
Lurker

Registered: 2003-06-02
Posts: 2
Loc: Bristol
Try shelling out to DOS and use
NET VIEW \\SERVERNAME |FINDSTR Print

PIPE it into a text file. Then load into an array.
Have just written a script with KIX forms to allow users to roam branches and ensure they have always got a local printer.
_________________________
Regards Steve

Top
#40927 - 2003-06-02 05:43 PM Re: Enumerating shared printers
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
code:
$printer=ArrayEnumKey('HKEY_CURRENT_USER\Printers\Connections')
for each $printer in $printers
? 'Printer = '+$printer
nnext

will get you started. ArrayEnumKey() is a UDF posted in the UDF Forum.
_________________________
There are two types of vessels, submarines and targets.

Top
#40928 - 2003-06-02 06:39 PM Re: Enumerating shared printers
sliver Offline
Getting the hang of it

Registered: 2002-09-05
Posts: 94
I am aware of the function but it doesn't really solve my problem. I need to find out if any printers on a given workstation are shared or not. If there are shared printers don't run the script....otherwise run it. I thought I could use the "Share Name" Key in the registry but the share name (in the registry) still exists by default even if you unshare the printer so that will not be accurate.

Did try the net view thing but that doesn't seem to work with consistency...I will have to mess around with that one a little more.

If anyone else know a way that would be great!!

Thanks for the help though sealeapord...you have always been good to me.

Top
#40929 - 2003-06-02 06:49 PM Re: Enumerating shared printers
sliver Offline
Getting the hang of it

Registered: 2002-09-05
Posts: 94
The problem I am having with the net view thing is for hidden printers!! Alot of the VP's hide there printers so no one else can use them and I do not want this script to run on pc's with printers shared out!!
Top
#40930 - 2003-06-02 07:00 PM Re: Enumerating shared printers
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
You should have been more specific in the first place. The sharename of a local printer is in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\your_printername_here as registry value Share Name. Just modify my script above to scan this new registry.

[ 02. June 2003, 19:21: Message edited by: sealeopard ]
_________________________
There are two types of vessels, submarines and targets.

Top
#40931 - 2003-06-02 07:17 PM Re: Enumerating shared printers
sliver Offline
Getting the hang of it

Registered: 2002-09-05
Posts: 94
That is how I have it currently set up....the only problem is that if you share a printer and then unshare it the 'Share Name' key keeps the share name in it's value by default. So machines that are not actually sharing a printer could be mistakenly identified as machines with a printer shared. I thought maybe there was a WMI thing you could do but I can't find it so I am pretty much stuck at this point.
Top
#40932 - 2003-06-02 07:23 PM Re: Enumerating shared printers
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
However, then check the Attributes value. It increases by 8 if the printer is shared and decreses by 8 if you unshare the printer. Read the FAQ Forum for tools on how to track changes in the registry.
_________________________
There are two types of vessels, submarines and targets.

Top
#40933 - 2003-06-02 08:08 PM Re: Enumerating shared printers
sliver Offline
Getting the hang of it

Registered: 2002-09-05
Posts: 94
Except that the number it starts at is random depending on the printer type (Ex- Acrobat PDF Writer started at 65 (before share)....document loader started 2096 (before share). So there is really no way to know what the number was before they shared it because they all use different types of printers (unless I found out every printer type being used and it's default attribute before share and included that in the script.)
Top
#40934 - 2003-06-02 08:27 PM Re: Enumerating shared printers
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
If you get the share name from the registry you can then test whether you can connect to that printer. Then evaluate the error codes to see what the status is.
_________________________
There are two types of vessels, submarines and targets.

Top
#40935 - 2003-06-06 12:28 AM Re: Enumerating shared printers
smacleanwalker Offline
Lurker

Registered: 2003-06-02
Posts: 2
Loc: Bristol
Home this is the script i've written. Requires kixforms but you get the idea.

Lists shared printers from a print server, then a gives user a list box of the printer to select.

The file name-ip.lst is a text file containing SERVERNAME IP Address
code:
 
;-----------------------------------------------------------------------------------------------------------------------
;script c2print.kix
;created Steven Maclean-Walker
;Date created 29 May 2003
;Last modified
;Overview Allows roaming XP laptops to automatically connect printers in local branches
;Uses Kix32.exe version 4.12 or later
; kixforms.dll version 2.20 or later
; Kixforms.dll requires registering using regsvr32.exe
;-----------------------------------------------------------------------------------------------------------------------


;-----------------------------------------------------------------------------------------------------------------------
;- Read current default printer device.
;- Finds Branch Server name the current printer is connectted too.
;-----------------------------------------------------------------------------------------------------------------------
$CurDefPrn = READVALUE('HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows', "Device" )
$Len001 = INSTR("\\",$CurDefPrn)
$Len002 = INSTR("\",$CurDefPrn)
$Len003 = INSTR(",",$CurDefPrn)
$CurDefPrnBrSvr = SUBSTR($CurDefPrn,$Len001-1,$Len002-$Len001-1)
$CurDefPrnName = SUBSTR($CurDefPrn,10,8)

;--- Debug Checking ---
? "Current printer - " + $CurDefPrn
? " Printer server - " + $CurDefPrnBrSvr
?
;--- Debug Checking ---


;-----------------------------------------------------------------------------------------------------------------------
;- Reads Machine IP address
;- Tidies up ip address so no blanks are showing
;- appends tidy workstation ip address
;- checks for branch subnet
;- appends branch server .11 ip address (assumes all branch servers are xxx.yyy.zzz.11)
;- N.B Above holds true for all branches bar SL2SVR (Solihull MPU) has 172.20 address
;-----------------------------------------------------------------------------------------------------------------------
$WorkIpAdd=SUBSTR(@ipaddress0,1,15)

;--- Debug Checking --- False IP Address
$WorkIpAdd=" 10. 11. 2.999"
;--- Debug Checking ---

$TempIpAdd=TRIM(SUBSTR($WorkIpAdd,1,3))+"."+TRIM(SUBSTR($WorkIpAdd,5,3))+"."+TRIM(SUBSTR($WorkIpAdd,9,3))
$CleanWorkIpAdd=$TempIpAdd+"."+TRIM(SUBSTR($WorkIpAdd,13,3))
SELECT
CASE SUBSTR($WorkIpAdd,1,4) = " 10."
$CleanSrvIpAdd=$TempIpAdd+".11"
CASE SUBSTR($WorkIpAdd,1,4) = "172."
SELECT
CASE SUBSTR($WorkIpAdd,5,4) = " 19."
$CleanSrvIpAdd = "172.19.2.51"
CASE SUBSTR($WorkIpAdd,5,4) = " 20."
$CleanSrvIpAdd = "172.20.2.103"
CASE SUBSTR($WorkIpAdd,5,4) = " 23."
$CleanSrvIpAdd = "172.23.2.52"
CASE 1
ENDSELECT
CASE 1
$CleanSrvIpAdd=$CleanWorkIpAdd
ENDSELECT

;--- Debug Checking ---
? "Current IP Address - " + $CleanWorkIpAdd
? " IP Address server - " + $CleanSrvIpAdd
?
;--- Debug Checking ---


;-----------------------------------------------------------------------------------------------------------------------
;- Reads name-ip.lst for branch server name from ip address
;- Yes I know you could use WINS / DNS to resolve but this is easier I can tell you. Bridging mutliple domains with no local logonserver
;- name-ip.lst is a text file in format below (no spaces in ip address)
; - ANYSVR www.xxx.yyy.zzz
;-----------------------------------------------------------------------------------------------------------------------
IF OPEN(1,"name-ip.lst")=0
$txt1=READLINE(1)
$len=LEN($txt1)
$str=INSTR($txt1,$CleanSrvIpAdd)
WHILE $str < 1 AND @ERROR = 0
$txt1=READLINE(1)
$len=LEN($txt1)
$str=INSTR($txt1,$CleanSrvIpAdd)
LOOP
IF @ERROR = -1
? " -*- END OF FILE name-ip.lst -*- "
ENDIF
CLOSE(1)
ENDIF
$SrvNmeLen=INSTR($txt1," ")
$LocalSrvName=UCASE(SUBSTR($txt1,1,$SrvNmeLen-1))

;--- Debug Checking ---
? "Current Server code- " + $LocalSrvName
? "Name String Length - " + $SrvNmeLen
;--- Debug Checking ---


;-----------------------------------------------------------------------------------------------------------------------
;- Select printer and prompt user to check printer
;-----------------------------------------------------------------------------------------------------------------------
;IF UCASE($CurDefPrnBrSvr) = UCASE($LocalSrvName)
; $PrnSelect=MESSAGEBOX("Current printer connected is " + $CurDefPrnName + ". Do you want to use this printer?","Current mapped printer",4,4096)

;--- Debug Checking ---
;? "Options selection - " + $PrnSelect
;--- Debug Checking ---

; IF $PrnSelect = 6
; SETDEFAULTPRINTER ("\\$CurDefPrnBrSvr\$CurDefPrnName")
; QUIT
; ELSE
; GOSUB ChoosePrinter
; ENDIF
;ELSE
GOSUB ChoosePrinter
;ENDIF


;-----------------------------------------------------------------------------------------------------------------------
;- ChoosePrinter creates GUI window with current branch printers
;-----------------------------------------------------------------------------------------------------------------------
:ChoosePrinter
SHELL "CMD /C net view \\$LocalSrvName | findstr Print > c:\temp\BranchPrinters.lst"

$PrintForm = CreateObject("Kixtart.Form")
$PrintForm.ClientWidth = 510
$PrintForm.ClientHeight = 200
$PrintForm.FontName = Verdana
$PrintForm.FontSize = 10
$PrintForm.Text = "Please select a printer"
$PrintForm.Center
$PrintForm.Show

IF UCASE($CurDefPrnBrSvr) = UCASE($LocalSrvName)
$PrintForm.PrintXY(10,10,"Your current printer is: $CurDefPrnName")
ELSE
$PrintForm.PrintXY(10,10,"Your default printer is not in this branch")
ENDIF
$PrintForm.PrintXY(10,30,"Please double click printer name to select")

; $txtinfo = $PrintForm.TextBox
; $txtinfo.Bounds = 50,10,100,25

$PrintList = $PrintForm.ListView()
$PrintList.Left = 380
$PrintList.Top = 10
$PrintList.Width = 115
$PrintList.Bottom = $PrintForm.ClientHeight - 10
$PrintList.OnDoubleClick = "Default_Click()"
$PrintList.Sorted = 1
$PrintList.MultiSelect = False
$PrintList.Gridlines = True
$PrintList.View = 3
$PrintList.FullRowSelect = False

$=$PrintList.Columns.Add("Printer Name",100)

Load_Click()

WHILE $PrintForm.Visible
$= EXECUTE($PrintForm.DoEvents)
LOOP
Exit 1

Function Default_Click()
$PrintList.SetFocus
? "\\"+$LocalSrvName+"\"+$PrintList.FocusedItem.SubItems(0).Text
;DELPRINTERCONNECTION ("\\"+$LocalSrvName+"\"+$PrintList.FocusedItem.SubItems(0).Text)
$PrintForm.PrintXY(10,140,"Please wait while printer connection is estabished...")
ADDPRINTERCONNECTION ("\\"+$LocalSrvName+"\"+$PrintList.FocusedItem.SubItems(0).Text)
$PrintForm.PrintXY(10,160,"Setting default printer... ")
SLEEP 1
SETDEFAULTPRINTER ("\\"+$LocalSrvName+"\"+$PrintList.FocusedItem.SubItems(0).Text)
$PrintForm.PrintXY(10,180,"connection estabished... ")
SLEEP 1
QUIT
EndFunction

Function Load_Click()
$PrintForm.MousePointer = 11
$PrintForm.Enabled = 0
$PrintForm.BeginUpdate
$PrintList.Items.Clear
IF OPEN(1,"c:\temp\BranchPrinters.lst")=0
$cnt=0
$txt1=READLINE(1)
WHILE @ERROR <> -1
$PrintItemTmp=SUBSTR($txt1,1,8)
$PrintItem = $PrintList.Items.Add($PrintItemTmp)
;--- Debug Checking ---
? "Printer in " + $CurDefPrnBrSvr " - " + $PrintItemTmp
;--- Debug Checking ---
$cnt = $cnt + 1
$txt1=READLINE(1)
LOOP
$cntmax = $cnt
;--- Debug Checking ---
? "Number of printers - " + $cntmax
?
;--- Debug Checking ---
CLOSE(1)
ENDIF
$PrintForm.EndUpdate
$PrintForm.MousePointer = 0
$PrintForm.Enabled = 1
$PrintList.Setfocus
EndFunction
RETURN


_________________________
Regards Steve

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1636 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.066 seconds in which 0.027 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org