Tuesday, August 15, 2006

Map Network Printers Logon Script

Last week I created a Windows Visual Basic Script file that will examine to find out what the user has as a default printer in Windows, record the printer if it's a network printer, then delete all network printers from the workstation, connect all network printers to the workstation, and then set the default printer back if it was a network printer. It works really well, but only on XP systems and newer, which stinks since we have a few Windows 2000 Pro workstations still sitting around here at work. With this script I can add a new line for a new network printer, or remove a network printer, and the next time everyone logs into their workstation it's setup or removed. I've deployed this as a logon script using a Group Policy Object in Active Directory. Here's the code if you'd like to borrow/use it, but it's at your own risk! '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Script Objective: ' ' To map network drive(s) to user workstation ' ' ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Created by: Troy D Murray ' Created on: 8/9/2006 @4:38p ' File name: MapNetworkPrinters2.vbs ' Notes: Initial script creation. ' ' ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Updated by: ' Updated on: ' File name: ' Notes: ' ' ' ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Header Section '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Create variables for reference ' On Error Resume Next '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Reference Section '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Assign variables their values or references ' strComputer = "." strOsVersion = "" strDefaultPrinterNetworkUnc = "" strPrinters = "" '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Worker Section '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Set WshNetwork = CreateObject("WScript.Network") Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colOSes = objWMIService.ExecQuery("Select * from Win32_OperatingSystem") ' Add a new printer mapping by copying one of the following lines ' and then modifying for the printer path used. ' NOTE: You must include a semi-colon at the end of the path! strPrinters = strPrinters & "\\srvr-print1\F1_IS_DELL3100;" strPrinters = strPrinters & "\\srvr-print1\F1_IS_DJ500;" strPrinters = Left(strPrinters,Len(strPrinters)-1) For Each objOS in colOSes strOsVersion = Split(objOS.Version,".") Next If strOsVersion(o) > 4 Then If strOsVersion(1) > 0 Then ' This is a Windows XP workstation or newer, do the script ' Query WMI for a list of the printers on this workstation Set colInstalledPrinters = objWMIService.ExecQuery _ ("Select * from Win32_Printer WHERE Network = TRUE") ' Determine if the default printer is a network printer For Each objPrinter in colInstalledPrinters If objPrinter.Default = True Then strDefaultPrinterNetworkUnc = objPrinter.Name End If Next ' Delete network printers from workstation For Each objPrinter in colInstalledPrinters WshNetwork.RemovePrinterConnection objPrinter.Name Next ' Map network printers to workstation strPrinters = Split(strPrinters,";") For i = 0 To UBound(strPrinters) If strPrinters(i) <> "" Then WshNetwork.AddWindowsPrinterConnection strPrinters(i) End If Next ' If old default printer was network printer, reset it as default If Len(strDefaultPrinterNetworkUnc) > 0 Then WshNetwork.SetDefaultPrinter strDefaultPrinterNetworkUnc End If Else ' This is a Windows 2000 workstaion, do nothing End If End If

No comments: