VBScript – Map Printers Based On IP / VLAN ID
Little script I made. Pretty self explanatory. Works real well.
' **************************************************** ' PRINTER MAPPER v0.9 ' **************************************************** ' This script will map a network printer at user ' logon based on what floor the computer is currently ' on. This is done by viewing the VLAN ID found in ' the IP address field. ' ' Scott T Richmond, ' IBM Australia. ' August 2009. ' ' TODO: ' - Turn off all reporting. ' - Error checking. ' ' Notes: ' - Setting default printer is simple, but no way ' to determine which one to set automatically so ' not used. ' objNetwork.SetDefaultPrinter "\\PRINTER_NAME" ' **************************************************** ' **************************************************** ' Compile Options ' **************************************************** ' Force variables to be explicitly defined or die: Option Explicit ' Continue script dispite any errors: On Error Resume Next ' **************************************************** ' Define Variables ' **************************************************** dim VLAN19 dim VLAN22 dim VLAN23 dim VLAN24 dim VLAN25 dim VLAN26 dim VLAN27 dim WIFI(2) dim IPArray(3) dim objWMIService dim objNetwork dim IPConfigSet dim IPConfig dim splitIP dim octet ' **************************************************** ' Set Variables ' **************************************************** VLAN19 = "139" VLAN22 = "145" VLAN23 = "147" VLAN24 = "149" VLAN25 = "151" VLAN26 = "153" VLAN27 = "155" WIFI(0) = "162" WIFI(1) = "184" WIFI(2) = "185" Set objNetwork = CreateObject("WScript.Network") Set objWMIService = GetObject("winmgmts:\\" & "." & "\root\cimv2") Set IPConfigSet = objWMIService.ExecQuery ("Select IPAddress from Win32_NetworkAdapterConfiguration ") ' **************************************************** ' Find and Parse Primary IP ' **************************************************** ' Primary IP is found by iterating through all interfaces ' and finding the best match for the LAN network IP. ' The code below discards ones that are: ' - NULL. ' - Anything other than a first IP octet of 10. ' - Or corresponds to the WiFi VLAN IDs. For Each IPConfig in IPConfigSet If Not IsNull(IPConfig.IPAddress) Then splitIP = Split(IPConfig.IPAddress(0), ".", -1, 1) If splitIP(0) = "10" Then If splitIP(2) = WIFI(0) or _ splitIP(2) = WIFI(1) or _ splitIP(2) = WIFI(2) Then WScript.Echo "Wi-fi connection found. Looking for LAN IP..." Else For octet=0 to 3 IPArray(octet) = splitIP(octet) Next End If End if End If Next ' **************************************************** ' Perform Work Based On VLAN ' **************************************************** ' Looks at third octet of the IP and determines what VLAN ' ID the IP address belongs to, then applies correct ' printer configuration. If IPArray(0) <> "10" Then WScript.Echo "Invalid IP. Not in the 10/8 network range." ElseIF IPArray(2) = VLAN19 Then WScript.Echo "IP is in VLAN 19. Mapping L19 printers..." objNetwork.AddWindowsPrinterConnection "\\PRINTSVR\L19UtilRmE-XerCol1" objNetwork.AddWindowsPrinterConnection "\\PRINTSVR\L19UtilRmE-XerBW1" objNetwork.AddWindowsPrinterConnection "\\PRINTSVR\L19UtilRmW-XerCol1" objNetwork.AddWindowsPrinterConnection "\\PRINTSVR\L19UtilRmW-HPPlot1" ElseIF IPArray(2) = VLAN22 Then WScript.Echo "IP is in VLAN 22. Mapping L22 printers..." objNetwork.AddWindowsPrinterConnection "\\PRINTSVR\L22UtilRm-XerCol1" ElseIF IPArray(2) = VLAN23 Then WScript.Echo "IP is in VLAN 23. Mapping L23 printers..." objNetwork.AddWindowsPrinterConnection "\\PRINTSVR\L23NthWest-XerBW1" objNetwork.AddWindowsPrinterConnection "\\PRINTSVR\L23UtilRmE-XerCol1" ElseIF IPArray(2) = VLAN24 Then WScript.Echo "IP is in VLAN 24. Mapping L24 printers..." objNetwork.AddWindowsPrinterConnection "\\PRINTSVR\L24UtilRmW-XerCol1" objNetwork.AddWindowsPrinterConnection "\\PRINTSVR\L24UtilRmE-XerCol1" objNetwork.AddWindowsPrinterConnection "\\PRINTSVR\L24UtilRmW-HPPlot2" ElseIF IPArray(2) = VLAN25 Then WScript.Echo "IP is in VLAN 25. Mapping L25 printers..." objNetwork.AddWindowsPrinterConnection "\\PRINTSVR\L25UtilRmE-XerCol1" objNetwork.AddWindowsPrinterConnection "\\PRINTSVR\L25UtilRmE-XerBW1" objNetwork.AddWindowsPrinterConnection "\\PRINTSVR\L25UtilRmW-XerCol1" objNetwork.AddWindowsPrinterConnection "\\PRINTSVR\L25UtilRmW-XerBW1" ElseIF IPArray(2) = VLAN26 Then WScript.Echo "IP is in VLAN 26. Mapping L26 printers..." objNetwork.AddWindowsPrinterConnection "\\PRINTSVR\L26UtilRmE-XerCol1" objNetwork.AddWindowsPrinterConnection "\\PRINTSVR\L26UtilRmW-XerCol1" objNetwork.AddWindowsPrinterConnection "\\PRINTSVR\L26NthEast-XerCol1" ' Printer below is a secure printer and should not be automatically installed. 'objNetwork.AddWindowsPrinterConnection "\\PRINTSVR\L26Cent-XerCol-1" ElseIF IPArray(2) = VLAN27 Then WScript.Echo "IP is in VLAN 27. Mapping L27 printers..." objNetwork.AddWindowsPrinterConnection "\\PRINTSVR\L27West-XerCol1" objNetwork.AddWindowsPrinterConnection "\\PRINTSVR\L27UtilRm2E-XerCol1" objNetwork.AddWindowsPrinterConnection "\\PRINTSVR\L27UtilRm1E-XerCol1" Else WScript.Echo "Unknown VLAN." End If WScript.Quit ' End of VBScript