Page 1 of 1 1
Topic Options
#62560 - 2002-01-30 02:19 PM Another Printer Script
MasterOfDesaster Offline
Fresh Scripter

Registered: 2002-01-23
Posts: 21
Loc: Munich, Germany
Hi,

i wrote the following script (and i stole some code from other scripts in here in the board). I have a Database on a SqlServer wich contains the configuration of every Networkprinter (Printername, PrintServer, Sharename, and the NT-Groupname which user get this printer). So I have 2 printservers with the same configuration, when one printserver will fall out, then i change in the database all printers to the running printserver. Everytime the user runs the script (in the loginscript), he gets the printers mapped on the other Printserver. I tested the script on NT4 and W2K Clients and NT4 Server.

code:
 
;Setzen der Laufzeitumgebung
break on
$=SetOption("wrapateol","on")
settitle("NetworkPrinter Mappings")
cls


;*****************************************
;Starten der einzelnen Subroutinen
;*****************************************

Gosub Variablen
;Gosub DBServerCheck
Gosub ReadDefaultPrinter
Gosub DelNetworkPrinter
Gosub Networkprinter
color n/n
exit 0


;*****************************************
;Festlegen der Variablen
;*****************************************
:Variablen


;Tabellenname in der Datenbank
$tblName="Printer"

;SQL Server, wo die Printer Datenbank liegt
$DBServer="SM009"

;ConnectString für die Verbindung zur Datenbank
$Connectstr="Provider=SQLOLEDB; Data Source="+$DBServer+"; Database=ZMDB; UID=sa; PWD=;"

return ;Variablen


;*****************************************
;Auslesen des bisherigen Standard Druckers
;*****************************************
:readDefaultPrinter

$keyDefaultPrt=readvalue("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows","Device")

$keybegin=instrrev($keyDefaultPrt,"\")+1
$keyend=instr($keyDefaultPrt,",")

$keyLength=len($keydefaultPrt)-$keyend

$DefaultPrinter=substr($keydefaultprt,$keybegin,$keylength)


return ;DefaultPrinter

;*****************************************************************************
;Löschen der bestehenden Netzwerkdrucker, lokale Drucker werden nicht gelöscht
;*****************************************************************************
elNetworkPrinter

$RC=deltree("HKCU\Printers\Connections")
errorcheck("DelPrinter: Del Registry")

$RC=addkey("HKCU\Printers\Connections")
errorcheck("DelPrinter: addkey")


return ;DelNetworkPrinter

;*****************************************
;Verbinden der Drucker
;*****************************************
:NetworkPrinter


;Öffnen der Datenbankverbindung via ADO
$objConn=CreateObject("ADODB.Connection")
errorcheck("DBS:CreatObject")

$objConn.Connectionstring = $Connectstr
errorcheck("DBS.Connectstring")

$objConn.Open()
errorcheck("DBS BSopen")


;Auslesen der Einzelnen Datensätze

$RS=$objConn.execute("select * from $tblname")
errorcheck("DBS:SQL query")

;Auslesen der Datensätze bis zum Tabellenende
while not $RS.eof

;Auslesen der einzelnen Felder in der Tabelle
$sqlprtShare =$rs.fields("PrtShare").value
$sqlPrtName =$rs.fields("PrtName").value
$sqlPrtServer =$rs.fields("ActPrinter").value
$sqlPrtGroup =$rs.fields("PrtGroup").value
errorcheck("DBS: Values")

;Löscher aller Leerzeichen in den Variablen
$sqlprtshare=rtrim($sqlprtshare)
$sqlPrtname=rtrim($sqlPrtname)
$sqlPrtServer=rtrim($sqlPrtServer)
$sqlPrtGroup=rtrim($sqlPrtGroup)

errorcheck("DBS: RTrim Variables")

;Aufruf der Funktion Mapprinter mit Parametern
mapprinter($sqlPrtServer, $sqlPrtShare, $sqlPrtName, $sqlPrtGroup)


;Setzen des Defaultprinters
if $sqlPrtName=$DefaultPrinter
setdefaultprinter("\\"+$sqlPrtServer+"\"+$sqlPrtName)
endif

$rs.movenext ;zum nächsten Datensatz weiter
loop

;Schließen der Datenbankverbindung
$objConn.close


return ;Networkprinter


;*****************************************
; Prüfen ob der Datenbankserver online ist
;*****************************************
BServerCheck

if ping($DBServer,0) =0
? "Datenbankserver nicht online"
exit 99
endif

return


;*****************************************
;Funktion zum Netzwerkdrucker verbinden
;Start mit mapprinter(, ,,)
;*****************************************

function MapPrinter($PRServer, $PRShare, $PrinterName, $PrtGroup)

$regkey="HKCU\Printers\Connections\,,"+$PRServer+","+$PrinterName

if ingroup($PrtGroup) =1
if keyexist($regkey)
? "Drucker \\$PRServer\$PRShare bereits vorhanden"
return
else
if addprinterconnection("\\"+$PRServer+"\"+$PRShare) =0
color w/b
? "Verbinde Netzwerk-Drucker \\$PRServer\$PRShare"
color n/n
else
color r+/n
?
? "Fehler beim Verbinden mit Netzwerkdrucker \\$PRServer\$PRShare"
? @serror
?
color n/n
endif
endif
else
if keyexist($regkey)
if delprinterconnection("\\"+$PRServer+"\"+$PRShare) =0
?" Lösche Netzwerkdrucker \\$PRServer\$PRShare"
else
color r+/n
?
?" Fehler beim Löschen des Netzwerkdruckers \\$PRServer\$PRShare"
? @Serror
?
color n/n
endif
else
? "Nix da!"
return
endif

endif

endfunction ;Mapprinter


;*****************************************
;Funktion errorcheck
;Start: Errorcheck()
;*****************************************

Function errorcheck($point)

if @error<>0
color r+/n
beep
? "Fehler beim Programmpunkt: " $point
? @serror
?
color n/n
quit 99
endif

endfunction

;FUNCTION Ping()
;
;ACTION Pings the Computer specified, or returns its ip address (Wins Resolved)
;SYNTAX Ping(Computer,GetIP,[LoopCount],[Timeout])
;
;PARAMETERS Computer (Required)
; - String value representing the Computer to ping
;
; GetIp (Required)
; - If specified (1), the function will return the ip-address
; of specified Computer rather than pinging it ...
; Has to be 0 if you want to check for reply !
;
; LoopCount (Optional but useful !)
; - Integer value representing the number of times
; the Computer shall be pinged
;
; Timeout (Optional)
; - if ommited the Computer gets pinged with the default
; timeout (1s) and default retries (4)
;
;REMARKS If there is a reply the function will return immediately with 1
; so it could be faster not specifiying a timeout.
; The return of IP addresses works only with NT / 2000 / XP , on 9x it will fail
;
;RETURNS 0 - No reply
; 1 - Ping reply
; Optional - ip address of specified Computer
;
;DEPENDENCIES OS: NT / 2K / XP
;
;EXAMPLES call "path\Ping.udf"
; if Ping("www.microsoft.com",0,12,5000) ;pings for max. 60 seconds
; run "%ComSpec% /c start www.microsoft.com"
; else
; 'Site isn't available ' ?
; endif
; if Ping("www.microsoft.com",0,15) ;pings also for max. 60 seconds
; run "%ComSpec% /c start www.microsoft.com"
; else
; 'Site isn't available ' ?
; endif
; $ip = Ping("www.microsoft.com",1)
; $ip ?

function Ping($Computer,$GetIP,optional $LoopCount,optional $TimeOut)
if $GetIP
dim $ip, $ipfile
$ipfile = %TEmp% + '\ip.txt'
if exist ($ipfile) del $ipfile endif
shell '%Comspec% /q /e:1024 /c for /F "tokens=2 delims=[]" %%i IN ('+ chr(39)
+ '"ping $Computer -n 1 | find "]""' + chr(39) + ') do echo %%i >' + $ipfile
$ = open(10,$ipfile,2) $ip = readline(10) $ = close(10) del $ipfile
if $ip
$Ping = $ip
else
$Ping = 'Bad IP address $Computer !'
endif
exit
else
if $TimeOut
for $c = 0 to $LoopCount
shell '%Comspec% /C ping $Computer -n 1 -w $TimeOut | find /C "TTL=" > nul'
if @error = 0
$Ping = 1
exit
endif
next
else
for $c = 0 to $LoopCount
shell '%Comspec% /C ping $Computer | find /C "TTL=" > nul'
if @error = 0
$Ping = 1
exit
endif
next
endif
$Ping = 0
endif
endfunction



Keep Scripting!!!!

_________________________
Greetings,

Michael

Top
#62561 - 2002-01-30 03:27 PM Re: Another Printer Script
Will Hetrick Offline
Hey THIS is FUN

Registered: 2001-10-02
Posts: 320
Loc: Harrisburg, PA USA
Very Nice!
_________________________
You have at least 2 choices. Each choice changes your destiny. Choose wisely!

Top
Page 1 of 1 1


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

Who's Online
0 registered and 633 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.041 seconds in which 0.012 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