|
server
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
AD, W2003Svr, connect Printer from CSV file via Script to Host?
bloody newbie here (in case of Win2003Svr, AD and that WSH thingie) The conditions: ------------------ A network, Windows 2003 R2 Domaincontroller, Active Directory and a CSV file. Several usergroups within several OUs. Only network printers, no local ones. 400+ Computers and 140+ Printers The problem: ---------------- To serve (a) network printer(s) to a/the user(s) the CSV file exist. In this file are all hostnames stored and for every hostname amount four/five printers. So to say, the network printers and the hostnames are stored into this CSV file for a central administration. The CSV file looks like this: host-name; printer1; printer2; printer3; printer4; printer5 Cronos; Dr0102249; Dr102012; infotek-3030; Dr050607; Dr060708 Torsos; Dr908070; Dr807060; Dr706050; Dr605040; .... The dream: -------------- Every time a user logs in (at the Domain) should run a script (at the server) that do the following steps: - check, who is logging in (the hostname) - open the CSV file, find the line with the hostname - connect the five printers to that machine - set the first printer of the line to the default printer - close CSV file An Example: Joe Random logs in with the Host "Cronos". He should get the printer Dr010203 as default printer, the other 4 ones as additional printers. And: i can _not_ use 3rd party software, the challange is to realise that with Windows2003 R2 internas. I've been trying out some things with WSH and scripting, checking the www for my needs, but i didn't found a suitable solution. I have no experiences in scripting but here is what i did. The following script do _not_ work. The stringcompare fails and the printer also do not connect. I thought to step through the lines of the csv and read in the strings into an array. '*********************************************************************** ' * * * * * * * * * * * * * * * * * * * * * * * * '*********************************************************************** '* Plattform: Windows2003Server, x86 * '* Umgebung: WindowsScriptingHost * '* Dateiname: PrinCon.vbs * '* Arbeitstitel: wsh_playground_6.vbs * '* Funktion: Verbinde alle Drucker zu einem der Domain * '* gehoerenden Host, setze den Standarddrucker (CSV) * '* ACHTUNG: Computerbezogen, nicht Benutzerbezogen !! * '* Datum: 22. Oktober 2007 * '*********************************************************************** ' ===== explicit values OPTION EXPLICIT ' ===== define some consts CONST CSV_ORDNER = "Z:\Eigene Dateien\_code_bin\" CONST CSV_DATEI = "prn-csv.csv" CONST ForReading = 1 ' ===== declare values DIM obj_NetHost, obj_NetPrnServer, WSHShell, FSO, obj_FSO, obj_fileExist DIM FileIn, FileOut, TXT, TXT2 DIM Ar_Host, Ar_Prn, Ar_Prn0, Ar_Prn1, Ar_Prn2, Ar_Prn3, Ar_Prn4 DIM host_current, host_fromCSV PUBLIC status1 ' ===== create a network object SET obj_NetHost = WScript.CreateObject ("WScript.Network") ' ===== check computername here host_current = "" DO host_current = obj_NetHost.ComputerName LOOP WHILE (host_current="") SET obj_FSO = CreateObject("Scripting.FileSystemObject") SET obj_fileExist = obj_FSO.GetFile(CSV_DATEI) 'check if file is empty IF obj_fileExist.Size > 0 THEN '######################## HOT Code here: ######################## SET WSHShell = WScript.CreateObject("WScript.Shell") SET FSO = CreateObject("Scripting.FileSystemObject") SET FileIn = FSO.OpenTextFile(CSV_DATEI,1,TRUE) status1 = 0 DO WHILE NOT (FileIn.atEndOfStream) TXT = FileIn.ReadLine Ar_Host = Split(TXT,";",-1,1) host_fromCSV = Ar_Host(0) Ar_Prn = Split(TXT,";",-1,1) Ar_Prn0 = Trim(Ar_Prn(1)) Ar_Prn1 = Trim(Ar_Prn(2)) Ar_Prn2 = Trim(Ar_Prn(3)) Ar_Prn3 = Trim(Ar_Prn(4)) Ar_Prn4 = Trim(Ar_Prn(5)) 'echo here to see what happens, only for debug IF UCase(Trim(host_fromCSV)) > UCase(Trim(host_current)) THEN WScript.Echo UCase(host_current) & " <---------> " & UCase(host_fromCSV) & vbCRLF & _ "--------------------------------------------------------" & vbCRLF & _ "Drucker 1: " & Ar_Prn0 & vbCRLF & _ "Drucker 2: " & Ar_Prn1 & vbCRLF & _ "Drucker 3: " & Ar_Prn2 & vbCRLF & _ "Drucker 4: " & Ar_Prn3 & vbCRLF & _ "Drucker 5: " & Ar_Prn4 status1 = 1 EXIT DO END IF LOOP ' ===== analysis of read-in of csv file and set printers SELECT CASE status1 CASE 0 WScript.Echo "No suitable entry in CSV found!" & vbCRLF _ & "Connecting printers failed!" CASE 1 'echo here to see what happens, only for debug WScript.Echo "Printer: " & Ar_Prn0 &" connecting?" SET obj_NetPrnServer = WScript.CreateObject("WScript.Network") ' here are some tryouts , nothing worked :((( 'obj_NetPrnServer.RemovePrinterConnection "LPT1", TRUE, TRUE obj_NetPrnServer.AddPrinterConnection "IP", "\\print\" & Ar_Prn0 'DR102249" obj_NetPrnServer.AddPrinterConnection "COM", "\\print\" & Ar_Prn1 'DR102012" obj_NetPrnServer.AddPrinterConnection "LPT3", "\\print\" & Ar_Prn2 'InfoTec-3030" 'obj_NetPrnServer.AddPrinterConnection "LPT4", "\\print\" & Ar_Prn3 'DR203040" 'obj_NetPrnServer.AddPrinterConnection "LPT5", "\\print\" & Ar_Prn4 'DR304050" CASE ELSE WScript.Echo "Error!" objNetwork.RemovePrinterConnection END SELECT '######################## End HOT Code here ######################## ELSE Wscript.Echo "The file > " & CSV_DATEI & " < is empty or does not exist." END IF WScript.Quit '********************************************************************* '*** E N D *** '********************************************************************* Maybe i'm on the right way, maybe not. Every help is welcome! Please excuse my spelling, i'm not nativ english. Matthias |
|||||||||||||||||||||||